Homebrew’s cache is a hidden culprit for gobbling up disk space on your Mac, and it’s usually not immediately obvious how much it’s consuming.
Let’s see it in action. First, we’ll check how much space Homebrew thinks its cache is using.
brew --cache
This command will output the directory where Homebrew stores its downloaded formula and Cask files, typically something like /Users/yourusername/Library/Caches/Homebrew.
Now, let’s get a real, ground-level view of the disk usage for that directory. We’ll use du (disk usage) with the -sh flags: -s for summary and -h for human-readable format.
du -sh /Users/yourusername/Library/Caches/Homebrew
You might be surprised to see numbers like 15G or even 30G staring back at you. That’s gigabytes of downloaded package archives that Homebrew keeps around "just in case" you need to reinstall something or if it needs to build from source.
The problem Homebrew’s cache solves is speed and reliability. When you install a package, Homebrew first downloads the source code or pre-compiled binary. If it keeps these downloads locally, subsequent installations or upgrades of the same package, or even different packages that depend on it, can be significantly faster because it doesn’t need to re-download everything. This also acts as a fallback; if a download source temporarily goes offline, Homebrew might still have a local copy.
Internally, Homebrew manages this cache by downloading files into the Caches/Homebrew directory. When you run brew install <package> or brew upgrade <package>, Homebrew checks this cache first. If the required archive is present and valid, it uses it. If not, it downloads it. The system doesn’t automatically prune this cache aggressively; it tends to accumulate over time as you install and update more software.
The primary lever you control is the brew cleanup command. This is the built-in mechanism for managing the cache.
brew cleanup
When you run brew cleanup, Homebrew iterates through your installed formulae and Casks. It looks at the versions currently installed on your system and compares them to the archives stored in the cache. It then removes any cached archives that are no longer associated with an installed version. This includes older versions of formulae and Casks that have been superseded by newer ones.
Here’s a more targeted approach if you want to be aggressive. You can tell brew cleanup to remove all cached downloads, not just those associated with installed packages. This is useful if you’re trying to reclaim maximum space and are confident you won’t need to reinstall older versions soon.
brew cleanup --prune=all
This command will delete every archive file found within the Caches/Homebrew directory, regardless of whether a package is currently installed. It’s a complete purge of the download cache.
The real magic of brew cleanup is that it’s smart enough to know which files it can safely delete. It doesn’t just blindly remove everything in the cache directory. Instead, it maintains a record of installed packages and their exact versions. When you run brew cleanup, it consults this record and only removes cached archives that correspond to uninstalled or older versions of packages. This prevents it from deleting the archive for the version you are actively using, ensuring that a subsequent brew reinstall of your current version won’t trigger another download.
After running brew cleanup --prune=all, you should re-run the du -sh command to verify the space reclaimed.
du -sh /Users/yourusername/Library/Caches/Homebrew
You’ll likely see a much smaller number, indicating that gigabytes of space have been freed up.
The next thing you’ll likely want to explore is how Homebrew manages its formulae and caskroom directories, which can also grow significantly over time with older, unlinked versions of installed software.