How to delete directories with path/names too long for normal delete
Windows seems to have a length limit on file names when trying to delete, though it won't prevent those files from being created.
Our build process creates a number of temporary files (many build off of a WSDL) that run afoul of this limit. Our ant script is somehow able to delete them when doing a clean, but sometimes I need to delete the workarea directory (where all the temp files go) without actually doing a full clean from ant.
This is the same errors this question, but the answers there don't really work for me as I'm dealing with a directory, not a file, and I don't always know what specific files or subdirectories are causing the problem. And I'm trying to avoid any manual process (other than triggering a single command) to actually delete them.
If I try deleting the directory from Explorer I get the error
Cannot delete [file name]: The file name you specified is not valid or too long.
Specify a different file name
Trying Remove-Item
in powershell gives the following error:
Remove-Item : The specified path, file name, or both are too long. The fully qualified file name must be less than 260 characters, and the directory name must be less than 248 characters.
At line:1 char:12
+ Remove-Item <<<< -force -Recurse <directory>
Does anyone know of any tools or easy ways to get around this delete error without having to manually find the problem files and move/rename them?
Use the 7-Zip File Manager to delete them.
If you are still having trouble, ensure that you utilize Shift+Delete inside the 7-Zip File Manager. Otherwise, Windows tries to move them to the Recycle Bin (which will fail again).
None of the other (free) answers here worked for me, but I found one on another site:
rimraf <dir>
rimraf is a Node.js package, so you will need to install Node.js which includes npm. Then you can run:
npm install -g rimraf
Then you can run rimraf
from the command line.
I found this solution because npm itself was causing this problem due to the way it nests dependencies.
By the way, rimraf
gets its name from the UNIX command rm -rf
, which recursively deletes files and folders.
There is no need to install any program for solving this issue.
This issue is easily solved using robocopy
, preinstalled since Windows Vista, launched in 2006.
For example, rmdir /S /Q <dir>
has been reported to fail in some cases. There is no need to use 7zip or any other 3rd party tool. Powershell is an overkill. Cygwin may work, but you may not have it installed. So, let's focus on robocopy
The idea is to
- use
robocopy
to copy+updated - from a new empty folder
- to the folder you want to delete, the target.
After executing robocopy
, the target directory would be empty as well.
These instructions are for the command line. Just open the search in Windows, type cmd
and hit Enter.
Let’s say the target for deletion is:
C:\delete\this folder\with a very long name
We proceed as follow:
-
First create an empty directory, f.i.
C:\emptyfolder
.mkdir C:\emptyfolder
-
Copy+update from the empty directory to the target, using the option
/purge
robocopy c:\emptyfolder "C:\delete\this folder\with a very long name" /purge
-
Delete the empty directory. You don't need it anymore.
rmdir c:\emptyfolder
Since there are no files or folders in the source directory (C:\emptyfolder
), it simply deletes the files and folders under the target directory (C:\delete\this folder\with a very long name
) recursively!
-
Final trick: you can avoid writing by hand
C:\delete\this folder\with a very long name
By dragging the folder from an Explorer window and dropping in the Terminal/cmd window.
Be careful: The deleted files will not go to the trash folder! Once deleted, the files cannot be recovered.
(Taken from "Path too long? Use Robocopy" by BVLANGEN)
PS: I realize this answer was here, less didactically. How to delete a file in Windows with a too long filename? [duplicate]
Benoit added:
You may need to go through this process more than once to get rid of all of the files.