How to Delete ALL node_modules folders on your machine and Free up HD space!

Mark Pieszak
3 min readAug 25, 2019

--

Originally published on the Trilon Blog on Aug 7, 2019.

Dreaded “out of HD space” message on MAC

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?

A single node_modules folder can take up anywhere from 200+ megabytes of space.

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!

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 also show us how much space the folder is occupying as well!

$ cd documents 
$ find . -name "node_modules" -type d -prune | xargs du -chs
--- Example output ---255M ./Github/Trilon.io/node_modules
482M ./Github/Some_Demo/node_modules
707M total

Windows:

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

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 -exec rm -rf '{}' +

Windows:

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

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

Trilon — Next-level Application Consulting

Trilon.io is a software consulting firm aimed to help get teams & applications up to speed by working with some of the leading industry experts in JavaScript, NodeJS, NestJS Framework & ASP.NET.

Contact us:
hello@trilon.io

Website:
Trilon.io

Trilon Consulting — Next-level Application Consulting https://trilon.io

Originally published at https://trilon.io on August 7, 2019.

--

--

Mark Pieszak

Trilon Co-Founder (Trilon.io) — Next level Consulting from Open-source fanatics and key contributors. >> Angular Universal core team >> NestJS Core Team