How to delete node_modules safely

Every abandoned side project leaves a node_modules tree. Deleting is safe for projects you are not running today — npm/pnpm/yarn will recreate it — but wipe the wrong folder and you waste a reinstall.

These commands permanently delete files. Most are caches the tool rebuilds on its own (the next build or install is slower and may need internet). Anything that can hold data you cannot re-download is flagged on the page. Read each command, then copy and run it yourself. devdisk never runs cleanup commands for you.

Find the heavy ones

# from your code root
du -sh */node_modules 2>/dev/null | sort -hr | head

Or run devdisk, which groups every package.json’s node_modules and marks them Review first.

Delete one project

cd /path/to/project
rm -rf node_modules
# later:
npm install   # or pnpm i / yarn

Many projects

Prefer a report over a blanket find-delete. Blind find ~ -name node_modules -exec rm -rf {} + can hit tools you still need and is hard to undo.

devdisk builds a commented script for review items so you uncomment only what you mean.

Also clear the package cache

After pruning projects, package managers still keep caches:

npm cache clean --force
pnpm store prune
yarn cache clean

devdisk lists these under Safe to regenerate.

Run free scannerSample report

Want one-click cleanup?

A planned devdisk desktop app for Mac and Windows: live treemap, cleanup that moves files to the Trash, scheduled scans. One email when it launches, with an early-supporter price. Your address is used only for that, and every email has an unsubscribe link.

FAQ

Is deleting node_modules safe?

Yes for projects you are not actively developing. You need a lockfile (or package.json) and network to reinstall.

Why is it so large?

Nested dependencies, optional native binaries, and duplicated trees across projects. pnpm helps; old npm trees do not.

Does devdisk delete for me?

No. Read-only. You copy or download a script and run it yourself after reading every line.

More cleanup guides