macOS Trash – Yosemite Issues with Emptying Trash

macostrash

I am on Yosemite (10.10.1) and I noticed the annoying habit of not wanting to empty the trash — it keeps whining that The operation can’t be completed because the item “whatever” is in use. This is starting to piss me off – I can go to .Trash and remove stuff by hand, but is going on? Once a file is not in use and has been binned, why can't the OS recognise it's not actually linked to any application? This problem has come up whether the app used for that kind of file is still open or not btw.

Best Answer

To find out what file(s) in your Trash are in use, and by whom:

lsof +D ~/.Trash

The command name in the first column will be truncated. If it's truncated so much that you cannot identify the process, the second column contains the pid (Process ID), which you can cross-reference in Activity Monitor. Or get additional info using ps -p pid.

Note:

It has been suggested that the command:

sudo rm -rf ~/.Trash

may be useful here. This is not the case!

If a file is in use (which is the scenario the question is about), it cannot be removed! Not even by root.

The suggested command may be useful if:

  • You are in an admin account (or in sudoers, which I discourage as a security weakness), and
  • The only reason you cannot remove the file is that its permissions deny deletion.

In general there are three reasons why you might not be able to empty the trash, and each reason has its own cure.

  • If the file (or folder) is locked, you can either unlock it or hold down the option key while emptying the trash.

  • If permissions do not allow you to remove the file, you can either change the permissions to allow it, or use sudo to override the file's permissions.

  • If the file is in use, your only recourse is to find the process that is using the file and make it stop using it. In the simplest case, trying again after a short wait is all it takes. It may be necessary to force quit the app, and the purpose of the lsof command is to determine which app needs to be quit. However, even root cannot remove a file that is in use.

The takeaway is that the problem "I cannot empty the trash" does not have a one-size-fits-all solution. When you are told that something is in use, do not waste time trying to apply a fix that doesn't fit that situation.

I have also seen it suggested that Secure Empty Trash may be helpful. Again, this is not the case! Secure Empty Trash will overwrite the sectors on disk that the file's data was stored in, but only after the file is successfully deleted. If you can't delete the file, for whatever reason, it won't be deleted or overwritten. If the file is hard-linked, deleting it only deletes one of its links. As long as other links remain, the underlying inode is not deleted and the file's disk space is not overwritten.