DEV Community

Cover image for How to delete ALL node_modules folders on your machine and free up HD space!
Mark Pieszak for Trilon

Posted on • Updated on • Originally published at trilon.io

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

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 about the toll this takes on our hard-drive?

The dreaded "out of space" message

A single node_modules folder can take up anywhere from 200+ megabytes of space (sometimes 1GB+ !).

Now take a look at your github/ folder, or wherever else you're storing the majority of your projects, and you can start to see where some of your free hard-drive space has gone!

I recently tweeted about this, as it's something I use every few months and wanted to share with others who might have ran into the same situation themselves!

Liquid error: internal

List all node_modules found in a Directory:

First, let's take a look at ALL the node_modules we have in a directory, before we actually start deleting them!

NOTE: Make sure you cd into a specific directory where most of your projects are. ie: documents or documents/github.

Mac / Linux:

This command will print out each folder, and even show us how much space the folder is occupying ! Thanks to Rhymes for the tip!

$ cd documents
$ find . -name "node_modules" -type d -prune -print | xargs du -chs

--- Example output ---
1.0G ./Github/big-project/node_modules
225M ./Github/Trilon.io/node_modules
482M ./Github/Some_Demo/node_modules

1.7G total
Enter fullscreen mode Exit fullscreen mode

Windows:

$ cd documents
$ FOR /d /r . %d in (node_modules) DO @IF EXIST "%d" echo %d"
Enter fullscreen mode Exit fullscreen mode

This list will give you a good idea of just the sheer amount of projects you have installed on your machine!


Delete all node_modules found in a Directory:

NOTE: Use caution here, and make sure that you are in a directory where you're comfortable removing all the instances of node_modules, run the script above to see a full list of them all before deleting.

This script is actually very similar to the one above, but we're going to be utilizing rm -rf to completely delete them.

WARNING: This process is irreversible!

Mac / Linux:

$ cd documents
$ find . -name 'node_modules' -type d -prune -print -exec rm -rf '{}' \;
Enter fullscreen mode Exit fullscreen mode

Windows:

$ cd documents
$ FOR /d /r . %d in (node_modules) DO @IF EXIST "%d" rm -rf "%d"
Enter fullscreen mode Exit fullscreen mode

Powershell Users:

Get-ChildItem -Path "." -Include "node_modules" -Recurse -Directory | Remove-Item -Recurse -Force
Enter fullscreen mode Exit fullscreen mode

There we have it!

For me personally, this typically clears out about 40-60GB from my hard-drive, but your mileage may vary!

In Conclusion

  • Make sure to list all node_modules in a given directory BEFORE deleting them.
  • Make sure to be cautious as this process is irreversible!
  • Remember to npm install on the projects you need to work on again.
  • Enjoy the free space! :)

Comment below I'd love to hear how much you saved!

Also, don't forget to check out the Trilon Blog for more in-depth articles!

Oldest comments (48)

Collapse
 
rhymes profile image
rhymes

Thanks!

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

find . -name "node_modules" -type d -prune | xargs du -chs
Enter fullscreen mode Exit fullscreen mode

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

Collapse
 
markpieszak profile image
Mark Pieszak

I like it !! Let me update the article with this in there. Definitely helpful 🙌

Collapse
 
carlillo profile image
Carlos Caballero

Thanks Mark!

Collapse
 
woubuc profile image
Wouter • Edited

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).

Collapse
 
markpieszak profile image
Mark Pieszak

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 !

Collapse
 
openspeedtest profile image
OpenSpeedtest.com

Perfect!

Collapse
 
markpieszak profile image
Mark Pieszak

🙌🙏🙏

Collapse
 
askjkrishna profile image
Jaya Krishna Namburu

Much needed !!

Collapse
 
markpieszak profile image
Mark Pieszak

How much space did you end up saving? 🙌

Collapse
 
askjkrishna profile image
Jaya Krishna Namburu

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

Thread Thread
 
markpieszak profile image
Mark Pieszak

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 😂

Thread Thread
 
askjkrishna profile image
Jaya Krishna Namburu

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

Thread Thread
 
markpieszak profile image
Mark Pieszak

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 !!

Thread Thread
 
askjkrishna profile image
Jaya Krishna Namburu

Thank you, so much 😄 Would love to learn and built it 😊

Collapse
 
kp profile image
KP

@markpieszak you're a lifesaver.

Collapse
 
markpieszak profile image
Mark Pieszak

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

Collapse
 
kp profile image
KP

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

Thread Thread
 
markpieszak profile image
Mark Pieszak

That's great!
Glad to hear it :)

Collapse
 
ruslangonzalez profile image
Ruslan Gonzalez

Such a great idea! Thanks.

Collapse
 
markpieszak profile image
Mark Pieszak

Appreciate the kind words ! Hope it cleared out plenty of space for you! 🙌

Collapse
 
renesansz profile image
Rene Padillo 🇵🇭

awesome! thanks for sharing! ❤

Collapse
 
markpieszak profile image
Mark Pieszak

Of course! More related tips and tricks soon ! 🙏

Collapse
 
peterwitham profile image
Peter Witham

Fantastic Tip, thank you!

Saved .... 1.7gb

Collapse
 
markpieszak profile image
Mark Pieszak

Glad to hear it!! :)

Collapse
 
thatkazuki profile image
Desmond Edem

1.9GB

Collapse
 
markpieszak profile image
Mark Pieszak

Excellent !! 🥇🤘

Collapse
 
jdnichollsc profile image
J.D Nicholls

Ohh my God, 21gb!!

Collapse
 
zenmumbler profile image
zenmumbler

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.

Collapse
 
markpieszak profile image
Mark Pieszak

Great idea! I’ll have to do add notes about pnpm in there for folks to use in the future! Thanks for reminding me 🙌🙌

Collapse
 
scrabill profile image
Shannon Crabill

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

Today I cleared 697M in node_module folders.

Collapse
 
markpieszak profile image
Mark Pieszak

🙌🙌🙌🎸

Collapse
 
kensixx profile image
Ken Flake

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.

Collapse
 
markpieszak profile image
Mark Pieszak

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!

Collapse
 
gnuchu profile image
John Griffiths

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.

Collapse
 
markpieszak profile image
Mark Pieszak

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

Collapse
 
gnuchu profile image
John Griffiths

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.

Collapse
 
canastro profile image
Ricardo Canastro

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.

Collapse
 
anthonybrown profile image
Tony Brown

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

Collapse
 
markpieszak profile image
Mark Pieszak

Happy to hear it !!! :)

Collapse
 
sebtoombs profile image
Seb

If anyone else wants to know how to delete node_modules folders that haven't been used recently, modify the find command to check the mtime (modified time) of the folder. I use this to only delete node_modules older than 30 days like so;

Delete node_modules older than 30 days;

find . -name "node_modules" -type d -prune -print -mtime +30 | xargs du -chs
Enter fullscreen mode Exit fullscreen mode

The -mtime flag can be configured other ways, e.g. to get all files modified today pass -1 instead.

Collapse
 
markpieszak profile image
Mark Pieszak

WOW I love this !!!! This is an amazing addition :)
Adding this to my bash profile immediately - this is basically what I've always needed!

Great find Seb !