How to find and clean node_modules and build folders on a Mac
Find the biggest node_modules and build folders, check that each one can be rebuilt, and move them to the Trash instead of rm -rf.
6 min read · Updated October 9, 2026
Every JavaScript project has a node_modules, every Rust project a target, every Next.js app a .next. Coding agents multiply them: each worktree an agent sets up gets its own install and its own build. These folders can all be rebuilt, which makes them the first thing to clean. The trouble is making sure a folder really is one of them.
Find them
These commands only read. Replace ~/projects with the folder that holds your code.
# Every node_modules, without looking inside nested ones
find ~/projects -type d -name node_modules -prune -print
# The same, with sizes, biggest last
find ~/projects -type d -name node_modules -prune -exec du -sh {} + 2>/dev/null | sort -h
-prune stops find from walking into each node_modules, which keeps it fast and skips the packages nested inside.
Other build folders, in one pass:
find ~/projects -type d \( -name node_modules -o -name .next -o -name .turbo \
-o -name target -o -name .venv -o -name DerivedData \) -prune -print
To find projects you haven't touched in a while, check when the project's package.json last changed:
# node_modules whose project's package.json hasn't changed in 90 days
find ~/projects -type f -name package.json -mtime +90 -not -path '*/node_modules/*' \
-exec sh -c '[ -d "$(dirname "$1")/node_modules" ] && echo "$(dirname "$1")/node_modules"' _ {} \;
What can be rebuilt
| Folder | How it comes back |
|---|---|
node_modules |
Reinstall with your package manager |
.next |
next build or next dev rebuilds it |
.turbo |
Turborepo rebuilds it |
target |
Cargo rebuilds it |
.venv |
Recreate the virtual environment |
DerivedData |
Xcode rebuilds it; the next build takes longer |
dist, build, vendor |
Sometimes generated, sometimes written by hand. Check first. |
vendor deserves extra care: vendored code is sometimes edited by hand, and some projects commit it.
Why a name isn't enough
A folder called node_modules is almost always installed packages, but not always. A dist folder might be generated output, or the only copy of something you made. Before deleting, check three things:
1. The project file beside it. A real node_modules has a package.json next to it. This lists the ones that don't:
find ~/projects -type d -name node_modules -prune \
-exec sh -c '[ -f "$(dirname "$1")/package.json" ] || echo "no package.json: $1"' _ {} \;
For target, Cargo leaves a CACHEDIR.TAG file inside; a Python .venv has a pyvenv.cfg inside.
2. Git ignores it. A build folder should be ignored by git:
cd ~/projects/my-app
git check-ignore -q node_modules && echo "ignored by git"
3. Git tracks nothing inside it. If this prints anything, the folder holds files someone committed:
git ls-files node_modules | head
Also stop dev servers and agent sessions that work in that project first. A running next dev or a test run is still using these folders.
What deleting really frees
du can overstate what you'll get back. On a Mac with APFS, a copy can share its disk blocks with the file it was cloned from, and du counts the full size of every clone.
pnpm does this. Its store lives in ~/Library/pnpm/store on macOS, and by default it puts packages into node_modules by cloning them from the store on macOS, falling back to hard links. Space shared with the store isn't freed when you delete a node_modules, and space shared with your projects isn't freed when you delete the store. Deleting the store also means pnpm downloads everything again on the next install.
npm and Yarn caches are simpler: npm keeps its cache in ~/.npm/_cacache, Yarn in ~/Library/Caches/Yarn, and both download packages again when needed.
Move them to the Trash
Use the Trash rather than rm -rf. If you deleted the wrong folder, you can still get it back. macOS 15 and later include a trash command:
# Moves to the Trash
trash ~/projects/old-app/node_modules ~/projects/old-app/.next
On older macOS versions, ask Finder to do it:
# Moves to the Trash
osascript -e 'tell application "Finder" to delete POSIX file "/Users/you/projects/old-app/node_modules"'
The space comes back when you empty the Trash. When you return to the project, reinstall:
npm install # or: pnpm install, yarn install, bun install
Doing it with Devbroom
Devbroom finds build folders in the folders you choose and gives each one a verdict with its reasons. Scanning only reads.
- A name isn't enough.
node_modulesis Ready only with apackage.jsonbeside it,.nextwith a Next.js config orpackage.json,.turbowith aturbo.jsonorpackage.json,targetwith Cargo'sCACHEDIR.TAGinside,.venvwith apyvenv.cfginside.dist,buildandvendoralso need a build script that produces them. Without its marker a folder stays Review. - Git must agree. The folder must be one git ignores, with no file inside that git tracks.
- Hands off running work. A process running from the folder or a file held open makes it Blocked; an agent session running in the project makes it Review.
- Real size. Space shared through hard links and APFS clones isn't counted, so the number is what deleting frees.
- Developer caches such as the npm, Yarn and pnpm caches are found too. The pnpm store stops at Review, because projects share files with it.
You can also do it from the terminal. The command line is free:
devbroom ~/projects --details # scan only, with every reason
devbroom clean ~/projects --dry-run # show the plan, change nothing
devbroom clean ~/projects # show, ask, then move to the Trash
Everything cleaned goes to the Trash and shows up in History, where Put back returns it. More in Rules and Command line, or download the app.
Related
- What Ready, Review and Blocked mean for a build folder: Verdicts.
- Package manager caches outside your projects: How to clean the pnpm store on a Mac, and the developer caches Devbroom lists in Getting started.
- Worktrees bring their own
node_modules: How to remove git worktrees left by coding agents, safely. - Coding agents make many of these folders: see what each one keeps in Agents.
More guides
- How to clear Xcode DerivedData on a MacWhat DerivedData holds, how to see which project uses the most, Clean Build Folder versus deleting, and the simulators that take even more.
- How to clean the pnpm store on a MacFind the store, spot the folder an old pnpm version left behind, prune what no project uses, and know why freed space can be less than du says.
- Where coding agents keep their data on a MacA folder-by-folder map of Claude Code, Codex, Cursor, OpenCode and Orca: what each folder holds and whether to clean it.
- How to free up disk space taken by Claude Code on a MacTranscripts, checkpoints, scratch folders and worktrees: what Claude Code keeps, what you can delete and what you should leave alone.