Delete all node_modules folders recursively on Windows or Mac

Last night, I asked an interesting question.

I want to do this because node_modules creates so many small files, it greatly increases my backup times (lots of small files are super slow to copy, no matter how fast your disks are). Not to mention, it feels messy, ugly, and I want that disk space back.

If you are like me, after a year of “modern dev” on a new laptop, you have 100s of projects, and therefore 100s of node_modules folders and maybe a billion .js files .. what a mess.

I got some good answers to it. But, I was really hoping for an out of the box solution that I could just run from command line, so I don’t have to install stuff or download a utility, or worse .. write something myself etc.

The best solution was suggested by Davis Trent.

Here is how you do it,

Windows

To see which folders will be deleted,

FOR /d /r . %d in (node_modules) DO @IF EXIST "%d" echo %d"

And then to actually delete them, just run this,

FOR /d /r . %d in (node_modules) DO @IF EXIST "%d" rm -rf "%d"

(Sidenote: I use cmder which supports things like rm -rf on Windows)

Mac

find . -name "node_modules" -exec rm -rf '{}' +