How to delete a directory with too long path / name for normal delete?

I faced a very strange problem today morning. I created a simple ASP.NET MVC Core application on my laptop. After playing around with it for sometime, I decided to delete the project source code using Delete key on my keyboard and there you go, I got very famous (it seems) error message

file-name-too-long-to-delete-windows

At first, I thought, maybe the folder size is too big for Recycle Bin to handle it, so I tried with Shift + Delete key combination without any success.

Hmm. I knew I had a problem. I immediately googled around it and not so surprisingly found many forums with suggestions on possible resolutions.

I started one by one, first renaming long file or directory names with short ones. for e.g renaming c:\temp\really_long_directory_name with c:\temp\r etc. Although few users claimed that this has fixed their problem, but it requires a manual intervention of renaming all the directories with shorter name, which is definitely not an efficient solution. I mean there has to be some solution to delete a directory efficiently.

Then I remembered the famous node_modules folder from one of my AngularJS application. For those who are not unknown to it, node_module folder contains all the node packages of the application dependencies mentioned in package.json file. Going into its detail is not in scope for this article, so lets get back to deleting directories. This folder contains recursive dependency and package information of other NodeJS packages. I tried to delete the folder and oops I got the same error message. Now I was desperate to find out an efficient solution to delete this directory.

Enter rimraf – a deep deletion module for node. I love node community, they have solution for all the weird problems exists there. If there are times when you want to recursively delete files this is a tool that does one thing well.

Install rimraf globally so that you can execute it from any path

npm install rimraf -g

Once installed, you should be able to delete the problematic directory using rimraf command shown below

rimraf directory_with_really_long_name_and_path

I hope this helps someone.

Leave a Reply