Sunday, May 18, 2014

How to Delete Files which exceed 255 Characters Without 3rd Party Tools

Windows Explorer and many Windows applications including PowerShell are limited to 255 characters max file path.  Whilst this limitation is in place at an application level, the NTFS file system does not support this limit.  In fact file paths can be created remotely over the SMB protocol to exceed this limit which is how most file servers get stuck with folder paths administrators can no longer maintain using the native Windows Explorer application.

When attempting to delete folders using Windows Explorer the following errors may be experienced:

The source file name(s) are larger than is supported by the file system. Try moving to a location which has a shorter path name, or renaming to shorter name(s) before attempting this operation.

 
An unexpected error is keeping you from deleting the folder. If you continue to receive this error, you can use the error code to search for help with this problem.
 
Error: 0x80004005: Unspecified error
 
 
Even new applications from Microsoft such as PowerShell do not support file paths longer then 255 characters despite this being supported by NTFS.
 
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 then 248 characters.
 

I am going to show you a way to remove excessively long file paths without using third party tools such as Long Path Tool which come at a price or booting into different operating systems such as Linux to remove the unwanted file paths.

One Microsoft application which is not limited to the 255 character limit is robocopy.exe.  I know this as I often move large volumes of data with Robocopy between server infrastructure and have never been hit with a file path limitation.  As a result, this is the tool I chose to remove the data.

If you use robocopy with the /MIR switch, it will make the destination folder exactly the same as the source folder.  So if the source folder is empty, it will delete all data in the destination empty and in result deleting the content.

I have a path here with 3 users which have folder structures which exceed 255 characters.  Windows Explorer failed to remove these folders.


I created an empty folder on C:\ called test then used the mirror switch to copy the test folder to the HomeDrives folder.

robocopy /MIR c:\test E:\UserData\HomeDrives


 After running the command all my user folders under E:\UserData\HomeDrives were deleted.

This is a handy trick for dealing with folders on file servers which have excessive amounts of long folder structures which exceed the 255 character limit.

Hope this has been helpful, feel free to leave me a comment below.

93 comments:

  1. Thank you sooooo much, robocopy FTW!

    ReplyDelete
  2. G8, I use Robocopy to move data and then delete... Thank You...!

    ReplyDelete
  3. Thanks alot Clint It was very helpful...

    ReplyDelete
  4. Great Tips ! Thank you !!

    ReplyDelete
  5. Man that was a great tip! Saved the day from this annoying problem!

    ReplyDelete
  6. Thanks heaps. Just in case, you didn't specify the switch needed to delete stuff - I didn't want to mirror files, I just wanted to delete the long (endlessly long) files that had somehow built up as a product of using a synch program. Using /PURGE seemed to be the key and the following certainly worked for me in deleting all the long files and folder names in the affected directory. A lifesaver, thanks again:

    c:\users\peter>robocopy /PURGE c:\test d:\minidrivesynch\a

    Naturally the command could be run from anywhere I just happened to be at the users\peter prompt. Interestingly even though I didn't want to copy the files (i.e. so you would have thought /PURGE of the target directory would work) the command did not work unless the copy path was there as well (i.e. c:\test). That's weird, but it worked. I'm free!!!!

    ReplyDelete
    Replies
    1. PERFECT. Thank you. The "PURGE" worked as you wrote.
      At the beginning I had 21 folders interleaved (identical names) + 2 subfolders: origins unknown! It seemed to continue growing on.
      Impossible to delete: file paths too long. Roland Geissler, now nightmare-free! Thanks.

      Delete
  7. cannot say thank you enough, i've been around pc since model ibm 86, never knew the robocopy command existed, it is a joy
    what i did notice, perhaps my ignorance, the command is finicky if the directories have more than one word it seems it counts the spaces as well, at least what i think
    tried quite a few combinations and only one whereas both the going and coming had "one" word, anything with a space and the command failed me.
    what i did was send the irritating files to a not important usb drive and reformatted the drive after. it's safer, don't need to touch any of the directories.
    and the command doesn't care what kind of drive it is. so the further away from the computer the cleaner. MY OPINION

    ReplyDelete
  8. Thank you.. It worked like wonder .. Thanks again..

    ReplyDelete
  9. in my case robocopy /purge has crash after a while, perhaps because there're 2000+ sub directory which I've no idea where it came from. after some googling I found suggestion to use 7-zip file manager. just browse to that dir with 7-zip and simply hit shift del, and its gone.

    ReplyDelete
    Replies
    1. Thank you so much, Its all the nodejs node modules which only is creating the longest source file name paths in windows NTFS file system. Thanks to 7zip for helping.

      Delete
    2. Anonymous; please don't be unamed!! Just kidding!! Thanks a ton!! Just please add one more thing; use "Shift + Del" as simple "Del" will call the OS delete routine where as "Shift+Del" 7zip's own routine which clear up explicitly and forcefully and what ever you may say but worked like charm!! Thanks a looooooooooooooooooooooooooootf of again!!.

      Delete
  10. As with others: I've been playing around with Node and NPM and created some long files paths.Perhaps just deciding to delete the file directory wasn't the best idea but once done I was left with this issue. Robocopy /MIR blankdir nodedir helped resolve it. Thanks.

    ReplyDelete
  11. Simply cool, works great!
    Thanks!

    ReplyDelete
  12. Works well... Thanks

    ReplyDelete
  13. Thanks alot! Great tip.

    ReplyDelete
  14. Now that was easy, Thanks!!

    ReplyDelete
  15. easy way u can try long path tool :). this is the best way i think :)

    ReplyDelete
  16. Thanks bloke from WA ! This worked on my Win2008 server on a folder where the files were not even visible except to Unlocker and the DOS command rmdir /S neither of which could actually do anything about it. I suspect that these files were put there using Robocopy by my sysadmin in the first place, so the solution is kinda neat. And with Robocopy already installed it took about 5 seconds to accomplish .....

    ReplyDelete
  17. This issue had driven me insane since I had heaps and heaps of such folders(with pathnames longer than 255) due to my mistake with one of the sync scripts written using robocopy. I had tried many options including using long path tool which actually caused BSOD on my pc and scared me to death. I was hence unwilling to use any third party software for this job. And then, I came across your solution and it WORKED WONDERS. The MIR command with robocopy ran for like 12 minutes(imagine the junk I had which needed to be get ridden of!) and voila, I could finally delete the folders. I can't thank you enough. Cheers :)

    ReplyDelete
  18. Great info! Download "Long Path Tool" since I downloaded it I never encountered the same error ever.

    ReplyDelete
  19. Excillent Work!!
    Thanks :)

    ReplyDelete
  20. My idea is that the best solution is Long Path Tool.

    ReplyDelete
  21. Thank you so so much! this tip saved me from a lot of headache ;)

    ReplyDelete
  22. Thank you very much for this tip.

    ReplyDelete
  23. Thanks a lot for this well illustrated post!
    It saves me a lot of time and patience.
    Fred, Montpellier, France

    ReplyDelete
  24. Thank you! Clear description and instruction. Worked first time. Problem solved!

    ReplyDelete
  25. Awesome! Really save my life... T_T

    ReplyDelete
  26. That is worked for me as well, Thanks for the post :)

    ReplyDelete
  27. This is the only thing that I found that solved this issue. Thanks!!

    ReplyDelete
  28. hello friends,please try long path tool , its really helpful

    ReplyDelete
  29. Thanks!
    The only solution I could find.

    ReplyDelete
  30. This not deletes just hide files you morons check

    ReplyDelete
  31. This comment has been removed by the author.

    ReplyDelete
  32. Thanks! This was the best solution for me to use in a PowerShell deployment-script. The script failed because of genereated class-files, so I added the robocode line before the rmdir command:

    robocopy /MIR D:\install\empty $existingDomanDir | Out-Null
    execBat "rmdir /S /Q $existingDomanDir"

    Note the piping of the output of robocopy. This is done because robocopy is pretty verbose

    ReplyDelete
  33. Awesome.......... thanks a lot for the solution

    ReplyDelete
  34. Thank you. It was a great help.

    ReplyDelete
  35. Thank you, you just saved me a ton of time!

    ReplyDelete
  36. Great..! Thank you. It's working.... :)

    ReplyDelete

  37. If you trying to resolve path too long problem
    Use long path tool.... It's very easy solution and you can find it..... By writing at Google search "long path tool"

    ReplyDelete
  38. great writeup, thanks! Very easy fix.

    ReplyDelete
  39. This is a great help for those of us who were never pleased with MS for this in the first place. Thanks for showing me a way out of a 2.6 TB tangled mess! AJ

    ReplyDelete
  40. Long Path Tool could be the answer to your problem.

    ReplyDelete
  41. Amazing! it's working on me, thank you so much!!!

    ReplyDelete
  42. It worked, but... it did not copy the files in the offending folder to the one that I created for this purpose, it simply deleted them, which I wanted to do anyway - fortunately.

    ReplyDelete
  43. thanks a lot, it worked to me too.... thanks one more time.

    ReplyDelete
  44. This comment has been removed by the author.

    ReplyDelete
  45. Thank you Very much! solved a problem which was bugging me over the weekend!!!

    ReplyDelete
  46. Thanks... worked like a charm :)

    ReplyDelete
  47. You can use “Long Path Tool” !

    ReplyDelete
  48. thank you, saved me a lot of time!

    ReplyDelete
  49. Thanks, It's really great tip...

    ReplyDelete
  50. Awesome tip, this worked perfectly for me on Windows 10

    ReplyDelete
  51. Thanks a lot!! It worked like a magic :)

    ReplyDelete
  52. Super awesome !! Great help man

    ReplyDelete
  53. I used to have similar problems too, but after using "long path tool" everything was solved. Try this software and you would be glad you did.

    ReplyDelete
  54. Beautiful Tip and Share! Thank You much for the help. I ran into the exceeding min characters for using git and nodeJs. Installing packages sometimes can exceed the limit. I am not sure why, but possibly for the fact of custom builds using angulars command line interface.

    ReplyDelete
  55. Long Path Tool help me a lot when i have an issue like file deleting or renaming the file. Also good to use if file name and file extension is too long

    ReplyDelete
  56. When I try to delete or move the file, It gives me the error 0x80004005. I also cannot get to the Command prompt san someone help me?
    Email me with a possible solution ASAP: shmadamading@gmail.com

    ReplyDelete
  57. Thank you so much! Worked for me. (:

    ReplyDelete
  58. Saweet trick!
    Worked perfect on Server2012r2 with folders that contained invalid characters and missing all security descriptors.
    Check disk failed but Robocopy got it done without a restart.
    Thank You!

    ReplyDelete
  59. I have used Long Path Tool, It works as per needs.

    ReplyDelete
  60. I have used Long Path Tool, It works as per needs.

    ReplyDelete
  61. Long path error!
    "Long path tool" is very helpful for this problem. You can solve this problem like copy, delete, long path files by using this tool.
    I used to have similar problems too, but after using "long path tool" everything was solved.

    ReplyDelete
  62. Thank you very much.

    Solution very good.

    ReplyDelete
  63. The Internet thanks you. This is the kind of stuff that makes earth amazing.

    ReplyDelete
  64. Thanks mate, worked like a charm

    ReplyDelete
  65. Dude, you saved my life...it's incredible how some people seem to fit entire novels into their folder names...even more unbelievable is the fact that even Windows 10 still can't handle filenames longer than 255 characters...welcome to 1985.

    ReplyDelete


  66. “Long Path Tool” is very helpful for this error !
    best solution for your problem.

    ReplyDelete

  67. “Long Path Tool” is very helpful for this error !
    best solution for your problem.

    ReplyDelete

  68. Long path tool is the very good program for error, unlock solution.
    Try it and solve your problem.
    I used long path tool and I solve my error, unlock problem solution.

    ReplyDelete