How to delete ALL node_modules folders on your machine and free up HD space!

Mark Pieszak on August 07, 2019

Originally published on the Trilon Blog on Aug 7, 2019. Whenever we work on a new project we need to run npm install, but how often do we think ... [Read Full]
markdown guide
 

Thanks!

With this you can see how much each node_modules directory occupies:

find . -name "node_modules" -type d -prune | xargs du -chs

It will also tell you the total at the end of the command

 

I like it !! Let me update the article with this in there. Definitely helpful ๐Ÿ™Œ

 
 

I've actually written a little program recently that does something like this. It cleans up generated files and dependency folders, in all projects that haven't been worked on in more than 30 days (so you don't have to run yarn install again on your current projects).

 

Very cool! I like that it takes care of the -older- projects that haven't been touched in a while - I'll have to take it for a spin and give it a try!! Great work !

 

This is a very nice thread. Thanks!

Well for me, I had 3 projects out of like 15+ in my dev folder that I didn't want the node_modules folder to be deleted.

My little workaround was to rename the node_modules of those projects to node_modules2 so it won't be affected by the script, then I turned it back after.

 

Yeah a lot of times I just wipe everything out, and go back to the recent projects and npm install again real quick. Check out npkill as well, you can interactively delete node_modules folders with it!

 
 

How much space did you end up saving? ๐Ÿ™Œ

 

12 GB of hard disk space :). Actually, I got an idea, if this can be written as a cronjob which runs for every 48hrs (something like that), and deletes "node_modules" for the projects which were untouched for some time. May be like more than a week. Most of the time I tend to clone a repo check something and never visit it again :X :D

Thatโ€™d be amazing! Great idea ๐Ÿฅ‡
We might have to look into making that real quick! Would be fairly simple with NestJS Cron jobs and it could be registered as a startup service on the OS startup maybe ๐Ÿค”

The same happens to me, we do lots of open source & consulting so after a few weeks/months there are so many untouched projects, Iโ€™m constantly out of HD space ๐Ÿ˜‚

Exactly !! Let's start looking into this. I am new to NestJS, but it's worth a try which make life a little easier to manage :D

Iโ€™ll have a (hopefully) nice introduction blogpost to NestJS coming soon so keep an eye out!

Youโ€™ll โค๏ธ NestJS once you get going with it !!

Thank you, so much ๐Ÿ˜„ Would love to learn and built it ๐Ÿ˜Š

 

Since it wasn't mentioned in the article: npm tools such as pnpm (by @zkochan ) and yarn in PnP mode (or the upcoming Yarn v2) will keep a single copy of (each version of) a package you use across all your projects and just link them locally, saving you a ton of space without having to worry about deleting old npm_modules folders. PNPM also does a lot of cool other stuff, not my product, but I recommend it without reservation.

 

Great idea! Iโ€™ll have to do add notes about pnpm in there for folks to use in the future! Thanks for reminding me ๐Ÿ™Œ๐Ÿ™Œ

 

The windows version will take a while as it looks at every file. If you just want to blast it, using robocopy is a good solution.

$ robocopy c:/someemptydir c:/path-to-node_modules /MIR > NUL

This method also gets you round the windows long file name problem that you often come across here.

 

Wonโ€™t this delete everything, and not just the node_modules themselves ? Iโ€™ll have to give it a try ! I saw someone else post this as well:

robocopy EMPTY_FOLDER node_modules /mir

 

Itโ€™ll delete everything under the destination path. Syntax is:

Robocopy <source> <destination> /MIR

With a redirect to null to remove output.

Iโ€™m not a node dev but devops who has to regularly remove these things from build slaves. Obviously thereโ€™s a cost on next build re-downloading.

 
 
 

Much appreciated @KP!
How much space did it end up clearing out for you??

 

About 1GB, but I have enough space (for now!) I can potentially "save" another 14GB if I delete all my node_modules folders. Thanks!

 

I haven't maintained this repo much but I have created this github.com/canastro/remove-git-ign...

Its a cli that finds all .gitignore files under a given root and deletes them after confirmation.

 
 

This is awesome, I saved so much disk space by running this script

 
 

This is very handy! I run into storage issues on my laptop all the time.

Today I cleared 697M in node_module folders.

 
 
 

Appreciate the kind words ! Hope it cleared out plenty of space for you! ๐Ÿ™Œ

 
 

Of course! More related tips and tricks soon ! ๐Ÿ™

code of conduct - report abuse