Smart Systems for Home

Plain how-to guides for smart home, security and home Wi-Fi

Remove Locked File in Windows Command Prompt

· 5 min read · Home Tech

In this article
  1. What You'll Need
  2. Step-by-Step
  3. Common Mistakes
  4. Tips
  5. Troubleshooting
  6. Frequently Asked Questions

This guide shows you how to delete a file that Windows reports as locked or in use, using only the Command Prompt. You'll learn several methods that force-close the file handle or schedule deletion on reboot, so you can finally get rid of that stubborn file.

What You'll Need

  • Windows 10 or 11 (methods also work on Windows 7/8 with minor differences)
  • Administrator account access (required for most methods)
  • The full path to the locked file (e.g., C:\Users\YourName\Documents\file.docx)

Step-by-Step

Method 1: Use the built-in del command (if it works)

  1. Open Command Prompt as administrator: press Win, type cmd, right-click Command Prompt, and select Run as administrator.
  2. Navigate to the folder containing the file (optional but helpful): cd /d "C:\path\to\folder"
  3. Try deleting: del "filename.ext" (replace with your file name). If it deletes, you're done.
  4. If you get "Access is denied" or "The file is in use by another program," proceed to Method 2.

Method 2: Find and kill the process that has the file open

  1. In the same admin Command Prompt, run: openfiles /query /fo csv (requires you to enable the "Maintain Objects List" global flag first; see Troubleshooting).
  2. If that doesn't list your file, use handle.exe from Sysinternals (download it first, then run handle.exe "filename.ext" from its folder).
  3. Note the process name and PID (Process ID) that holds the file.
  4. Kill that process: taskkill /PID /F (replace with the actual number).
  5. Now try deleting the file again with del.

Method 3: Schedule deletion on next reboot

  1. Open Command Prompt as administrator.
  2. Type: MoveFile "C:\path\to\file.ext" "" — this is from Sysinternals MoveFile utility (download it first). The empty quotes tell Windows to delete on reboot.
  3. Reboot your PC. The file will be deleted during startup before any other process can lock it.

Method 4: Use the rmdir trick for folders

  1. If the locked item is a folder, try: rmdir /s /q "C:\path\to\folder" — this removes the folder and all contents recursively.
  2. If it fails, rename the folder first: ren "C:\path\to\folder" "folder_old" and then delete the renamed folder.

Method 5: Use PowerShell's Remove-Item (alternative)

  1. Open PowerShell as administrator (right-click Start > Windows PowerShell (Admin)).
  2. Run: Remove-Item -Path "C:\path\to\file.ext" -Force
  3. If that fails, try: Remove-Item -Path "C:\path\to\file.ext" -Force -ErrorAction SilentlyContinue

Common Mistakes

  • Not running as administrator — many delete attempts fail because the Command Prompt lacks elevated privileges. Always run as admin.
  • Typing the wrong path — spaces in paths must be enclosed in double quotes. Use dir to confirm the exact file name.
  • Killing the wrong process — killing System processes (PID 4) can crash Windows. Only kill processes you recognize (like a text editor or media player).
  • Using del on a folder — del only works on files. Use rmdir for folders.
  • Forgetting to close the file in the program first — sometimes the simplest fix is to close the app that has the file open.

Tips

  • Use dir /x to see short names (8.3 format) for files with long names — these are easier to type.
  • If you use handle.exe, add its folder to your PATH so you can run it from any directory.
  • Always try del first — it's the fastest and safest method.
  • For a file locked by a background service, you may need to stop the service first: net stop "ServiceName" (run as admin).
  • If you're comfortable with GUI tools, use Process Explorer (Sysinternals) to search for the file handle and close it visually.

Troubleshooting

"The process cannot access the file because it is being used by another process."

This is the classic lock message. Use Method 2 to identify the process. If openfiles doesn't show anything, enable the global flag first:

  1. Run openfiles /local on (admin required).
  2. Restart your computer.
  3. Then run openfiles /query again.

"Access is denied" even when running as admin

This often means the file has restrictive permissions. Take ownership first:

  1. Run: takeown /f "C:\full\path\to\file"
  2. Then: icacls "C:\full\path\to\file" /grant administrators:F
  3. Now try deleting again.

File is locked by a system process (like svchost.exe)

Do not kill system processes blindly. Instead, reboot into Safe Mode (press F8 during boot) and delete the file there, or use Method 3 (MoveFile on reboot).

MoveFile doesn't work (file still there after reboot)

Make sure you ran MoveFile as administrator and that the file isn't protected by Windows (e.g., system files). For system files, you may need to take ownership and use a different approach like sfc /scannow to repair, not delete.

File is locked by a virus or malware

Do not attempt to delete it manually. Run a full antivirus scan first. If the file persists, boot from a rescue disk (like Windows Defender Offline) and delete from there.

Frequently Asked Questions

Can I delete a locked file without any third-party tools?

Yes, you can use the built-in del command if the file isn't actually locked, or use openfiles to find and kill the process. For stubborn cases, the built-in rd command on an empty folder might work. But for truly locked files, you'll likely need a utility like MoveFile or handle.exe.

Is it safe to kill a process that has the file open?

Only if you know what the process is. Killing a process like Notepad or a game is fine. Killing system processes (e.g., svchost.exe, explorer.exe) can cause instability. Always check the process name and PID before killing.

What does "file is in use by another program" mean?

It means a running program has opened the file and hasn't released it. This can happen if the program crashed, or if it's a background service. The lock prevents other programs from modifying or deleting the file.

Why can't I delete a file if I'm the administrator?

Windows enforces file locks even for administrators. The lock is at the kernel level, so admin rights don't override it. You must either close the program or force-close the handle.

Is there a way to delete a file on reboot without third-party tools?

Yes, you can use the PendingFileRenameOperations registry key, but it's risky and complex. A safer built-in alternative is to boot into Safe Mode and delete the file there, as most non-system processes won't be running.

What if the file is locked by a service that restarts automatically?

Stop the service first using net stop "ServiceName" (admin), then delete the file. After deletion, you can restart the service with net start "ServiceName". If the service restarts immediately, set it to Manual or Disabled temporarily.