Cannot delete or open specific files that are 'in use'

While rare and usually because of a bug, hang, or crash, sometimes files in OS X may be stuck with an "in use" lock that will prevent them from being edited or managed by other programs. Here is how to address this problem if it occurs.

Sometimes when managing files with various programs in OS X, you may experience a problem with the program such as a hang, crash, or some other odd hiccup that interrupts the program's read or write process. While most of the time this results in you being able to delete and recreate the file or otherwise reopen it to continue your work, sometimes it can result in a file being locked by the system so it cannot be edited or managed by any program, or even deleted.

Apple discussion poster "Spprrw" writes:

"I tried to download yahoo messenger to my computer and I accidentally downloaded 2 of them (I think),but only one was a full one. The other I cannot get rid of now. When I try to delete it, it says "The operation can't be completed because the item "" is in use."

This is a lie. It is not in use. If I pull it out of the trash and try to open it it tells me this "Safari can't resume this download. The file "" doesn't appear to be a valid download file."

How do I get rid of this vile corrupt file? It's driving me crazy and I want it OFF My computer.

When a file is locked like this, it is because a currently running process has reserved the file. Generally when a program access a file, it uses code to access the file and briefly lock it so the file cannot be edited while the program has access to it. The file is then read or written to and immediately released so there are no more locks on the file. In rare instances programs will maintain locks on files, but usually when this happens it is because of an error in the programming or a glitch.

If you find a file is locked, you can use the following procedure to try to locate the process responsible for the lock:

  1. Open the Terminal program and type "lsof" at the command prompt, followed by a single space.

  2. Drag the problematic file to the Terminal window so the full path to it completes.

  3. Press enter and see if any output points to an active system or user process.

The "lsof" command is used to list open files on a system, and has extensive options for customizing which files are shown and targeted, but the basic use shown above should be sufficient for the task at hand. If you just run "lsof" by itself then you will see all opened files on the system.

When you have identified the process responsible for holding the lock on the file, if it is an application, then you can try quitting it and deleting the file. If the process is a background process, then you can try quitting it using Activity Monitor, but keep in mind that forcing some background processes to quit may destabilize the system (see this article about OS X system processes to see which ones are critical versus those that can more readily be forced to quit).

Besides using Activity Monitor, the best way to clear a file lock is to either log out of the system and log back in (provided the process with the lock is being run under the current user's account) or to just reboot the system, which will quit and restart all system processes.

Questions? Comments? Have a fix? Post them below or e-mail us!
Be sure to check us out on Twitter and the CNET Mac forums.

Featured Video

Why do so many of us still buy cars with off-road abilities?

Cities are full of cars like the Subaru XV that can drive off-road but will never see any challenging terrain. What drives us to buy cars with these abilities when we don't really need them most of the time?

by Drew Stearne