Trying to delete a file in Windows only to get an error message that the file is open or in use in another program is a common issue many users face. The error message typically says “This action can’t be completed because the folder or a file in it is open in another program” or “It is being used by another person or program”.
This happens because Windows blocks deleting files that are currently opened by other applications. If the file is open in another program, Windows cannot access it to delete it. While this is a safety measure to avoid corrupting files, it can be frustrating when you need to delete a file right away.
There are several methods you can try to delete a file that is reported as open or in use. This guide will walk through the most common troubleshooting steps to close the file, stop the processes using it, or override the block by Windows to force delete the file.
Check if the File is Really in Use
Sometimes a file can report that it is in use or open even when no programs are actively accessing it. This can happen if a program crashed or didn’t close the file properly. The operating system may still have the file locked and marked as “in use.”
To check if the file is really in use, you can use the Task Manager in Windows or lsof command in Linux to view open file handles. This will show you if any programs actually have the file open. You can also try opening the file in another program to test if it is truly locked or access denied.
If nothing is shown to have the file open, it is likely a false report of the file being in use. You may need to restart your computer or unlock the file to fully release it.
Sources:
https://stackoverflow.com/questions/2987559/check-if-a-file-is-open
https://www.unix.com/shell-programming-and-scripting/89568-see-if-file-open.html
Close the Program Using the File
The easiest way to delete a file that is in use is to close the program that is using the file. To do this:
1. Open Task Manager by pressing Ctrl + Shift + Esc on your keyboard. You can also search for “Task Manager” in the Windows search bar.
2. In Task Manager, click on the Processes tab. This shows all running programs and processes on your computer.
3. Scroll through the list of processes until you find the program name that is using the file. This may require some detective work to match the file with the right program.
4. Right click on the program name and select “End task” to force close the program. This will free up the file and allow you to delete it.
5. You can also end the process tree by right clicking and selecting “Go to details”, then right click on the process and choose “End process tree”. This ends all associated processes.
6. After closing the program, try deleting the file again in File Explorer. If the file can now be deleted, the program was successfully closed.
Closing the program properly ensures any unsaved data is not lost. Forcing a program to close incorrectly with Task Manager should only be used as a last resort if closing normally does not work.
Source: https://www.makeuseof.com/tag/5-ways-delete-file-use-windows/
Use Task Manager to End Processes
The Windows Task Manager allows you to view and end processes that may be locking the file. To use it:
- Open Task Manager by pressing Ctrl + Shift + Esc or right-clicking the taskbar and selecting “Task Manager”.
- Go to the “Processes” tab.
- Look for any process using the file. This may show as the file name under the “Image Name” column.
- Right-click the process and select “End task” to terminate it. This will forcibly end the process and release the file.
- You can also select the process and click “End task” at the bottom.
Ending the process holding the file open this way will allow you to delete the file. However, first try closing any associated programs normally to release the file gracefully. Only use Task Manager if you cannot close the program normally or don’t know what is using the file. For more information, see this article on ending tasks with Task Manager.
Restart Your Computer
One of the simplest ways to close any programs using the problematic file is to restart your computer. When Windows restarts, it will force close all open applications and processes. This will release the file handle and allow you to delete the file after the restart.
To restart your computer:
- Save any work in open applications that you want to keep.
- Click the Start menu and select the Power button near the top-right.
- Choose ‘Restart’ from the menu that appears.
After your computer fully reboots, you should be able to delete the previously locked file without issue. Just navigate to the file location and use the Delete option.
Restarting your computer is often the quickest and simplest solution if a file appears locked or in use. It will close all open handles and release the file. Just be sure to save your work first!
Unlock the File
One option to delete a file that says it’s in use is to use a tool like Unlocker to unlock the file first. Unlocker is a free utility that can force delete files that Windows says are locked or in use.
To use Unlocker:
- Download and install Unlocker from the official website or a trusted download site like MajorGeeks.
- Right click on the file you want to delete and select “Unlocker” from the context menu.
- Unlocker will show you which program has locked the file. Click “Unlock All” to remove the lock.
- You can now delete the file as normal in Windows.
Unlocker works by closing handles that programs have open on the file. This releases the lock so you can delete the file. It provides an easy way to remove pesky files without rebooting or using the command line.
Delete on Reboot
One option to delete a file that says it is open or in use is to schedule the file for deletion on the next system reboot. This will force the file to be deleted when Windows restarts, even if a program has the file open.
To schedule a file for deletion on reboot, you can use the PendingFileRenameOperations registry key. This key allows you to list files that will be deleted when the system reboots next time. To use this method:
- Open Registry Editor and navigate to HKLM\SYSTEM\CurrentControlSet\Control\Session Manager
- Right click the PendingFileRenameOperations key and select New > Multi-String Value
- Give the new value a name, this will be the full path and filename you want to delete
- Reboot your PC and the file will be deleted
This will force deletion of the file when Windows starts up again, even if the file is locked or open. Just be careful when using this method to avoid accidentally deleting important files (source).
Edit Registry to Remove File Handles
The Windows registry contains information about open files and running processes. Advanced users can edit the registry to manually remove file handles that prevent a file from being deleted. However, caution is advised when editing the registry, as making mistakes can cause system instability or prevent Windows from starting properly.
To remove a file handle in the registry, first export a backup of the registry keys you plan to edit (for rollback if issues arise). Then use the registry editor tool RegEdit to navigate to HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services
. Carefully delete any subkeys referencing the stuck file. Also check HKEY_USERS
and HKEY_CURRENT_USER
for related keys. After editing the registry, restart the computer for changes to take effect.
Again, extreme caution is recommended when editing the registry directly, as there is a risk of damaging the operating system. Consider trying less invasive methods first. Make registry tweaks only if you understand the risks involved.
Delete from Command Prompt
You can delete files and folders using the Command Prompt in Windows. This allows you to remove files that may be locked or in use through a simple command line interface.
To delete a file using Command Prompt:
- Open the Start menu and search for “Command Prompt”. Right-click on it and select “Run as administrator”. This opens an elevated Command Prompt window.
- Navigate to the folder containing the file you want to delete. For example, to navigate to the Windows folder, type
cd C:\Windows
and press Enter. - Type the command
del filename
and press Enter. Replace filename with the actual name of the file.
For example, to delete the file test.txt, you would type del test.txt
.
To delete a folder, use the rmdir
command instead of del. For example, rmdir foldername
would delete the folder named foldername.
Using the elevated Command Prompt allows you to forcibly delete files and folders, overriding any locks or permissions. Just be cautious, as deleted files can’t be recovered through this method.
Recover Deleted Files
If you accidentally delete an important file, there are several ways to try to restore it:
First, check the Recycle Bin on your desktop. Any files you delete are usually sent there, unless you permanently deleted them by pressing Shift + Delete or emptying the Recycle Bin. You may be able to restore the file by opening the Recycle Bin, right-clicking on the file, and selecting Restore.
You can also try using the builtin File Recovery tool in Windows. Go to Settings > Update & Security > Backup > More options > Advanced setup. Under Restore files from a current backup, select File recovery. This will scan your system for deleted files that can potentially be recovered.
Third party data recovery software like Recuva can also help recover deleted files. These tools scan your hard drive and pull up lists of files that have been deleted but not yet overwritten. You can then selectively restore the needed files.
Finally, if you have System Restore or previous file versions enabled on your system, you may be able to go back to a previous restore point or version when the file still existed. Right click on the folder where the file used to be, select Properties > Previous Versions tab, and browse through earlier versions to find the deleted file.