How to force delete a file that is in use by another program?

Trying to delete a file that is currently in use or open by another program can be frustrating. You may get error messages saying the file is in use or access is denied. There are a few quick solutions to force delete files in use on Windows and Mac systems.

Quick Answer

To quickly force delete a file in use on Windows:

  1. Close the program using the file if possible
  2. Use Task Manager to end task on the program locking the file
  3. Restart your computer to clear any locked files
  4. Use Unlocker utility to delete locked files

On Mac:

  1. Close any apps using the file
  2. Use Force Quit to quit apps locking the file
  3. Restart your Mac
  4. Use Terminal to delete the locked file

Why Can’t I Delete a File in Use?

When a program is using a file, it puts a lock on the file to prevent other programs or users from accessing or modifying it while it is open. This lock prevents the file from being deleted as well. Some common examples of programs locking files:

  • A Microsoft Word document you have open to edit or read
  • An Excel spreadsheet you have open to update
  • A media file opened in a program like VLC media player
  • A system file being accessed by Windows or another program

Trying to delete the file results in errors like:

  • “File in use” or “Access denied”
  • “This file is open in another program”
  • “Cannot delete file: make sure the disk is not full or file is not in use”

The file lock prevents all access to the file, including deleting, moving or renaming it. To delete a locked file, you first need to remove the lock.

How to Delete Locked Files on Windows

Here are the main ways to remove file locks and force delete files in use on Windows:

Close Any Programs Using the File

The easiest way is to properly close the program using the locked file. This releases the lock and allows deleting the file. For example, close Microsoft Word to delete a .doc file it has open. Make sure to properly exit the program – not just minimizing it.

Use Task Manager to End Task

If you cannot close the program normally, you can use Task Manager to force end the program or process locking the file. This removes any file locks held by the program.

  1. Press Ctrl + Shift + Esc to open Task Manager
  2. Go to the “Processes” tab
  3. Find the program locking the file
  4. Select it and click “End task”
  5. This will force the program to close, releasing the file lock
  6. You can now delete the file

Restart Your Computer

Restarting your computer will clear any file locks and allow you to delete any files. This should release any locked system files as well. Restarting closes all running programs and restarts the file system.

Use Unlocker Utility

For an easy way to delete locked files, use a dedicated utility like Unlocker. This lets you delete files with a right-click even if in use. Other utilities like LockHunter also work.

  1. Download and install Unlocker
  2. Right-click on the locked file and select Unlocker
  3. Click “Delete” to force delete the file

Unlocker will close the handle or process locking the file behind the scenes, allowing it to be deleted.

How to Delete Files in Use on Mac

On Mac OSX, here are some ways to force delete files that are locked or in use:

Close Any Apps Using the File

As on Windows, properly quitting the app using the file will release the lock allowing you to delete it. For example, close Preview to delete a .pdf file it has open.

Use Force Quit

If the app won’t close normally, you can use Force Quit to make it quit, releasing any file locks. To Force Quit an app:

  1. Press Cmd + Opt + Esc to open Force Quit
  2. Select the app locking the file
  3. Click Force Quit
  4. This will force the app to close so you can delete the file

Restart Your Mac

Restarting your Mac essentially Force Quits all running apps and restarts the file system. This will allow you to delete any previously locked system files as well.

Use Terminal to Delete

On Mac you can also use the Terminal app to delete locked files by entering commands. For example:

  1. Open Terminal
  2. Enter: sudo rm /path/to/filename
  3. Enter admin password when prompted
  4. File will be force deleted

Using sudo overrides the file lock allowing deletion.

Tips for Deleting Files in Use

Follow these tips when trying to delete files in use:

  • Close apps using the file properly before deleting
  • Know which app has the file open if possible
  • Use Task Manager on Windows to end the locking process
  • Force Quit misbehaving apps on Mac
  • Restarting your system clears all file locks
  • Use unlocker tools like Unlocker for Windows
  • Terminal commands can override file locks on Mac

Why Programs Lock Files

Programs intentionally lock files while using them to ensure data integrity. Locking prevents the following problems:

  • Data corruption: If multiple programs edited the same file at once, data could be corrupted.
  • Data collisions: Locks prevent two users from unsafely editing the same file simultaneously.
  • Partial reads/writes: Locks prevent a file being changed while a program is still reading it.
  • Unauthorized access: Locks act as a form of access control to the file while in use.

So file locks are an important mechanism for programs accessing files on your system. But sometimes these locks persist unintentionally, preventing file deletion. In these cases, the methods above can help remove the locks.

Troubleshooting Files You Can’t Delete

If you still can’t delete a file in use, try these steps:

  1. Double check no programs are using the file in the background. Check Task Manager on Windows.
  2. Completely reboot your system if a normal restart did not work.
  3. Update or reinstall the application locking the file.
  4. Disable or stop any security or antivirus software temporarily.
  5. Check for file and folder permission issues preventing access.
  6. Use the Command Prompt on Windows or Terminal on Mac for advanced delete operations.
  7. As a last resort, delete the file on next reboot using a tool like DeleteOnReboot.

Conclusion

Deleting files in use can require some troubleshooting, but using the quick methods outlined can help you remove pesky file locks. Properly closing programs using files, restarting your system, and using unlock tools are generally effective. Understanding what causes file locks can help avoid issues deleting files in the future.