Delete deep nested folders in Windows

Trying to delete a nodeJs project the other day but had a bug with long file names in windows.. so here’s the fix:

Posted by Michael Whelan

Deleting Nested node_modules Folders

When using Node Package Manager (NPM) on Windows, it has an annoying habit of creating massively nested node_modules folders that exceed the 260 character path length limit. This makes it extremely painful to try to delete and causes problems with most windows tools. In Visual Studio, for example, I cannot Refresh Folder when I am in a website project (as opposed to a web application project).

Fortunately, the fix is pretty simple.

First, you need to install RoboCopy. If you are on Windows 8, I suggest you use the version from the Windows Server 2003 resource kit as there appear to be issues with the Windows 8 version. Next, create an empty directory and use the RoboCopy mirror flag to mirror the empty directory to the problematic node_modules folder. You can then delete the empty directory in the normal way. Cunningly, deleting the mirrored directory also deletes the node_modules directory.

mkdir new_empty_directory && robocopy new_empty_directory .build /MIR

Credit to toby1kenobi for this clever solution!

Finally, you just have to restore the deleted node packages. From the application directory, just run npm update:

npm update

This command will update all the packages listed in the packages.json file to the latest version. It will also install missing packages.

2 thoughts on “Delete deep nested folders in Windows

  1. stepan raisa says:

    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. Try this software and you would be glad you did.

  2. stepan raisa says:

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

Comments are closed.