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.
Footnotes & References
1: Even then, the Windows API should prevent it — but once again, it can happen anyway.