Home » Questions » Computers [ Ask a new question ]

How to delete directories with path/names too long for normal delete

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.

Asked by: Guest | Views: 357
Total answers/comments: 5
bert [Entry]

"I believe I've found a way to delete things from cmd. Originally I tried the del command, but that didn't work. Then I remembered rmdir. Doing the following:

rmdir /S /Q <dir>

seems to have worked."
bert [Entry]

"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."
bert [Entry]

"Without installing additional software you can use subst command to temporary create an alias to a long named directory.

e.g. If you want to delete folder C:\Very long directory\that exceed\length limit\blah blah blah\abcde\folder to be deleted you can use the command

subst x: ""C:\Very long directory\that exceed\length limit\blah blah blah\abcde""

and then you can delete X:\folder to be deleted in Windows Explorer or in command prompt easily. To remove the temporary drive letter alias use the command

subst x: /d"
"Without installing additional software you can use subst command to temporary create an alias to a long named directory.

e.g. If you want to delete folder C:\Very long directory\that exceed\length limit\blah blah blah\abcde\folder to be deleted you can use the command

subst x: ""C:\Very long directory\that exceed\length limit\blah blah blah\abcde""

and then you can delete X:\folder to be deleted in Windows Explorer or in command prompt easily. To remove the temporary drive letter alias use the command

subst x: /d"
bert [Entry]

Cygwin's rm -rf works well on long paths!
bert [Entry]

"Just drop from Powershell into command by running below:

Cmd /C ""rmdir /S /Q <dir>"""