How to delete ALL node_modules folders on your machine and free up HD space!
Tags: showdev, tutorial, productivity, beginners
Published: 2019-08-07 18:16:01

> Originally published on the **[Trilon Blog](https://trilon.io/blog/how-to-delete-all-nodemodules-recursively)** 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?

![](https://thepracticaldev.s3.amazonaws.com/i/cnorkdsd8nmcse1erid7.png)
<figcaption>The dreaded "out of space" message</figcaption>

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!

{% twitter 1159136343559155712 %}


## 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](https://dev.to/rhymes/comment/dpci) for the tip!

```bash
$ 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
```

### Windows:

```bash
$ 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.

<blockquote class="alternate"> <strong>WARNING:</strong> This process is irreversible!</blockquote>

### Mac / Linux:

```bash
$ cd documents
$ find . -name 'node_modules' -type d -prune -print -exec rm -rf '{}' \;
```

### Windows:

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

#### Powershell Users:

```bash
Get-ChildItem -Path "." -Include "node_modules" -Recurse -Directory | Remove-Item -Recurse -Force
```

### 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](https://trilon.io/blog) for more in-depth articles!