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

ToolCacheClear it withWhat it costs
Swift Package Manager~/Library/Caches/org.swift.swiftpmswift package purge-cachePackages are fetched again on the next resolve.
Xcode's packagesSourcePackages in each project's DerivedDataFile › Packages › Reset Package Caches, with the project openXcode resolves and downloads that project's packages again.
CocoaPods~/Library/Caches/CocoaPodspod cache clean --allPods download again on the next pod install.
Carthage~/Library/Caches/org.carthage.CarthageKitMove the folder to the TrashDependencies are cloned and built again on the next bootstrap.
Homebrewbrew --cache, usually ~/Library/Caches/Homebrewbrew cleanup --prune=allOld downloads and old versions of installed formulae go. Reinstalling one downloads it again.
npm~/.npmnpm cache verify for unneeded data, or npm cache clean --force for all of itThe next install downloads again.
pnpmpnpm store path, usually ~/Library/pnpm/storepnpm store pruneRemoves 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

  1. Run each tool's own command first. It knows which parts are in use.
  2. Then clear node_modules in projects you haven't touched for a month or more.
  3. 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.