Deleting files that are being used or are stuck in Windows can be a frustrating experience. Sometimes when you try to delete a file, you may get an error saying “The action can’t be completed because the file is open in another program” or “Access is denied”. This happens when a program has locked the file and Windows cannot delete it until the program unlocks it. However, there are ways to force delete files in Windows using the Command Prompt. Here’s a guide on how to force delete files using the cmd delete command.
What does “Access is denied” error mean?
When you get an “Access is denied” error when trying to delete a file, it means that the file is currently being used by another program or process. Windows locks files that are in use to prevent them from being modified or deleted accidentally while a program is accessing them. So if another program has the file open or is using it in some way, Windows will not allow you to delete the file until it is released from use by the other program.
Some common examples of programs that may lock files:
- Antivirus or security software scanning the file
- Another user having the file open over the network
- A program crashed while using the file and still has it locked
- The file is required by an application or Windows process that is currently running
So the “Access is denied” error occurs because Windows is just doing its job by preventing the file from being changed while in use. To delete the file, you first need to find and close the program that is locking the file.
How to find what program is locking the file
If you are not sure what program has locked the file you want to delete, you can use the following steps to identify it:
- Open Task Manager in Windows by pressing Ctrl + Shift + Esc.
- Go to the Processes tab.
- Click on the Image Name column header to sort the processes alphabetically.
- Look through the list of processes for any that might have the file open. Common culprits include antivirus programs, Office programs, web browsers, etc.
- Select a suspicious process, right click it and choose “Open File Location”.
- This will open the folder containing the program executable. Check if the locked file is in use by that program.
- If you find the program locking the file, close the program and try deleting the file again.
Using Task Manager this way enables you to identify and close the offending program so the file can be deleted.
How to unlock and force delete a file with CMD
If closing the program does not resolve the issue, you can use the Command Prompt to force delete locked files. The CMD delete command includes switches that allow you to delete read-only files as well as bypass the Recycle Bin.
Here are the steps to force delete a file using CMD:
- Open the Command Prompt as administrator.
- Type the following command to delete the file, replacing filename with the actual filename:
del /f /s /q filename
- The switches used are:
/f
– Force deletion of read-only files/s
– Delete specified file from current directory and all subfolders/q
– Quiet mode, do not ask for confirmation
- Press Enter. The file will be forcefully deleted ignoring any restrictions.
Here are some other useful CMD delete switches:
Switch | Description |
---|---|
/a |
Deletes only files with the read-only attribute set. |
/p |
Prompts for confirmation before deleting each file. |
/r |
Overwrites file contents with zeros before deletion. Use to securely erase files. |
/s |
Deletes the specified file from the current directory and all subdirectories. |
/q |
Quiet mode, does not prompt for delete confirmation. |
Using these CMD options bypasses all the usual restrictions and force deletes the file. Just be cautious when force deleting system files.
Alternative methods to force delete locked files
Aside from using the CMD delete command, there are a couple other ways you can delete locked files in Windows:
1. Restart in Safe Mode
Restarting the computer in Safe Mode loads Windows with only the bare essential drivers and services. This prevents most programs from running that could lock files. While in Safe Mode you should be able to delete previously locked files.
To restart in Safe Mode:
- Open the Start Menu and click the Power button.
- Hold down the Shift key while clicking Restart.
- Troubleshoot > Advanced options > Startup Settings > Restart
- After restarting, select Safe Mode from the startup menu.
- Now try to delete the file again.
2. Unlocker utility
Unlocker is a free utility that can force delete locked files when standard methods do not work. It integrates with Windows Explorer right-click context menu for convenience. Simply right-click a locked file, select Unlocker, and choose the delete option.
Unlocker works by closing handles that programs have open on the file you want to delete. This allows it to remove restrictions so the file can be deleted.
3. Handle.exe from Sysinternals
The Handle utility is part of the Sysinternals suite of tools from Microsoft. It shows you which handles (resources) are open by each process. You can then use the handle ID to force close specific handles that may be locking files.
To use Handle to delete locked files:
- Run Handle.exe and find the file handle for the locked file.
- Double click the handle entry to bring up the Properties box.
- Click Close Handle to release the lock on the file.
- Now you should be able to delete the file.
Conclusion
Being unable to delete a file due to it being locked or in use can be annoying. But Windows is just trying to prevent accidental changes or corruption. Using the CMD delete command with the appropriate switches allows you to override these restrictions and force delete files. Unlocker and Handle can also help release file locks. Just be cautious when force deleting system files.
With the steps outlined in this guide, you should be able to successfully force delete any stuck or locked files in Windows. Just remember to first try closing any applications that may be accessing the file. Let us know if you have any other file deletion tips!