Guides · Packages
Clear SwiftPM, CocoaPods, Carthage, Homebrew and npm caches on a Mac
Every package manager keeps a copy of what it downloads, so the next install is faster. Most of them never shrink on their own. Here is where each one lives, the command that clears it, and what that costs you.
Short answerAll of these caches are safe to clear; the cost is downloading again on the next install. Use each tool's own command where it has one: swift package purge-cache, pod cache clean --all, brew cleanup, npm cache clean --force and pnpm store prune. Move Carthage's cache folder to the Trash. Old node_modules folders in your projects are often bigger than all of these together.
First, measure them
du -sh ~/Library/Caches/{org.swift.swiftpm,CocoaPods,org.carthage.CarthageKit,Homebrew} ~/.npm "$(pnpm store path)" 2>/dev/null
Folders for tools you don't use simply don't show up. On the Mac this guide was written on, npm's cache was the biggest at 2.4 GB, then pnpm's store at 1.3 GB. The SwiftPM cache was tiny, because Xcode keeps each project's packages in its DerivedData instead.
Each cache, and how to clear it
| Tool | Cache | Clear it with | What it costs |
|---|---|---|---|
| Swift Package Manager | ~/Library/Caches/org.swift.swiftpm | swift package purge-cache | Packages are fetched again on the next resolve. |
| Xcode's packages | SourcePackages in each project's DerivedData | File › Packages › Reset Package Caches, with the project open | Xcode resolves and downloads that project's packages again. |
| CocoaPods | ~/Library/Caches/CocoaPods | pod cache clean --all | Pods download again on the next pod install. |
| Carthage | ~/Library/Caches/org.carthage.CarthageKit | Move the folder to the Trash | Dependencies are cloned and built again on the next bootstrap. |
| Homebrew | brew --cache, usually ~/Library/Caches/Homebrew | brew cleanup --prune=all | Old downloads and old versions of installed formulae go. Reinstalling one downloads it again. |
| npm | ~/.npm | npm cache verify for unneeded data, or npm cache clean --force for all of it | The next install downloads again. |
| pnpm | pnpm store path, usually ~/Library/pnpm/store | pnpm store prune | Removes packages no project uses. Installs that need them download again. |
Two of these have a preview: brew cleanup -n lists what it would remove without removing anything, and npm cache verify only removes data npm no longer needs. npm's own documentation says clearing its cache is rarely needed for anything except space, which is why clean asks for --force.
For pnpm, prefer pnpm store prune to deleting the store folder. Your projects' node_modules share files with the store, so deleting it frees less than its size suggests, and pnpm has to fill it again.
Homebrew also cleans up by itself from time to time and removes downloads older than 120 days when it does, so its cache is rarely the big one.
Old node_modules folders
The caches above are one per tool. node_modules is one per project, and on a Mac with many cloned projects it is usually the bigger problem: each project you ran npm install in once has its own full copy. To list them by size:
find ~/Code -type d -name node_modules -prune -exec du -sh {} + | sort -h
Replace ~/Code with the folder you keep projects in. -prune stops find from going inside each node_modules, so nested ones aren't counted twice. Move the ones for projects you aren't working on to the Trash; npm install, npm ci or pnpm install brings them back from the lock file.
The same goes for CocoaPods' Pods folder and SwiftPM's .build folder inside projects: pod install and swift build make them again.
A safe order
- Run each tool's own command first. It knows which parts are in use.
- Then clear
node_modulesin projects you haven't touched for a month or more. - Use the Trash, not
rm -rf, for anything you remove by hand, and empty it once your projects build again.
Package caches and old node_modules in one list
DevSweep finds the SwiftPM, CocoaPods, Carthage and Homebrew caches, and the node_modules folders (and, if you like, Pods and .build) in project folders you choose, once the project hasn't changed for 30 days. Each item says which command brings it back. What you pick goes to the Trash, and History can put it back.