Find and delete old node_modules folders on a Mac without breaking a project

How to list every node_modules folder by size, check that a project can be reinstalled before deleting it, and clean the npm, pnpm and Yarn caches.

A node_modules folder is fully described by its lockfile, so in principle you can always delete it and reinstall. In practice three things go wrong: the lockfile was never committed, someone patched a package in place, or the registry no longer serves a version you need.

List them by size

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

Replace ~/dev with wherever your projects live. -prune stops find from descending into nested node_modules.

Check before you delete

cd path/to/project
git status --short            # uncommitted work?
git ls-files --error-unmatch package-lock.json   # is the lockfile tracked?
git status --short --ignored node_modules | head  # anything unexpected?

Delete and restore

rm -rf node_modules
npm ci        # or: pnpm install --frozen-lockfile / yarn install --immutable

The caches behind them

npm cache verify            # checks and compacts ~/.npm/_cacache
npm cache clean --force     # removes it entirely
pnpm store prune            # removes packages no project references
yarn cache clean

Prefer pnpm store prune to deleting the pnpm store folder: the store mixes packages that projects still link to with ones nobody references, and only pnpm can tell them apart.

How Siftaway handles this

The Project Artifacts page finds node_modules across your repositories and shows, per repo, the branch, uncommitted and unpushed work, stashes, and whether the lockfile is intact. Repos that fail those checks cannot be selected. Selected folders go to the Trash.