Deleting a directory in Windows 10 with 'Source path too long' using robocopy

2016-10-04 Update: Microsoft finally allows long paths, provided you are running the latest version of Windows 10; you have to opt-in by editing a group policy for now—maybe by 2050 or so this will be the default, once all the old legacy Windows apps are finally dead!

For some reason, Windows doesn't play well with deep folder hierarchies. Often, when building Drupal projects inside VMs with synced directories, I end up with folders with 10+ levels of hierarchy, and when I try to delete the directory within windows, I get the error "Source path too long" (or "File path too long"). This happens a lot with npm, composer, or other package/dependency managers, and I'm amazed there's no easy workaround... but I figured I'd document what I do here, because I've had to look up the process too many times.

1 - Download robocopy

First, download and install the Windows Server 2003 Resource Kit Tools, which includes a CLI utility, robocopy.

2 - Use robocopy to move the folders into a deletable folder

  1. In cmd or PowerShell, cd into the parent directory of the folder you need to delete.
  2. Create an empty directory (mkdir empty).
  3. Copy the directory to be deleted into the empty directory (robocopy empty DELETE_ME /mir).

3 - Delete both directories

rmdir DELETE_ME, then rmdir empty, and the directory should be gone!

There may be other ways to do this, but I haven't seen any other reliable method. Credit where it's due.

Comments

I am getting this same issue and its very difficult to solve this problem without any third party app, i guess the best thing to do is to use a third party app like Long Path Tool. Just download it and use it to solve this issue. I hope this would help.

I'm getting a rather large number of comments about Long Path Tool, enough to make me think something is a bit shady with this 'free' 3rd party tool. I'll leave this comment in, but I still recommend a first-party approach like the one in my blog post, as the software is signed/created by Microsoft and is practically guaranteed to be free of malware or unintended sidecar installs...

You can do something similar in pure Powershell instead, with no need for extra software.

Just mount a deeper part of the share:
New-PSDrive Q FileSystem "C:\Path\To\Some\Deep\Location"

Then delete from the new mount point (Q, in the above example) using standard commands (rm, del, etc.). If the path is really, really, really ridiculous you may have to mount it a few times at different parts of the path.

This worked like a champ. Thanks for the easy to follow help.

Wow.. worked great deleting folders nested 450 characters deep. Never would imagine that good ole Robocopy would work to delete files and solve my issue.

Why are you telling people to go download Robocopy from 2003 toolkit if they're on Windows 10?

This article was written in 2016, when Windows 10 still didn't have any mechanism for dealing with long file paths, so I have left it up for historical purposes. Nowadays, you have to edit a group policy which is the preferred option (see link at the top of the article), but IMO it's still kinda stupid you can get in this situation in the first place.

We use Robocopy to remove deep file paths but we use the /purge command. For example if the directory with the deep path was named 'delete' we do similar to you and create a new directory named 'empty' and then run this command Robocopy 'empty' 'delete' /Purge. This scans the 'delete' folder and purges (deletes) anything not in the 'empty' folder.

You can also use the "subst" command from a CMD prompt to map a drive letter to a path, similar to the PowerShell command mentioned earlier:
subst z: "C:\Path\To\Some\Deep\Location"