How to clean up node_modules and package caches on your Mac
Updated
In short: Package managers, the programs such as npm, pip or Homebrew that download and install software components, keep every download in a cache, a store kept for the next time it is needed. On top of that, every JavaScript project has its own node_modules folder with all the packages it needs. Both can be downloaded again, so you can clean them up; the next install then takes longer. The biggest wins are usually the node_modules of idle projects, old Homebrew versions and unused Docker data. For Homebrew and Docker, use the tools' own commands, not the Finder.
How do I find large node_modules folders?
The quickest way is a command in Terminal, the app for text commands in Applications > Utilities. It lists every node_modules folder in your home folder with its size, the largest at the bottom:
find ~ -type d -name node_modules -prune -exec du -sh {} + 2>/dev/null | sort -h
The command only reads and can take a few minutes when there are many files. Thanks to -prune it does not search inside a node_modules folder it has found, so it does not count the nested folders in it twice. For projects you are not working on, you can delete the node_modules folder. npm install in the project folder restores it; if there is a package-lock.json, npm ci installs exactly the versions recorded in it. If the project uses Yarn or pnpm, run yarn install or pnpm install instead. Never delete package.json or the lock file, because node_modules is rebuilt from them.
Where are the caches of npm, Yarn and pnpm?
- npm stores packages in
~/.npm/_cacache.npm cache verifychecks the cache and removes data that is no longer needed;npm cache clean --forceempties it completely. According to the npm documentation the cache heals itself, so emptying it is only needed to reclaim disk space. npm never removes anything on its own; the cache grows with every new install. - Yarn 1 uses
~/Library/Caches/Yarnon a Mac;yarn cache dirshows the exact location,yarn cache cleanempties it. Yarn 4 keeps its cache in~/.yarn/berry/cacheby default, while Yarn 2 and 3 keep it inside the project in.yarn/cache. - pnpm keeps all packages in one shared store, on a Mac in
~/Library/pnpm/store;pnpm store pathshows the exact location. The files in your projects are clones or hard links of that store and do not take up the space twice. Emptying the store therefore mostly frees what no project uses any more. That is exactly whatpnpm store pruneremoves; according to pnpm, the command has no side effects on your projects.
How do I clean up the caches of pip, Go, Cargo and Gradle?
pip, the package manager of Python, keeps downloads in ~/Library/Caches/pip. pip3 cache info shows the size, pip3 cache purge empties the cache; on some installations the command is called pip instead of pip3. Go keeps its build cache in ~/Library/Caches/go-build, deletes entries that have not been used for five days on its own and empties it completely with go clean -cache. Cargo, the package manager of Rust, stores downloaded packages in ~/.cargo/registry; according to the Cargo documentation any part of it can be removed, and Cargo downloads what is missing again. Gradle keeps dependencies and build caches in ~/.gradle/caches and, according to its documentation, deletes on its own what has not been used for 30 days (7 days for some caches).
How do I clean up Homebrew?
brew cleanup --dry-run shows what Homebrew would remove, without deleting anything. brew cleanup then removes old versions of installed formulae (Homebrew's name for its packages) and outdated downloads; downloads older than 120 days are removed entirely. The currently installed versions stay. brew --cache shows the download cache, by default ~/Library/Caches/Homebrew. Homebrew deletes directly, not to the Trash. After brew install and brew upgrade, Homebrew cleans up the affected packages anyway, and all packages every 30 days.
How do I clean up Docker?
Docker Desktop stores all images (templates that containers are created from), containers (running or stopped instances of them) and volumes (storage for their data) in a single large file, Docker.raw. You can therefore only clean up through Docker itself, never in the Finder. docker system df shows how much space images, containers, volumes and build cache take. docker system prune removes stopped containers, networks no container uses, dangling images (images without a name) and unused build cache, and asks before it does. Careful with the extra options: -a also removes all other images no container uses, and --volumes also deletes anonymous volumes, which may hold the data of a database, for example. What Docker deletes is gone for good.
What should you not do?
- Do not clear caches while an install or a build is running.
- Do not delete
node_modulesshortly before you have to work without internet: restoring it downloads missing packages from the network. - Only remove Docker volumes when you know what is in them, and never delete or move
Docker.rawin the Finder.
Frequently asked questions
Can I simply delete node_modules?
Yes, the folder only holds downloaded packages. npm install, or npm ci if there is a package-lock.json, in the project folder restores it, as long as package.json and the lock file stay in place.
Is it safe to delete the npm cache?
Yes. According to npm, the cache is only a temporary store that repairs itself when something is wrong; it only needs emptying to reclaim disk space. npm downloads missing packages again on the next install.
Does brew cleanup delete my installed programs?
No. It removes old versions of installed formulae, outdated downloads and stale lock files; the current versions stay installed. brew cleanup --dry-run shows beforehand what it would delete.
Why is Docker.raw so huge?
Many tools show the maximum size of this file, not the space it actually takes, as Docker explains in its documentation. ls -klsh shows the space actually used in its first column; you free it with docker system prune, not by deleting the file.
Faster with SpaceBroom
In System Junk, SpaceBroom finds the node_modules of projects with a package.json where no file outside node_modules and hidden folders has changed for more than 90 days, and does not preselect them. System Junk also shows the caches of npm, Yarn, pnpm, pip, Go, Cargo, Gradle, CocoaPods, Swift Package Manager and the JetBrains IDEs with path and size and moves them to the Trash; it does not preselect pnpm, Gradle and JetBrains. For Homebrew and Docker it offers brew cleanup and docker system prune in the Maintenance section, never with -a or --volumes: on request it measures first what would be freed, shows the command in the confirmation dialog and runs it only after you confirm; these commands delete permanently. Xcode and simulators are covered in the guide How to clean up DerivedData, archives and simulators on your Mac.
More guides
- How to free up storage space on your Mac: 9 ways that workMac storage full? See what takes up space and free it up: the Trash, Downloads, large files, unused apps, old device backups and more.
- System Data on your Mac: what it is and how to reduce itSystem Data taking up many gigabytes? What the category holds, what macOS cleans up by itself and what you can safely remove.
- How to uninstall apps on your Mac completely, with no leftoversDragging an app to the Trash leaves settings, caches and background services behind. Where the leftovers are and how to remove them safely.
- How to clean up DerivedData, archives and simulators on your MacXcode DerivedData, archives, device symbols and simulators: where they are, what you can safely delete and what Apple says to keep.
- Is it safe to delete caches on your Mac?Is it safe to clear caches on a Mac? What caches are, where they are stored, what macOS clears by itself and which folders to leave alone.
- How to find large files on your MacVideos, downloads and old archives often take the most space. How to find large files with macOS Storage settings, the Finder or Terminal.
- How to find and remove duplicate files and photos on your MacDuplicate photos in your library, copies in Downloads and Documents: how to spot duplicates reliably, which copy to keep and where deleting frees nothing.