433

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,943
  • 2
  • 36
  • 48
Herms
  • 9,992

25 Answers25

501

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
  • 16,463
  • 24
  • 53
  • 67
Dentrasi
  • 11,325
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.

215

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,563
89

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,992
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
  • 14,104
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.

26

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

gdw2
  • 1,385
22

Just drop from Powershell into command by running below:

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

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,749
  • 3
  • 31
  • 46
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.
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,835
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.

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.

3

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

Ian Boyd
  • 23,066
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
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.

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

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.

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
  • 632
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,799
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,145
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
0

If you can use long paths as described in Julian's answer, then a little PowerShell and .NET should work for you. For example, I had a directory where, buried many folders deep, were some very long file names. I needed the whole directory gone, so I used this:

Add-Type -AssemblyName "System.IO"
$d = [System.IO.DirectoryInfo]::new("\\?\D:\my\parent")
$d.Delete($true)

I ran the above before I could try it, but I suspect the .NET probably isn't even necessary, you could simply try to use existing PowerShell commands:

Remove-Item "\\?\D:\my\parent" -Force -Recurse
Cᴏʀʏ
  • 211
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).
Epicurist
  • 128