378

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?

  • 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 3 '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 3 '09 at 20:42
  • By Far the simplest solution: 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 Priyadarshane Nov 29 '15 at 4:04
  • 1
    This older question has some interesting answers too: superuser.com/questions/45697/… – AdamV Dec 3 '15 at 11:01

23 Answers 23

421

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

  • 5
    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
  • 63
    worked great using shift+Del - 7zip couldn't send long-pathed files to the recycle bin.... – cori Mar 2 '11 at 3:44
  • 66
    +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
  • 8
    7zip didn't work for win7 64, so linux did. – Erik Friesen May 15 '14 at 20:09
  • 13
    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
310

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.

  • 7
    Tried many ways. This just works perfectly! – Duckegg Nov 17 '15 at 13:14
  • 2
    The best answer of all :) yes its easy beasy – Al-Mothafar Dec 8 '15 at 12:47
  • 2
    This should be the accepted answer. – NINCOMPOOP Mar 10 '16 at 6:44
  • 13
    Your solution works but this is so f##k-up that I have to use a node js module for deleting node js modules folders... I do not want to live on this planet anymore – Benoit P Sep 23 '16 at 10:04
  • 4
    Node created the problem in the first place for me so... interesting... that they also have a solution. – Eric J. Sep 27 '16 at 16:43
140

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.

  • 11
    Thank-you good sir!! This should be the accepted answer as all the others either didn't work or required a 3rd party tool. :) – MickyD Dec 23 '16 at 0: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 6 '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 <Del> "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
  • 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
84

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.

  • 105
    Didn't work for me. :( – Andrew Arnott Dec 26 '10 at 1:20
  • remember to drop into cmd before you attempt to run rmdir. – brianpeiris Jul 13 '12 at 13:16
  • 8
    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
  • 2
    This solution is obtaining inconsistent results. Please, don't vote it up. – hectorpal Sep 13 '16 at 0:09
29

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.

  • 3
    If you are looking for a command-line tool, you could use a small tool I've published on GitHub (github.com/epsitec/Tools-RecursiveDelete). – Pierre Arnaud Mar 27 '15 at 6: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 4: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
26

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

  • 3
    Cygwin worked for me perfectly! Just did a -rm -rf and all the files got deleted. – Ranhiru Jude Cooray Oct 23 '14 at 4:42
  • 3
    Worked for me as well under GNU bash, version 4.1.10(4)-release – rjzii Sep 22 '15 at 4:13
  • 2
    Worked great when running from a Git Bash command window. I had installed GitGui for that. – Albo Best Aug 30 '16 at 15:48
25

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
  • 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
  • 1
    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 9 at 2:31
22

Just drop from Powershell into command by running below:

Cmd /C "rmdir /S /Q <dir>" 
  • 3
    "The directory is not empty". Oops. The OP wants to delete a non-empty folder. – leye0 Apr 23 '16 at 23:19
  • how is this different to @herms' answer ? – bigbadmouse Sep 11 '18 at 10:22
18

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.
  • 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: github.com/nodejs/node-v0.x-archive/issues/… – Mass Dot Net Sep 14 '15 at 5: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 8: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
10

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.

  • {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 5 '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 9 '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 4 '18 at 2: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 8 '18 at 12:23
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.

  • 1
    I've written a tiny tool that does just that and put it up to GitHub. github.com/epsitec/Tools-RecursiveDelete – Pierre Arnaud Mar 27 '15 at 6:07
  • 1
    All commands or Shift+DEL doesn't work for me, but this does. – larkee Jul 21 '15 at 12:35
  • Doesn't really work when you have 10's k's of nested folders – Julian Knight Sep 11 '15 at 16:39
  • also doesn't work if one of the folders has a really long name (you can't rename the offending folder). – aucuparia Nov 21 '17 at 12:06
7

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)

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

  • 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 9: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 9: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 0:27
2

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

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

  • 2
    And if you do not want to use Java, you could use this tool I wrote using .NET (github.com/epsitec/Tools-RecursiveDelete). – Pierre Arnaud Mar 27 '15 at 6: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 4:30
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:

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.

  • Please read How do I recommend software 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 9 '17 at 12:38
  • @David: Better? Link was very useful, thanks. – Laurie Stearn Jan 10 '17 at 10:35
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

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.

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

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.

0

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

protected by BinaryMisfit Nov 9 '10 at 6:48

Thank you for your interest in this question. Because it has attracted low-quality or spam answers that had to be removed, posting an answer now requires 10 reputation on this site (the association bonus does not count).

Would you like to answer one of these unanswered questions instead?

Not the answer you're looking for? Browse other questions tagged or ask your own question.