428

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?

djsmiley2kStaysInside
  • 6,643
  • 2
  • 31
  • 43
Herms
  • 9,404
  • 9
  • 35
  • 36
  • 2
    I found the solution just before posting this question, but since others may run into this I decided to post it so that they could find the solution I ran across. – Herms Dec 03 '09 at 19:24
  • Oh I know, but generally when I do things like this I leave the question "unanswered" for a while to see if anyone else has a better solution. – Herms Dec 03 '09 at 20:42
  • By __Far__ the simplest solution: https://www.youtube.com/watch?v=qQTyTprFAOg – TaW Nov 28 '15 at 21:54
  • using `Ubuntu` or any other `Linux Bootable CD` we can boot our computer to `Ubuntu` or any other OS for temporary.then we can select the folder and we can delete it easily. – Kelum Nov 29 '15 at 04:04
  • 1
    This older question has some interesting answers too: http://superuser.com/questions/45697/how-to-delete-a-file-in-windows-with-a-too-long-filename/392431#392431 – AdamV Dec 03 '15 at 11:01
  • Ya, the simplest solution will be install 7-zip and compress it and don't forget to check the box 'Delete after compression' and then delete the zip file also. Thanks @Dentrasi. – Avishek Nov 29 '16 at 17:32
  • 2
    Actually, is there anybody who knows *why* Windows made such a restriction after all? It seems to make little sense since a lot of third-party tools can just circumvent this restriction with ease, so it certainly doesn't seem to be something inherent with the filesystem or whatnot. Seems to only add difficulty for the user, what for? – xji Jan 12 '17 at 19:10
  • If you happen to have IntelliJ installed, deleting the file/folder from there will work. I haven't tried any other code editors. (PS, I cannot add this as an answer due to the Protected status.) – Tormod Haugene Jan 30 '17 at 10:04
  • it is 2017, and it seems M$ don't want developer use their OS, If you are NodeJS developer, NPM may create a lot of nested folder. And you can not delete at all. – vanduc1102 Mar 29 '17 at 12:44
  • https://superuser.com/a/1263183/439537 – Andrew Oct 28 '17 at 00:37

24 Answers24

496

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).

Run5k
  • 15,723
  • 24
  • 49
  • 63
Dentrasi
  • 11,155
  • 4
  • 27
  • 28
  • I tried a bunch of explorer replacements, but all of them just seemed to pass delete requests to the default explorer mechanism, so I gave up. Never thought to use 7zip as an explorer replacement though. I'll have to give it a try. – Herms Dec 03 '09 at 20:41
  • 8
    Thanks - this got rid of a dir tree that went a bit recursive and was too deep for del/rmdir/rm/explorer to remove – Martin Beckett Aug 24 '10 at 15:47
  • 72
    worked great using shift+Del - 7zip couldn't send long-pathed files to the recycle bin.... – cori Mar 02 '11 at 03:44
  • 70
    +1 7zip. You have to navigate to the folder, and THEN use `shift + Del` like @cori said. If you select the `Delete` option from the context menu, it WILl fail there. `shift + Del` it! – Urda Jun 20 '12 at 21:25
  • @Urda I'm getting [this error message (Data error - cyclic redundancy check)](http://i.stack.imgur.com/AOaPm.png) when shift-deleting a long-pathnamed directory with 7-zip. Have you or anyone else encountered it? – galacticninja Mar 26 '13 at 09:16
  • 2
    In addition to `SHIFT + DEL`, 7-Zip provides the `Menu Bar/View/Flat View` option which in my case was able to list the directories of a supposedly recursive path when `cmd` or Explorer didn't show anything. – dgs Nov 21 '13 at 10:22
  • 4
    7zip just got awesomer – Nahn May 06 '14 at 07:21
  • 8
    7zip didn't work for win7 64, so linux did. – Erik Friesen May 15 '14 at 20:09
  • 2
    6 years later, this saved me =D – thclpr Jul 10 '14 at 13:37
  • 17
    I upvote this because with 7zip you can delete also delete a folder wich contains long nested folders inside. Had my problem with a node_module folder that contained crap tons of nested folder and I wasn't able to remove it. 7zip fixed from the base folder flawlessly. +1 – Gruber Oct 13 '14 at 23:33
  • 1
    You can use winrar for this too.. just right click the folder and select Add to Archive. Then in Archiving Options section, select Delete files after archiving ... done.. !!!! – tarzanbappa Mar 25 '15 at 11:19
  • 2
    I got "directory not empty" when trying to delete, but I just kept repeating, eventually, I got there and the folder is finally gone. – Malyngo Aug 31 '15 at 14:28
  • Didn't work for me with 10's k's of recursive folders. Only rimraf worked. – Julian Knight Sep 11 '15 at 16:40
  • 1
    It amazes me how well 7-zip is programmed. I am using an apparently old version that has been released back in 2010 and could not get a single issue with it. And among other things, I use it to cleanup virus-infected flash disks because its file browser is really good! And now we have it to be able to delete long-path files! Oh, my! To whomever is behind 7-zip, thanks tremendously! – Ghasan Oct 30 '15 at 18:41
  • 4
    Didn't work for me on win7 (with shift-del) - Get the error 'the directory is not empty' – UpTheCreek Jan 21 '16 at 13:44
  • 1
    In case 7zip isn't an option for Windows 10 x64 users, PeaZip can do it too (and is a very goo open-source program of course). – Erick Petrucelli Feb 26 '16 at 21:35
  • This worked like a charm anb should be bundled with nodejs ;-) – hoeni Mar 11 '16 at 10:16
  • Windows 10 x64 didn't work for me. I will try this elsewhere, since I have 7Zip everywhere. – WernerCD Apr 03 '16 at 01:39
  • I'm using 7zip v15.14 (64bit)'s File Manager and I could actually RENAME the files from it! Very important you need to use: SHIFT+Delete in 7zip to delete directly, so that 7zip does not send them to the Recycle Bin! – Wasted_Coder Apr 29 '16 at 06:15
  • @UpTheCreek, when you get "Dir not empty" go down into the next subfolder (press ENTER) and press SHIFT+DEL (the topmost file/folder should be automatically selected when you go down). It will either successfully delete the file or show the same error. Keep going down until you have success then go back to the top and try to shift+delete the tree again. Usually takes about 5 or 6 levels before it works, sometimes less, sometimes more. – O.M.Y. Jun 25 '16 at 09:05
  • win10 x64 - total commander do the trick with shift+del – gsziszi Aug 11 '16 at 09:01
  • 7zip didn't work for win7 64 – CodeTweetie Feb 15 '18 at 12:32
  • Funny that Microsoft Windows can't take care of the files in file system but 7zip can! – xbmono Apr 17 '18 at 23:16
  • I think this is the only write answer. Nothing else worked for me. Just rename it with 7-zip manager and delted it. thank you so much – Arundev Jul 12 '18 at 14:59
  • I can't believe this is the best answer. How do we delete files from Windows USING WINDOWS??? – pabrams Aug 16 '19 at 21:11
  • 7zip renames files with too-long-names also. (There's another q&a somewhere for that BTW.) – davidbak Apr 18 '20 at 03:00
  • I just tried the rename hoping it would work after photorec 'recovered' a pdf and gave it a too-long name from the metadata. 7zip gave me an error trying to do the rename "The filename, directory name, or volume label syntax is incorrect." – Nick J Jul 14 '21 at 14:57
  • A possible corner case I just encountered with 7-Zip: This time I got an error when trying to delete the root folder after clearing what I could with other tools. I had to drill down to the furthest folder, which still had undeletable files in it (we were probably several undeletable folders deep by then) and deleted my way back up the tree, one folder at a time, but of course once I got back into normal length range, I could delete the root folder normally. IIRC the error was about a subfolder (the deepest one) still having files in it, so it seems it won't work recursively in this case. – Rook Jul 31 '22 at 08:23
319

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.

jordanbtucker
  • 3,562
  • 2
  • 17
  • 15
206

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

  1. use robocopy to copy+updated
  2. from a new empty folder
  3. 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:

  1. First create an empty directory, f.i. C:\emptyfolder.

    mkdir C:\emptyfolder
    
  2. 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
    
  3. 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.

hectorpal
  • 2,453
  • 1
  • 15
  • 14
  • 20
    Thank-you good sir!! This should be the accepted answer as all the others either _didn't work_ or required a _3rd party tool_. :) – Deleted Dec 23 '16 at 00:44
  • This works perfectly...no need to install anything (as far as I know) and it's quick. This should be the accepted answer! – ksumarine Jan 06 '17 at 17:01
  • 1
    This keeps some (or all) of the directory tree under "c:\deletefolder" (target dir). Anyway, deleted everything in a blink for me (guess the files were small) and I just had to manually "c:\deletefolder", the child dirs got removed without an issue. Worked like a charm. +1, should be the accepted answer! :) – rld. Jan 15 '17 at 13:45
  • @rld, but did "deletefolder" exist before? Otherwise, I think it should be empty if it was a new directory. Otherwise it's a mess. I could add a last step for "rmdir deletefolder". – hectorpal Jan 16 '17 at 17:11
  • @hectorpal 1. I had a tree with remains of a Windows C drive backup from another machine, that I managed to delete except for the very long parts, ie. "%USERPROFILE%/foo/very_long_dirname/vldn2/vldn3/(...)/very_long_filename". 2. `cd ~; mkdir empty; robocopy empty foo /purge` 3. Was left with ~/foo/maybe_something_deeper_too_cant_remember/ and didn't really take time to investigate further because I was already pissed at M$ for disrespecting me and wasting my time, but a simple on ~/foo in explorer removed it successfully, after failing endlessly (even with ownership) until robocopy. – rld. Jan 18 '17 at 18:22
  • I might add, "foo" being "deletefolder" and "empty" being "emptyfolder" – rld. Jan 18 '17 at 18:29
  • @rld. It seems the robocopy failed, but it should have complaint. It would have been interesting to run robocopy /purge again. – hectorpal Jan 27 '17 at 15:14
  • THIS IS THE BEST SOLUTION, IT WORKS PERFECTLY, NONE OF THE REST WORKED FOR ME! THIS ONE DOES!!! – Krzysztof Cichocki Feb 18 '17 at 21:36
  • I believe I know 95% of Windows guts but still surprised by Microsoft's talent to inject this kind of "features" when in order to delete file you have to either install Node.js (Node.js, man, the server-based Javascript engine made by a company non-existing at the time when Windows was created!) + packages with a hundred dependencies - basically you have to do everything except of straightforward `rmdir /s /q *` @hectopal nevertheless, thank you for the answer using Windows built-in tools. – maoizm Apr 05 '17 at 08:07
  • Didn't work for me. It moves files, but the resulting directory couldn't be removed (still long paths in it) – steph643 May 08 '17 at 09:56
  • @steph643 Could it due to a permission issue? – hectorpal May 09 '17 at 18:14
  • I guess it's complicated – Alex78191 Jun 01 '17 at 02:08
  • Robocopy looked like a winner for me, but it crashed when it hit some other internal limit. The last successfully reported path was 5666 characters. – Tim Sparkles Jun 12 '17 at 17:44
  • Wow. I'll try to do it in Python. https://stackoverflow.com/a/10620762/204981 – hectorpal Jun 12 '17 at 21:06
  • awesome! I prefer this because no 3rd party dependency. Thanks very much !!! – abhi Nov 23 '17 at 13:08
  • This should be the accepted answer :) – defines Jan 31 '18 at 21:02
  • 1
    When dealing with highly recursive directories, this option is much slower than 7zip. After waiting 20 minutes, I benchmarked them and realized 7zip, albeit doesn't show progress was running 50x faster. – Andy Gauge Aug 22 '18 at 22:25
  • While the other answers are useful this is the best answer as it requires no additional tools. md C:\empty robocopy c:\empty c:\Target /purge /NFL /NDL >nul &Rem CMD robocopy c:\empty c:\Target /purge /NFL /NDL >$null # powershell – HerbM Dec 02 '18 at 20:23
  • 2
    I agree with @MickyD: this should be right at the top as the accepted answer! I am now routinely using my "designated empty directory": robocopy C:\Users\\empty \purge >nul Although I like robocopy diagnostics when synchronizing directories, I want to delete things in silence... – Flandraco Feb 28 '20 at 17:55
  • This is very slick. It just worked. Doesn't require an install. – noybman Sep 05 '20 at 17:19
  • This worked, somehow I picked a folder in a folder and visual studio created 1000's of nested folders.. this was the only way to get rid of them.. Thanks!! – Gavin Stevens Jan 11 '23 at 16:17
90

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.

Herms
  • 9,404
  • 9
  • 35
  • 36
  • 110
    Didn't work for me. :( – Andrew Arnott Dec 26 '10 at 01:20
  • remember to drop into cmd before you attempt to run rmdir. – brianpeiris Jul 13 '12 at 13:16
  • You have to do this in the command prompt. It won't work in PowerShell unless you run it as a command prompt command from PowerShell. – Mark Rucker May 07 '13 at 13:51
  • 9
    For those who are unable to use this solution I have an alternative solution that worked for me (with too long path/file names): Move the file(s)/directory to C:\ to shorten the path, then delete from there. – Anders Sandberg Nordbø Sep 16 '13 at 10:38
  • 6
    It is possible to have directories that this solution won't work for, that 7-Zip can delete. I just had this happen to me. – SamStephens Dec 18 '13 at 16:07
  • For those it didn't work look at Benoit's answer on this question: http://superuser.com/questions/45697/how-to-delete-a-file-in-windows-with-a-too-long-filename – Nishant Nov 30 '14 at 13:53
  • Didn't work for me with 10's k's of recursive folders. Only rimraf worked. – Julian Knight Sep 11 '15 at 10:02
  • it can works but it's a recursive job, you have to hit the command a lot of times. if you have to delete node folders like "node_modules" use the @jordanbtucker answer. – Luca Filosofi Oct 06 '15 at 12:45
  • It doesn't seem to work. At least not on Windows 10... – bashan Oct 30 '15 at 20:11
  • W10x64. This didn't work, but 7zipx64 did. – Mayyit Jul 13 '16 at 12:46
  • Worked for me (Windows 10) – mmey Jul 27 '16 at 08:59
  • 2
    This solution is obtaining inconsistent results. Please, don't vote it up. – hectorpal Sep 13 '16 at 00:09
  • @hectorpal, the whole point of each user casting a single "up" or "down" vote is to ensure those that work for most people have a higher rating. So telling people (for whom this solution works) to not vote up is counter-productive to SU. –  Sep 23 '16 at 07:10
  • @ZaLiTHkA oh, good question. It's the old: straight voting vs strategic voting. Call me strategic, but I think StackExchagen is better when the voted solutions are most useful. The alternative is more noisy for the non-contributing visitors, that is the majority of readers. – hectorpal Sep 28 '16 at 01:47
  • Didn't work.... – Deleted Dec 23 '16 at 00:40
  • rd /s doesn't work if the file or path names are too long. Shift+Del in 7zip File Manager did it for me in Win10 pro. – Darr247 Jun 13 '17 at 00:36
  • This should absolutely not be the accepted answer. – defines Feb 13 '18 at 17:43
  • Nope - Windows 10. Filename too long still happens. – user922020 Aug 28 '18 at 15:43
  • This one worked when I used the absolute path to the directory, with the extended-length path prefix ```\\?\```. – Palec Jun 29 '22 at 17:14
34

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
Kenneth L
  • 13,954
  • 1
  • 22
  • 34
  • I tried all other answers (including answers from other similar questions) and this was the only one that ended up working for me. And it was by far the fastest. – glenneroo May 26 '17 at 12:54
  • 2
    This is by far the best answer: simple, built-in features, no messing round with the registry. Should be the accepted answer. – Kevin Roche Sep 12 '17 at 15:01
  • Easily the cleanest and most straightforward method, thank you Kenneth. – Seth Jun 13 '18 at 16:25
  • Uh this answer scares me because I actually have an X: drive. There isn't enough information here and I do not recommend people follow this without further explanation. – qodeninja Sep 09 '19 at 02:31
  • this works but it's more fiddly and it's actually easier to use 7zip or robocopy as described in other answers. – davidbak Apr 18 '20 at 03:02
30

The tool that I used when I had this problem was FastCopy. I selected Delete All from the dropdown, selected the the directory I wanted to delete, and clicked Delete All.

FastCopy delete dialog screenshot

FastCopy is portable (no install required), and there are 32 bit and 64 bit versions available.

braveterry
  • 803
  • 14
  • 18
  • 3
    If you are looking for a command-line tool, you could use a small tool I've published on GitHub (https://github.com/epsitec/Tools-RecursiveDelete). – Pierre Arnaud Mar 27 '15 at 06:10
  • I had a recursively generated set of nested file/folder. This was the only solution working – Pierluigi Nov 26 '15 at 13:15
  • 2
    This worked for me when, the command line stuff failed, the 7zip attempt failed. and even Microsoft support was stumped! – Kit Ramos Mar 25 '16 at 04:02
  • Same here - I kind of started to delete files with 7zip, but it also acted very weird and I had to Shift+Delere each directory multiple times in a row to actually delete it, but then I tried this tool, and worked like a charm. Launched it twice to get rid of all files. Thanks! – Artanis May 30 '16 at 20:47
  • Thanks, for me as well the only working solution, GREAT!!!! – tim Jun 12 '16 at 11:27
  • That's great.It worked very well for me. – fbarikzehy Sep 06 '16 at 11:33
  • Nothing worked, but this did! – demonkoryu Jan 09 '17 at 22:21
  • Stumbled on this when every other solution including robocopy failed for deeply nested dirs, but choked on some dirs the same way Shell (using Mobaterm) did. But seems like a better option for comparing/sync'ing dirs. Will give it a shot, hope it works better than BeyondCompare, which hogs the CPU. Thanks for the link ! – killjoy Dec 30 '17 at 13:48
  • perfect! This is a tool im not going to forget! – Adsy2010 Jan 22 '20 at 20:31
26

Cygwin's rm -rf works well on long paths!

gdw2
  • 1,295
  • 1
  • 11
  • 11
22

Just drop from Powershell into command by running below:

Cmd /C "rmdir /S /Q <dir>" 
Karl Glennon
  • 369
  • 2
  • 4
20

Dentrasi's solution is what worked best for me, but I wanted to post the specific steps in a top-level answer.

  1. Download and install the latest stable build of 7zip.
  2. Run the 7zip File Manager (7zfm.exe).
  3. In the 7zip File Manager, navigate so that you can see the name of the folder you want to delete (i.e. you're in the parent folder).
  4. Hold the Shift button down.
  5. Click the "Delete" button, either on your keyboard or in the 7zip File Manager toolbar; make sure you're still holding the Shift key down.
  6. Click the "OK" button; make sure you're still holding the Shift key down.
Mass Dot Net
  • 361
  • 3
  • 6
  • Nice try but still didn't work for me with 10's k's of recursive folders. Only rimraf worked. – Julian Knight Sep 11 '15 at 16:40
  • @JulianKnight: Yeah, this approach didn't end working for me 100% of the time, either. I just wanted to mention it because it was among the more straightforward processes, and it at least worked part of the time. The only thing I've found that that has worked 100% of the time is the Powershell + NTFSSecurity module script approach, which I described below. IIRC rimraf failed when called, which is how I got into this mess in the first place. See this Node.js thread for details: https://github.com/nodejs/node-v0.x-archive/issues/6960#issuecomment-46704998 – Mass Dot Net Sep 14 '15 at 05:38
  • Yes, there will always be problems like that. In fact it was the Arduino IDE's library installation code that did me in (Java), I accidentally selected the folder containing the libraries as a folder to install a library from and off it went! I killed the process as soon as I realised but already had 10's of k's of folders (damn i7/SSD!) The next main release of NPM is meant to help resolve many of the deep library installation issues for Node as dependencies should be met from a much flatter structure. Still I'm really glad someone put me on to rimraf - easy and free. – Julian Knight Sep 18 '15 at 08:22
  • @JulianKnight - Yep, a day or two ago somebody told me that npm3 should help to alleviate these problems by preventing deeply nested dependency folder structures. – Mass Dot Net Sep 27 '15 at 21:38
  • Don't know whether it "prevents" but it does treat nested dependencies more sensibly so that if your main app requires library X v1.0 and library Y requires Z which also requires X v1.0, the top level library will be used. Currently Z would install its own version of X. – Julian Knight Sep 28 '15 at 14:00
  • @JulianKnight: So yes, "mitigate" would probably be a better description -- it doesn't solve the problem, but it will hopefully make it a less common problem on Windows than it is today. – Mass Dot Net Nov 11 '15 at 05:16
  • Worked for me (win 7 x64). I messed up with the library folder of my project in Netbeans and it created a /lib/lib/lib/... path that seemed to be thousands of subfolders deep!! – Broken_Window Mar 08 '16 at 22:55
19

There are two things not already mentioned in the existing answers.

  1. You can use extended length path prefix to access long paths
  2. With Windows 10, you can enable long path support for general use - note however, that not all applications will work with long paths, you should test older software. Only software with an appropriate manifest entry will be able to make use of this.

Long Path Prefix

The Windows API has a special function that supports Unicode path names up to 32k characters long (each element being up to 255 characters in length).

This is often mistakenly referred to as UNC naming but it isn't though it is related.

The prefix used to tell the API to use long Unicode paths is \\?\ as in:

\\?\D:\very long path

UNC names are normally used when referring to paths on remote servers in the form \\servername\path name\file name. You can combine this with the long path prefix so: \\?\UNC\server\share

Note that some, poorly written but sadly quite common, applications do not support UNC file paths nor the long path prefix.

Reference: https://msdn.microsoft.com/en-gb/library/windows/desktop/aa365247(v=vs.85).aspx#maxpath

Long path support

This is an option that has been added to recent versions of Windows 10. It is not, at the time of writing, activated by default since it requires specific application support. Interestingly, PowerShell has support built in so enabling this option will allow long paths to be used directly in PowerShell scripts.

You can activate it via the Group Policy editor if you have a version of Windows with that. Or you can simply use the following Registry change. As always, please back up the section of the registry you are changing before doing anything.

  1. Run regedit.exe.
  2. Confirm the UAC prompt.
  3. Navigate to the key: HKEY_CURRENT_USER\SOFTWARE\Microsoft\Windows\CurrentVersion\Group Policy Objects\{48981759-12F2-42A6-A048-028B3973495F}Machine\System\CurrentControlSet\Policies
  4. Create or change the key LongPathsEnabled.
  5. If it does not exist, right-click on Policies and select New > Dword (32-bit) Value from the menu.
  6. Set LongPathsEnabled to 1 to enable.

Any applications with appropriate manifest entries will now have access to POSIX standard long paths. This should include Windows Store apps.

Reference: https://blogs.msdn.microsoft.com/jeremykuhne/2016/07/30/net-4-6-2-and-long-paths-on-windows-10/

UPDATE 2017-04-09

@maoizm pointed out an issue with the registry key above so I did some more digging. It looks like you can now set long filename support with a system key:

HKLM\SYSTEM\CurrentControlSet\Control\FileSystem LongPathsEnabled (Type: REG_DWORD)

This still only works for applications that are specifically set to use it though (requires an entry in the application's manifest.xml).

You should also note that you cannot use relative path names with this method.

Julian Knight
  • 14,313
  • 3
  • 27
  • 41
  • `{48981759-12F2-42A6-A048-028B3973495F}Machine` part of registry path will be different at each computer - it might be not obvious to everyone – maoizm Apr 05 '17 at 13:00
  • 1
    @maoizm - good point, thanks for highlighting that. Please see my update for an alternative if using the W10 Anniversary update or later. – Julian Knight Apr 09 '17 at 13:44
  • I am writing a library called FoundationIO, and I'm trying to make the long path the default, is there any function I can call or macro I can set to make long path support automatically enabled for any user of my library? – MarcusJ Jun 04 '18 at 02:49
  • Hi Marcus, is this a native Windows library? If so, .NET has the required support already present as part of the relevant Windows API - sorry that I don't know the details as I'm not a .NET or Win API programmer but the details should be searchable on MSDN. – Julian Knight Jun 08 '18 at 12:23
  • Setting LongPathsEnabled in the registry allowed me to use the standard RD /S command to remove a directory with really long paths after other attempts failed. This should be a highlighted answer. It worked on a Windows 2016 server to remove the backup of a C drive on a client workstation that had some really long paths in the backed-up AppData folders. Note that ROBOCOPY /PURGE of an empty directory also worked. – UweBaemayr Jan 01 '22 at 21:37
9

The 7-zip solution works great. Another option if you have git installed is to open a bash shell and use:

rm -f

(or rm -rf for folders)

User
  • 3,597
  • 9
  • 37
  • 50
  • 1
    For the uninitiated please explain *how* you "open a bash shell" in Windows? – O.M.Y. Jun 25 '16 at 11:30
  • 1
    @O.M.Y. you need git installed. Then from start menu you can open git bash or if you opted to have the shell extensions when you installed git then you can right click in or on a folder and select `Git Bash Here` – User Jun 25 '16 at 11:36
8

I like the cmd solution. Although I want to share another workaround that even works in just Explorer: try to shorten the names of the parent folders before deletion: shorter names will shorten the path. Say, you have this tree structure of the folder:

C:\

.. Projects

.. .. Some Awesome Project during spring 2014

.. .. .. Some Activity in the park in the city a friday

.. .. .. .. Bla bla bla bla bla bla bla bla bla bla bla bla

.. .. .. .. .. Actual files with long names, too

In this case you cannot delete the folders, you cannot delete or rename the files neither. If you want to remove the whole folder structure or part of it, you can temporarily rename the parent folders and shorten the path, and then remove the folder. The names can be just letters:

C:\

.. Projects

.. .. x

.. .. .. y

.. .. .. .. z

.. .. .. .. .. Actual files with long names, too

In my case I just wanted to remove the whole folder with many subfolders and files. So I didn't care about folders' names.

Anatoly Mironov
  • 981
  • 1
  • 7
  • 7
4

I suggest Total Commander (shareware, but keeps working after trial period with only a minimal nag start screen). That is the way I allways solve the too long filename issues.

Sopalajo de Arrierez
  • 6,603
  • 11
  • 63
  • 97
  • 1
    Thank the lord I found this. Having issues deleting a node_modules folder because it decides to stupidly nest the directories. – Alias Aug 14 '14 at 09:27
  • Well, @Alias, Total Commander makes that issue easier to detect, at least for me. After identifying such problem, `chkdsk` should solve it. – Sopalajo de Arrierez Aug 14 '14 at 09:59
  • Version 7.0 doesn't work, it shows an error message and suggests doing operation as Administrator, which fails as well. – izogfif Jan 21 '16 at 15:36
  • @izogfif : latest version as for today is 8.52a. I would try this one. You don't give details about your error message, but you could have some inconsistency on archive structure that must be solved (for NTFS at least) with "chkdsk c: /f" (assuming drive C:). – Sopalajo de Arrierez Jan 22 '16 at 00:27
2

You can also delete them using their equivalent short name. dir /x will show you them.

Ian Boyd
  • 21,642
  • 49
  • 139
  • 184
2

I created a simple java program that uses robocopy to delete the files. The jar is a runnable one. For additional output run it from cmd line.

https://drive.google.com/file/d/0B5pSEjxJvt_1WVp1T3puSm1CNjg/view?usp=sharing

CamHart
  • 196
  • 2
  • 3
    And if you do not want to use Java, you could use this tool I wrote using .NET (https://github.com/epsitec/Tools-RecursiveDelete). – Pierre Arnaud Mar 27 '15 at 06:09
  • @PierreArnaud great utility. I needed something like this for command line automation. @ CamHart good work from u too! Thanks guys! – amitthk Jan 20 '17 at 04:30
1

Bigger Directories is designed for this task as well. enter image description here

  • Download
  • Uses a recursive call with RemoveDirectoryW to delete existing directories
  • Uses own file repository for newly created directories
  • Files can be moved from the directories prior to deletion of containg directories.
  • Speedy, although navigating to top of directory is a click per nested directory
  • Windows XP(SP3) and up
  • Unsigned unmanaged code without installer, so possible Smartscreen warning when run from spare folder e.g. desktop.

More info at CodeProject.

Laurie Stearn
  • 416
  • 6
  • 19
  • Please read [How do I recommend software](https://meta.superuser.com/questions/5329/how-do-i-recommend-software-in-my-answers/5330#5330) for some tips as to how you should go about recommending software. You should provide at least a link, some additional information about the software itself, and how it can be used to solve the problem in the question. – DavidPostill Jan 09 '17 at 12:38
1

I faced this issue in Windows 10 and found this easy solution.

  1. Go in to little deeper of your folder tree.
  2. Drag and drop a folder from somewhere middle of the tree to somewhere begin of the tree using address bar.
  3. As in my image drag and drop red box to green box. (in this case path is shorter, but for long paths it works as well)
  4. Then your path become shorter and delete as normal using using delete key.

enter image description here

Nayana Adassuriya
  • 263
  • 1
  • 3
  • 7
1

If you have Bash on Ubuntu on Windows installed, you can cd to the parent directory in a regular command prompt and then type:

bash -c "rm -rf dirname_here"

Which launches bash, recursively deletes the directory from linux where there are no filename restrictions, and then exits back to your regular command prompt.

Richard Szalay
  • 681
  • 3
  • 11
  • 23
1

Got the perfect answer here (for files). Works in Windows 10.

REM Make sure there is no \ at the end of either path!
robocopy "C:\...\...\...\long\path" "C:\DeleteMe" "FileName" /MOV
REM Note: this ^ can be called many times before...:

REM Finally, recursively delete the higher up DeleteMe directory.
rmdir /S /Q DeleteMe

See also: https://msdn.microsoft.com/en-us/library/aa365247.aspx#maxpath

Not a single alternative answer in 4 questions was sufficient for my needs. You're welcome world.

Andrew
  • 552
  • 5
  • 9
1

Navigate to the directory in WinRAR, select the file, press F2, rename the file to a shorter name. Then you can delete the file as usual.

Source: https://www.youtube.com/watch?v=qQTyTprFAOg

root
  • 1,114
  • 11
  • 27
1

I've tried all the other answers, but they didn't work (atleast for me).

I came across delinfile and worked quite well and pretty quickly!

It's trial is limited to 3 actions / 15 Days but it's usable if you only wanted to delete a few folders/files:

Jürgen Paul
  • 1,025
  • 2
  • 12
  • 19
0

Expanding to jordanbtucker's answer:

If you have NodeJS npx package, you can just run this command without having to install the package globally.

npx rimraf <dir>

Takneik
  • 141
  • 1
  • 5
0

I finally found out how to do this via Powershell. Here are the instructions I followed for getting this to work on Windows 7:

  1. Create a new folder called NTFSSecurity in the folder C:\Users\XXXX\Documents\WindowsPowerShell\Modules\NTFSSecurity, where XXXX is your Windows username. Example: if my username was "aspnyc", the new folder would be C:\Users\aspnyc\Documents\WindowsPowerShell\Modules\NTFSSecurity .

  2. Download the File System Security PowerShell Module package - it should be available as a simple ZIP file.

  3. Open up a Powershell console, run Get-Module -ListAvailable and make sure NTFSSecurity shows up somewhere in the list of registered modules.
  4. In the Powershell console, run Import-Module NTFSSecurity .
  5. In the Powershell console, run Remove-Item2 "YYYY" -Recurse , where YYYY is the Windows path to the folder you want to recursively delete (e.g. C:\Potatoes\Badgers\FolderToDelete ).
Mass Dot Net
  • 361
  • 3
  • 6
0

I finally found out how to do this with Powershell on Windows 10 where the individual filenames were too long. Here are the steps I followed for getting this to work on Windows 10:

  1. Download the File System Security PowerShell Module package - it should be available as a simple ZIP file.
  2. Extract the Zip file as a folder NTFSSecurity in the directory C:\WINDOWS\system32\WindowsPowerShell\v1.0\Modules.
  3. Open up a Powershell console with the administrative elevation (a.k.a. Admin Mode) and run Get-Module -ListAvailable and make sure NTFSSecurity shows up somewhere in the list of registered modules.
  4. Run Set-ExecutionPolicy Unrestricted to bypass the signed execution mode. You have to confirm this action with a Y(es)
  5. Eventually cd to the folder that you want to delete.
  6. In the Powershell console, run Import-Module NTFSSecurity .
  7. In the Powershell console, run Remove-Item2 "YYYY" -Recurse , where YYYY is the relative or absolute Windows path to the folder you want to recursively delete (e.g. C:\Potatoes\Badgers\FolderToDelete ).
  8. Check if it worked.
  9. Eventually set back to signed execution mode with Set-ExecutionPolicy Restricted. You have to confirm this action with Y(es).
Epicurist
  • 128
  • 4