Sometimes, it might be important.
In a previous article, I used a folder that I could not delete as an example of a problem not worth fixing. One could waste a lot of time trying to fix something that was ultimately inconsequential.
But what if it does matter?
I’ll give in to the pressure of everyone who wanted to suggest solutions, and… suggest solutions.
Become a Patron of Ask Leo! and go ad-free!
Folders that can't be deleted
The most common causes of folders that can’t be deleted include being in use by another program, the length of the entire path to a file in the folder, and invalid characters in one or more file names. Closing programs, rebooting, and renaming or moving the offending files are common solutions.
Causes of undeletable folders
In my experience, there are three common causes of undeletable folders or files.
The file is in use. This is often resolved by closing the program that’s using the file or folder. In case you can’t determine which program that is, a reboot also frequently clears it up. You can also use Process Explorer to determine what program has the file open. See How Can I Find Out Who Is Using a “File in Use”? for more details.
The full path to the file is too long. While a file might have a simple name, like “example.txt”, depending on what you’re doing, the full path to the file comes into play. For example, if example.txt is in my personal documents folder, the full path is something like:
While the file name — example.txt — is only 11 characters long, the full path has a length of 35 characters.
Depending on the Windows API being used by whatever tool you might use, the maximum length of a path is either 260 characters or approximately 32,767 characters. While running into the latter is rare, the former is not uncommon. If a path is created that ends up being longer than 260 characters, it can be undeletable using some tools.
The file name has an invalid character. This often happens either because of disk corruption or because some software on the machine managed to insert an invalid character into the file name. An example of a file name with an invalid character:
The character “>” is not allowed in a file name. In theory, there should be no way to create a file with an invalid character in its name, but for a variety of reasons, experience once again shows it can happen, typically with non-Microsoft tools or tools that have been ported from other operating systems with different restrictions.1
Solutions: start by deleting what you can
When I’m faced with this situation, there are several things I try if closing programs or rebooting doesn’t help. Almost all of them are time-consuming — hence my previous post on why it’s not always worth the effort to solve every problem.
To begin, I start with an admin Command Prompt. If “C:\foldername” is the folder I’m having problems with, I enter:
CD /d C:\foldername
RD /s .
“CD /d” says “Make the following drive and folder the ‘current’ drive and folder.”
“RD /s .” says “Delete the current folder and everything in it.”
This will delete everything that’s deletable. What’s left will only be the files and folders with problems. When you’re experiencing this problem, you’ll likely get several error messages about files that could not be deleted.
Note that if everything is deleted without a problem, you’ll still get an error. You can’t delete the current folder. If the result is that the current folder is empty, then I’d close Command Prompt and delete the now-empty folder in Windows File Explorer.
Attacking the symptoms
For every file that remains in the undeletable folder tree, back in Command Prompt I would change directory to the folder containing the file:
CD /D c:\foldername\subfolder1\subfolder2\…\subfolderX\
I would then try renaming the file to something simple, like “t.t”.
REN example.txt t.t
(If the current file name is more complex than “example.txt”, I’d probably type the first character or two of the file name and then press the TAB key to let Command Prompt auto-complete the name.) If the rename works, I’d then delete the renamed file.
If any of these steps fail, I’d try moving the file to the root of its drive:
MOVE example.txt \
Thus if the file were “C:\foldername\subfolder1\subfolder2\…\subfolderX\example.txt”, after the move it would be “C:\example.txt”. If the move was successful, I’d then delete the file.
Once I’d done this for all the problematic files (often there’s only one), I’d then try to delete the original folder once again.
More obscure techniques
Without going into extensive details, here are a few additional techniques I might consider.
- Booting from a Linux live USB and deleting the folder using Linux tools.
- Using the SUBST command to create a virtual disk drive out of one of the subfolders. For example, if if the file were “c:\foldername\subfolder1\subfolder2\…\subfolderX\example.txt”, then using SUBST to map “D:” to “C:\foldername\subfolder1” would mean that “D:\subfolder2\…\subfolderX\example.txt” would be a shorter path and thus possibly deletable.
- Using PowerShell instead of Command Prompt for some of the procedures listed in the previous section.
- Using the Windows Subsystem for Linux and a Linux command shell to try the techniques listed in the previous section.
- Using SysInternals tools PendMoves and MoveFile, which are designed to delete or move files in use at the next reboot and may resolve other issues as well.
- Third-party tools.
I expect readers to have additional ideas, so do check out the comments as well.
Since this can sometimes be time-consuming to research and repair, make sure it’s worth it first. If it is, you now have a collection of techniques to try to delete the offending file or folder.
Subscribe to Confident Computing! Less frustration and more confidence, solutions, answers, and tips in your inbox every week.
Download (right-click, Save-As) (Duration: 11:06 — 15.3MB)
Footnotes & References
1: Even then, the Windows API should prevent it — but once again, it can happen anyway.
6 comments on “How Do I Delete a Folder I Can’t Delete”
If confronted with a file with an invalid filename, the first thing I’d try is a wildcard characters.
Ferinsince, in the case of “exam>ple.txt”, I’d try first “DEL exam?ple.txt” and then “DEL exam*”.
Only if those failed would I contemplate stronger measures, such as trying to delete the file under Linux.
Uh… I have to add: Before I’d issue either one of the “DEL” commands I listed in my previous message, I’d first run the pattern through “DIR,” to make sure I’m not about to delete more than I intend! It might well be a shame if I try to delete exam>ple.txt by using “DEL exam*,” only to unexpectedly find that I’ve deleted examination.txt as well! (Ouch!)
I’ve used Unlocker to remove files and folders that can’t be normally deleted. I’ve also used moveonboot, but Unlocker seems to do a better job.
Choose the custom installation as the default will install unwanted programs, not spam but programs ypou probably don’t need.
Yes, I would try Unlocker first: it places a convenient command in the file or folder’s context menu, and when run shows what programs, if any, are locking the file.
If this fails, I would try the file shredder in my Utlities suite.
I would then run checkdisk in case the file cannot be deleted due to corruption.
A virus scan may succeed in removing a file protected by malware.
Files in an undeletable folder can be deleted in blocks until the problem file(s) are found.
I try Unlocker first and last. If Unlocker doesn’t delete it, I don’t waste any more time on it.
To delete an invalid file name, use the prefix \\?\ before the full path.