How to effectively delete node_modules on Windows

This post is not directly related to SharePoint development, however, you might find it interesting if you deal with javascript projects (like SharePoint Framework projects)

There is one particular thing I don't like in JavaScript development infrastructure. Well, actually I hate it. That's the node_modules folder. From one side that's a very important folder, because it contains all dependencies, from the other side, usually, it contains tons of files, and some of them are not necessary. When you need to delete it, sometimes it takes just too much time: 

And I'm not the only one!

I've tried different options and found out, that rimraf works blazingly fast (suggested in the tweet above). Even when compared with the native windows command line "RMDIR", rimraf somehow works 30% faster. 

However it's not very convenient to use it every time from a console, so I added shell command extension for convenience.

1. Firstly install rimraf module globally:

$ npm install rimraf -g

2. Create a .bat file fast-del.bat:

@ECHO OFF
SET FOLDER=%1
IF [%1]==[] (
	ECHO Delete Folder: "%CD%"?
	PAUSE
	SET FOLDER="%CD%"
	CD /
)
ECHO Deleting folder %FOLDER%
rimraf %FOLDER%

3. This .bat file should be available under your "PATH" environmental variable. In other words, you should be able to run it from any command-line from any location. The easiest approach is to just copy this file to C:\Windows directory (it's absolutely safe). 

4. Open regedit and go to "HKEY_CLASSES_ROOT\Directory\shell\" path. 

5. Right-click on the "shell" folder and select New -> Key. Give it the name "Fast Delete".

6. Right-click on a newly created "Fast Delete" folder, then New -> Key. Give it the name "command".

7. Double-click on the default value for "command" folder and under value data enter:

cmd /c "cd /d %1 && fast-del.bat"

This is how it looks like:

Now when you right-click on your folder, you have this extra-menu:

Before deletion, it will ask to confirm your action. 

Also, I found useful to recursively delete all node_modules folders nested in different directories (to free some space when your project base grows). You can do this via the shell menu item as well. 

Simply

1. Create a new .bat file "fast-del-node_modules.bat" (and copy to C:\Windows): 

@ECHO OFF
FOR /d /r . %%d IN (node_modules) DO @IF EXIST "%%d" fast-del.bat "%%d"

2. Create a new folder "Delete node_module recursive" in the registry (see above steps). 

3. For the value add 

cmd /k "cd /d %1 && fast-del-node_modules.bat"

/k will remain console open, /c switch closes the console. You can play with these parameters to have the desired effect. 

Title image credits - Technology photo created by onlyyouqj - www.freepik.com