How do I delete a folder which is nested quite deep and avoid "File name too long"?

70

37

Eclipse created a temp folder in one of the directories which is nested quite deep, e.g.

dir1\dir1\dir1\dir1\...

I am unable to delete this folder in Windows via Explorer, the del or rmdir commands, nor the Cygwin 'rm' command. How should I remove this very long folder?

It just keeps saying "File name too long..."

user39186

Posted 2011-03-11T04:30:44.730

Reputation: 1 053

possible duplicate of Can't delete folder. Infinitely looped folders created within each other

– bwDraco – 2011-09-06T15:35:56.523

Answers

105

If you are like me and don't like to install additional software to fix a problem like this, I'd go with XQYZ's suggestion and use robocopy to solve the problem. (In my case the problem was created by robocopy in the first place, by copying a directory which had recursive junction points in it without supplying /XJ to robocopy).

To delete the directory tree starting at c:\subdir\more\offending_dir:

The total step-by-step-process is as simple as this:

  1. cd c:\subdir\more to cd into its parent directory.
  2. mkdir empty to create an empty directory.
  3. robocopy empty offending_dir /mir to mirror the empty directory into the offending one.
  4. After some waiting you're done! Finish it up with:
  5. rmdir offending_dir to get rid of the now empty offending directory and
  6. rmdir empty to get rid of your intermediate empty directory.

jofafrazze

Posted 2011-03-11T04:30:44.730

Reputation: 1 151

this is clearly the best and most reasonable answer, much better than a bespoke recursive batch script – monastic-panic – 2014-10-26T17:34:08.387

I tried this solution but I get a dialog box asking for confirmation because there are nested hierarchies in the folder. Do I need some switch in that case? I need this to run unattended. – julealgon – 2015-04-10T19:04:46.180

you may need to run the command with administrator privileges. – Abdessamad Idrissi – 2015-06-14T18:12:35.630

I had this recursive folder problem created by netbeans after creating a java project with messed up libs folder (for a reason i still don't know), the procedure described in this answer worked perfectly! – Alexander Fradiani – 2015-11-02T21:01:15.377

I would add too that once you're confident the command is working that the command be canceled and then redirected to NUL to speed up the process not having to bother with standard output printing, ie robocopy empty offending_dir /mir > NUL – jxramos – 2016-01-07T20:08:29.580

2Excellent suggestion. My problem was also created by robocopy, and as you described the robocopy fix worked for me. – Nathan Garabedian – 2011-12-03T00:18:48.800

3I also made a mess with robocopy and junction points; thanks for showing me how to use it to clean up the mess! – Mr.Wizard – 2012-06-10T10:55:27.130

my folders were not created by robocopy but it removed them perfectly – Sasha – 2014-01-09T00:25:39.223

11Node package manager (NPM) caused this problem for me. There were so many nested packages for some reason. – David Sherret – 2014-04-03T21:18:05.757

39

This is actually quite simple to fix. Say that the directory structure is as such:

C:\Dir1\Dir1\Dir1\Dir1…

To fix it, just rename each folder to a one-character folder-name until it is no longer too long to delete:

  1. Rename C:\Dir1 to C:\D
  2. Navigate to C:\D\
  3. Rename C:\D\Dir1 to C:\D\D
  4. Navigate to C:\D\D\
  5. Goto 1 until total length of path is <260

Here’s a batch file to automate the process (this simple version is best for simple directories like the one described in the question, especially for disposable ones). Pass it the highest folder possible (eg C:\Dir1 for C:\Dir1\Dir1\Dir1… or C:\Users\Bob\Desktop\New Folderfor C:\Users\Bob\Desktop\New Folder\abcdefghi…)

@echo off
if not (%1)==() cd %1
for /D %%i in (*) do if not %%i==_ ren "%%i" _
pushd _ 
%0 
popd

Technical Explanation

The other proposed solutions are backwards; you can’t fix it by working your way from the innermost directory outward, you need to go in the other direction.

When you try to access a directory, you do so using its absolute path whether explicitly or not, which includes everything that came before it. Therefore, for a directory structure like C:\Dir1\Dir1\Dir1\Dir1, the length of the path to the innermost Dir1 is 22. However the length of the path to the outermost Dir1 is only 7, and therefore is still accessible regardless of its contents (in the context of a given directory’s path, the file-system has no knowledge of what it contains or the effect it has on the total path length of its child directories; only its ancestor directories—you cannot rename a directory if the total path-length will be too long).

Therefore, when you encounter a path that is too long, what you need to do is to go to the highest level possible and rename it to a one-character name and repeat for each level therein. Each time you do so, the total length of the path shortens by the difference between the old name and new name.

The opposite is true as well. You cannot create a path that is greater than the maximum supported length (on DOS and Windows, MAX_PATH = 260). However, you can rename directories, working from the innermost outward, to a longer name. The result is that deeper folders whose absolute path is >260 will be inaccessible. (That does not make them “hidden” or secure, since they are simple enough to get at, so don’t use this method to hide files.)


Interesting Side Note

If you create folders in Windows 7 Explorer, it may seem like Explorer allows you to create subdirectories such that the total length is longer than MAX_PATH, and in effect it is, however it is actually cheating by using “DOS 8.3 filenames”. You can see this by creating a tree such as the following:

C:\abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789
   \abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789
    \abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789
     \abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789
      \abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789
       \abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789
        \abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789
         \abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789
          \abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789
           \abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789
            \abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789\

It is 696 characters long, which of course is much longer than 260. Further, if you navigate to the innermost subdirectory in Explorer, it shows it as expected in the address bar when it is not in focus, but when you click in the address bar, it changes the path to C:\ABCDEF~1\ABCDEF~1\ABCDEF~1\ABCDEF~1\ABCDEF~1\ABCDEF~1\ABCDEF~1\ABCDEF~1\ABCDEF~1\ABCDEF~1\ABCDEF~1\, which is only 102 characters long.

In XP, it does not do this, instead it steadfastly refuses to create a longer path than is supported.

What would really be interesting is to find out how Windows 7 Explorer handles “too-long paths” when the NtfsDisable8dot3NameCreation option is set.

Synetech

Posted 2011-03-11T04:30:44.730

Reputation: 63 242

3

It is possible to create a path longer than MAX_PATH, as explained here. Unfortunately, \\?\ doesn't work with rmdir.

– user1686 – 2011-03-11T18:35:05.350

@grawity, yes, but that is because it works under the same principal: a short path is renamed to a longer one; that just does it dynamically by expanding a variable as opposed to manually renaming it to la onger one. It is not possible to create a directory whose absolute path is too long when the creation command has enough information to determine the total length. – Synetech – 2011-03-11T19:31:59.503

3@Synetech: No, it works differently. Paths like \\?\C:\dir\dir\dir\dir literally bypass MAX_PATH; there are no "variables" involved. (But like I said, it does not work with rmdir or other cmd.exe builtins for some reason.) – user1686 – 2011-03-11T19:35:00.493

eg, try running md C:\01234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789

It won’t work because the file-system has sufficient information to determine that the total path length would be 263 characters, so it fails. – Synetech – 2011-03-11T19:35:19.877

2(Also, don't confuse the path length with component length. You cannot have a single directory with a name over 255 characters; however, you can have a path much longer than that.) – user1686 – 2011-03-11T19:37:42.893

\\?\ is a variable–of sorts: the "\?" prefix may be expanded to a longer string by the system at run time It can get dynamically expanded to a longer string. The raw path prefix is only 4 characters: \\?, but it gets expanded to a longer one, similar to environment variables: %systemroot% is 12 chars, but usually gets expanded to C:\Windows (which is actually 2 *less). – Synetech – 2011-03-11T19:39:57.653

@Synetech: I see. Nevertheless, it still uses a much larger limit (around 32 kilobytes) if the prefix is present. (I'm guessing the expansion results in something like \\?\C:\boot.ini\Device\HarddiskVolume7\boot.ini.) – user1686 – 2011-03-11T19:44:31.223

True, but that only applies to some, not all, of the internal API functions. Most tools and commands use the hard-coded limit of 260. I just checked, and even in VisualStudio 2010, it is still set to 260 in WinDef.h: #define MAX_PATH 260

Oh, and the “dots” add to the raw length of the path, but the resulting path will be shorter since the dots lead to either the same directory or the parent. The point is that there is a difference between the raw path and the resolved path. – Synetech – 2011-03-11T19:46:20.850

@Synetech: Right, MAX_PATH is 260, because the page is not talking about increasing it but bypassing it. As I understand it, it works with the Unicode version of any Windows API; e.g. CreateFileW(). (Except for When using an API to create a directory, the specified path cannot be so long that you cannot append an 8.3 file name (that is, the directory name cannot exceed MAX_PATH minus 12) from the same page, which is why your md example would not work.) – user1686 – 2011-03-11T19:50:33.240

Ahhhhhh, Unicode made things so much more annoying. Backwards compatibility is frustrating huh. :-D – Synetech – 2011-03-11T20:11:01.783

@Synetech: Speaking a non-Latin-1 language and having a matching last name, I would say Unicode made things less annoying. (Aside from having two versions of every API, of course. Backwards compatibility is hell, as The Old New Thing reminds every so often.)

– user1686 – 2011-03-11T20:14:05.607

For users, yes. I meant for developers and Windows engineers, and trying to cram twice as much data into the same amount of space (well, the same amount which now takes up twice the space). Then you’ve got 64-bit, and that almost doubles the size of programs and such as well. So now both *strings and numbers* take up about twice as much space as they used to, so it’s no wonder we need more RAM. (I’d like to see 4K demos today!) :-D – Synetech – 2011-03-11T20:19:36.970

Apparently the OP changed their mind and decided that they don’t like my solution anymore, but there’s no explanation of why. Hit-and-run down-voters are lame. *roll* – Synetech – 2011-09-10T16:59:29.270

Oops, nevermind; I misread the timeline. It was not the OP that down-voted, it was someone else (I’m looking at you music2myear; for the record, I did not down-vote your answer, someone else did). – Synetech – 2011-09-11T21:01:39.783

Nice work. I got this same idea from someone else's suggestion to use subst to shorten a path. – Bobson – 2011-11-21T02:58:32.160

17

You can shorten the path by using subst to create a virtual drive:

C:\>subst Z: "C:\TEMP\dir1\dir1\dir1\dir1\dir1\dir1\dir1\dir1\dir1\dir1\dir1\dir1\dir1"

Change into the virtual drive:

cd Z:

Now you can delete the files:

del *.*

Remove the virtual drive:

cd C:\TEMP
subst Z: /d

Remove the directory:

rd /s dir1

Matthew Simoneau

Posted 2011-03-11T04:30:44.730

Reputation: 619

Nope; that first command won’t work if the directory is too long; it will return the error Invalid parameter. – Synetech – 2011-09-08T05:30:22.863

2@Synetech, sure, but if you subst just C:\TEMP\dir1\dir1\dir1, then it will shorten part of it, thus allowing you to get in. It is just like your suggestion of renaming, but with mapping instead. ;) – Bobson – 2011-11-21T02:59:41.517

@Bobson, okay you’re right; +1 for both of you. :-) – Synetech – 2011-11-21T21:04:08.300

10

I wrote a small C# app to help me delete a similar very deep structure generated by a careless usage of Robocopy and a backup from Homeserver; by default Robocopy treats joint points as regular folders... :-( You might end up with a big mess without noticing it.

The tool is available at CodePlex with source files, for anyone to use.

http://deepremove.codeplex.com

JPJofre

Posted 2011-03-11T04:30:44.730

Reputation: 101

WORKS!!! This answer must be marked as working! The software works like butter.. solved my prob in few second!! Thank you! – Rafique Mohammed – 2015-02-02T11:26:37.537

7

Some time ago I created a small, self-contained utility executable called DeleteFiles that you can use to perform this task easily.

Using this self-contained, utility you can simply do:

deletefiles c:\yourfolder\subfolder\*.* -r -f

to delete the entire folder structure. -r recurses the folder hierarchy from the starting directory down, -f deletes any folders that are empty (which will be all of them if you use . as the filespec). DeleteFiles supports paths longer than the Windows MAX_PATH limit so it will work just fine on deeply nested folders.

DeleteFiles is free and open source and you can grab either the binary or source code from GitHub or install directly using Chocolatey

Rick Strahl

Posted 2011-03-11T04:30:44.730

Reputation: 409

Thanks, awesome tool, ++ for putting it in chocolatey ;) Makes it easy to integrate in a CI tool! – Charles Ouellet – 2015-03-27T14:17:25.927

1This did the trick. If you have a really long path, adding > NUL to the end can make the process quicker. – ryscl – 2016-03-17T09:53:22.937

The robocopy solution did not work for me and neither Synetech's solution. DeleteFiles worked for me, but for some reason I had to run it three times for all subfolders to be deleted. In any case, this solved my problem. – Frank – 2017-04-28T02:08:33.403

Re: running DeleteFiles 3 times. I've seen that as well - I believe it's due to some Windows quirks that lock folders with files in them for a short time even once files have been deleted. Multiple passes catch the occasional failure of this issue in subfolders - potentially multi-nested. I see same behavior with Explorer deletes of deep trees. – Rick Strahl – 2017-05-08T23:23:06.457

5

Simple & Easy Now

i was facing this same problem since so long with node_modules which very nested folders. so finally made a script to fix that which can delete folders by shortening paths.

https://github.com/dev-mraj/fdel

npm install fdel -g

fdel ./node_modules

dev.meghraj

Posted 2011-03-11T04:30:44.730

Reputation: 201

I don't know why the designers chose to include every dependency in a structure when they could've made it with a flat structure. So this script was the easiest way for me as I'm already using node.js – user2610529 – 2016-09-11T17:44:18.527

4

While working with Sikuli I got jacked with a Calculator.sikuli recursion loop in the program that made an uncountable amount of "calculator.sikuli.calculator.sikuli" dirs. I could move the tree, but pathname too long to delete.

After trying several solutions with popd loop, Scandisk and getting (perceptibly) nowhere....

I wrote this script to 'go deep' into the recursed dirs(in a dir called 'a'), move them(to a dir called 'b'), then delete the truncated tree, move them back(to 'a'), and repeat:

1)cd D:\a\calculator.sikuli\calculator.sikuli\calculator.sikuli\calculator.sikuli
.............go deeeeeep in         dir *A*
2) move calculator.sikuli ---> D:\b    
.............move the crazy tree to dir *B*    
3) kill D:\a\calculator.sikuli <---KILL(rd)    
.............wipe dir *A*'s tree    
4) move D:\b\calculator.sikuli ---> D:\a\    
.............move the crazy tree back to dir *A*    
REPEAT
  • REM Used to delete infinitely recursed subfolders
  • REM suggest to stop Windows Search service first(services.msc)

Remdirs.bat

D:
cd D:\a\calculator.sikuli\calculator.sikuli\calculator.sikuli\calculator.sikuli
move /-Y calculator.sikuli D:\b
cd D:\b
rd /s/q D:\a\calculator.sikuli
move /-Y calculator.sikuli D:\a
call D:\remdirs2.bat

This is just a call to run the batch file again.

SiloSix

Posted 2011-03-11T04:30:44.730

Reputation: 41

I've spent hours looking into this. This .bat file is like a gift from heaven. You, silo, are an angel. xD – Squish – 2014-10-06T05:32:23.537

2

We had a problem like this at work when eclipse decided to create rubbish on the harddrives. We fixed it by using robocopy's /MIR function to mirror an empty directory into the nested one.

XQYZ

Posted 2011-03-11T04:30:44.730

Reputation: 208

1

Open a command prompt.

Navigate to the folder/directory that contains the highest 'dir1' (we'll assume C:\)

c:\> RD /s dir1

Edit (after comments added):

Other ideas:

MS offers info on how to deal with the problem (lots of ideas to try) here.

There's also this tool (never used it personally) - TooLongPath.

Perhaps write something (since you have Eclipse) that navigates all the way in and then backs out one folder level at a time, deleting as it goes?

Ƭᴇcʜιᴇ007

Posted 2011-03-11T04:30:44.730

Reputation: 103 763

this worked for me on win7! thanks – leoh – 2015-01-28T15:14:57.597

1I get the following 3 errors while using the above command.The directory is not empty The system cannot find the path specified The file name is too long – user39186 – 2011-03-11T04:51:27.940

I tried traversing say 'n' levels deep and tried using the same command, but it doesn't seem to help – user39186 – 2011-03-11T04:52:29.937

1

I would try opening a command prompt and running:

rmdir /s <directory>

If that doesn't work, I'd cd partway into the directory tree and try to delete a subset of the directories -- say the 20 innermost directories -- and then work my way out from there.

Jesse S.

Posted 2011-03-11T04:30:44.730

Reputation: 19

1I tried your suggestion above and it still says "Directory is not empty" when I run the above command several levels deep – user39186 – 2011-03-11T05:03:08.040

1That’s because this method is backwards. ;-) – Synetech – 2011-03-11T06:11:02.973

1

If it is a network folder then just share that directory's parent directory and map it to a drive on your local machine and then delete your folder.

Punnakadu

Posted 2011-03-11T04:30:44.730

Reputation: 11

21966 [main] mv 1288 D:\work\software\cygwin\bin\mv.exe: *** fa tal error - internal error reading the windows environment - too many environment variables? – user39186 – 2011-03-11T05:09:32.607

I tried moving a a sub-folder nested 20 levels deep and got the above error – user39186 – 2011-03-11T05:10:01.623

1

Another solution: go download Total Commander. It's a very useful program, not just because it's long filename aware.

The unregistered version is nagware but fully functional, it will do the job.

Loren Pechtel

Posted 2011-03-11T04:30:44.730

Reputation: 2 234

1

This can be done directly off the command line or in a batch file by constructing a UNC path to the directory you want to delete

so instead of

rmdir /s/q c:\mydirectory

use

rmdir /s/q \\?\c:\myDirectory

UNC-style paths like this can be much longer and bypass the 260-char limit.

Steve Cooper

Posted 2011-03-11T04:30:44.730

Reputation: 165

Doesn't work. The path \\?\C:\temp\wqiyretiuqyertiuyqwteiyrutqwuiyertiqrqweirqyert\wqteriuwqyetriuqwteiryutwiuertiuyqerieerrt\IOQWUE~1\QIWUYE~1\OIUQYW~1\OIUQYW~1\OIUQYW~1\OIUQYW~1\OIUQYW~1\OIUQYW~1\OIUQYW~1\OIUQYW~1\OIUQYW~1\ OIUQYW~1\OIUQYW~1\OIUQYW~1\OIUQYW~1\OIUQYW~1\OIUQYW~1\OIUQYW~1 is too long. Windows 7 64-bit. – Victor – 2016-02-10T12:01:17.780

Does not work for windows 10. Still too long. – BananaAcid – 2016-07-30T04:07:48.140

The \\?\ version worked for me on windows 10! – Peter – 2017-09-26T08:52:23.090

0

When I have this problem I simply rename some of the folder names much shorter, then once the total path is short enough, it'll delete OK. No extra tools needed.

music2myear

Posted 2011-03-11T04:30:44.730

Reputation: 34 957

Yes, but like I said, you have to work from the outside in, otherwise it won’t work. – Synetech – 2011-09-08T05:33:14.373

Of course. I've generally found the longest folder names tend to be the first (in patch folders) or the last. Most of the time, you only need to change one or two folder names to get it to the right length. – music2myear – 2011-09-08T14:31:08.187

Yes, but if you start with the innermost one, it will not work because the ren command will fail with path too long. – Synetech – 2011-09-08T21:09:43.477

1Yes, the scripts provided above are a clever and effective method of handling this problem automatically. It has only happened to me a few times and so I've simply used the manual rename process. To do that I simply start renaming the folder structure wherever I happen to be at in the offending tree, and my experience is the longest folder names appear more often at the beginning or the end of the tree structure. My answer is therefore a valid one, though probably not the strongest or cleverest here. It's not worth a downvote. – music2myear – 2011-09-09T14:00:46.903

> I simply start renaming the folder structure wherever I happen to be at in the offending tree Well, yes, if you are already inside the tree, then you can certainly rename at least that folder (you’ll need to go to its parent); you may be able to rename a subfolder as well, but it may be too long. – Synetech – 2011-09-10T16:57:34.187

0

I had the same problem, except it was created by a recursive Cobian Backup task. I turns out the free Cobian software includes a Deleter application that can easily remove these pesky nested folders super quickly.

It's located under the tools menu.

mrshl

Posted 2011-03-11T04:30:44.730

Reputation: 11

0

I did run into the same issue with a 5000+ directory-deep folder mess that some Java application did and I wrote a program that will help you remove this folder. The whole source code is in this link:

https://gitlab.imanolbarba.net/imanol/DiREKT

It removed the whole thing after a while, but it managed to do the job, I hope it helps people who (as I), run into the same frustrating issue

Imanol Barba Sabariego

Posted 2011-03-11T04:30:44.730

Reputation: 116

-3

Your filesystem may be corrupt. Run chkdsk to see if it repairs anything, then try deleting the folder.

juggler

Posted 2011-03-11T04:30:44.730

Reputation: 1 530

Nope, that’s not the problem. The problem is that the total path length is longer than is supported (MAX_PATH=255). This can happen even with a non-corrupt file-system. – Synetech – 2011-03-11T05:51:03.820

Running chkdsk on the folder gave me the following error. The drive, the path, or the file name is not valid – user39186 – 2011-03-11T05:51:53.133