How to delete all node_modules folders (macOS, Linux and Windows)

Copy-paste commands to find, measure and delete every node_modules folder under a directory, plus the safety checks that keep you from deleting the wrong thing.

Nov 1, 20262 min read

package.json plate: the project markernode_modules being removedBox three has no marker, so its folder staysA gantry picker over five project boxes. At each one it checks for the package.json plate before lifting node_modules into the bin. The third box has a folder with the right name but no marker, so it is skipped. That check is the difference between a careful cleanup and find -exec rm -rf.

node_modules folders are rebuildable: delete one and npm install (or pnpm install, or yarn) puts it back. That makes them the easiest disk space to reclaim on a developer machine. Here’s how to do it for every project at once, in three steps: list, measure, then delete.

Step 1: list them

Always look before you delete. Run these from the folder that holds your projects, not from / or your whole home folder on the first try.

find . -name node_modules -type d -prune -print

-prune matters: it stops find from descending into a node_modules it has already found, so you get only the top-level folder for each project, and the command finishes far sooner.

Step 2: measure them

find . -name node_modules -type d -prune -exec du -sh {} + | sort -h

The biggest folders are usually old projects using heavy frameworks, or monorepos. That’s where to start.

Step 3: delete them

find . -name node_modules -type d -prune -exec rm -rf {} +

On Windows, also read why deleting node_modules is slow on Windows.

What’s safe to delete, and what isn’t

The blind find … rm -rf approach treats every folder named node_modules the same. Most are safe, but not every folder with a familiar name is generated, and not every generated folder is worth deleting.

Safe to delete? Tap to find out

A few things to watch for:

  • Global installs. Tools installed with npm install -g live in a node_modules folder too (for example under /usr/local/lib or your Node version manager’s directory). Deleting it breaks those CLIs. Don’t run the command from / or your Node install folder.
  • Apps that ship a node_modules. Some desktop apps and extensions bundle a node_modules folder that they need to run. Keep the search inside your projects folder.
  • Locally patched dependencies. If you edited a file inside node_modules (or use a patching tool that hasn’t been committed), it’s gone after deletion.
  • Lockfiles stay. Don’t delete package-lock.json, pnpm-lock.yaml or yarn.lock. They’re tiny and they’re what makes the reinstall reproducible.

Doing it with a review step

This is the workflow Gigaflush is built around. It looks for node_modules only when there’s a package.json next to it, lists each one with its size, preselects nothing, and shows the full paths plus how to rebuild before anything is removed. It also covers Rust target, Flutter build and .dart_tool, Python tool caches, Swift .build, and Gradle and Maven output, so one scan finds everything.

Getting a project back

When you return to a project, reinstall from the lockfile:

npm ci        # npm, exact versions from package-lock.json
pnpm install  # pnpm
yarn install  # Yarn

That’s it: the folder you deleted was always a cache of what those commands produce.

Keep reading