Many times Windows users face errors like “File in Use” or “Access Denied” when they try to delete a file. If the file is locked, it is not possible to delete it through normal methods. In this case, deleting the file using Command Prompt (CMD) can be an effective solution. In this article, we will explain in detail how you can delete locked files via CMD.
Why use CMD?
Windows Explorer does not allow the deletion of locked files.
You can use the Force Delete command through CMD.
Locked files are often affected by viruses or malware, which can be removed from CMD.
It is easier to work with Administrator Privileges in the system.
Ways to delete a locked file using CMD in Windows
1. Close the process using Windows Explorer
Sometimes a file gets locked due to a running process. To close it:
Open Task Manager by pressing Ctrl + Shift + Esc.
Go to the “Processes” tab.
Find the application or process related to that file.
Right-click on it and click “End Task“.
Now delete the file using CMD.
2. Ways to delete a locked file using CMD
Method 1: Use the Del command
Press Windows + R, type “cmd” and press Enter.
Now type the below command in CMD:
del /f /q “C:\Path\to\lockedfile.txt”
Here:
/f = Force delete
/q = Delete without Confirm message
Replace “C:\Path\to\lockedfile.txt” with the actual path of your file.
Method 2: Use the ATTRIB command
Many times a file has Read-Only, Hidden, or System Attributes set on it, making it difficult to delete. To remove it:
attrib -r -s -h “C:\Path\to\lockedfile.txt”
del “C:\Path\to\lockedfile.txt”
This command removes all privileges of the file, allowing it to be deleted.
Method 3: Delete with the TASKKILL command
If the file is being used by a program, first kill the relevant process:
taskkill /f /im notepad.exe
Now delete the file from CMD:
del “C:\Path\to\lockedfile.txt”
Method 4: Delete the file in Safe Mode
Press Windows + R, type “msconfig” and press Enter.
Go to the “Boot” tab and enable “Safe Mode“.
Now restart Windows.
Login to Safe Mode and open CMD.
Delete the file using the del command.
3. Deleting a locked file with PowerShell
If CMD doesn’t work, use PowerShell.
Remove-Item “C:\Path\to\lockedfile.txt” -Force
Frequently Asked Questions (FAQ)
1. Can a file deleted from CMD be recovered?
No, if you use the del /f /q or rmdir /s /q command, it becomes difficult to recover the file. However, some data recovery software can help.
2. Can a locked file be deleted in Windows without CMD?
Yes, you can use third-party tools like Unlocker, File Assassin, etc.
3. Will these methods work on Windows 11 as well?
Yes, all these methods work effectively on Windows 7, 8, 10, and 11.
4. Is it safe to delete system files from CMD?
No, deleting system files may cause Windows crashes or boot problems. Be careful.
Conclusion
Deleting locked files in Windows via CMD is an easy and effective method. If a file is not getting deleted, you can use del, attrib, task kill, and PowerShell commands. By trying these methods, you can easily manage your system files.