Mac maintenance
The Mac Terminal Cleanup Commands Worth Knowing
A power-user list of Mac Terminal cleanup commands that actually free space and speed things up — with what each one does and when to skip it.
You’ve got a 512 GB MacBook Pro that’s been “Macintosh HD — 23 GB available” for three weeks. The Storage pane in System Settings has been recalculating since Tuesday. You don’t want to install another GUI cleaner that pesters you for a subscription — you want the actual commands, what they do, and which ones are safe.
This is that list. Every command here is something I’d run on my own Mac. None of them require disabling SIP. A few need sudo. Where there’s a foot-gun, I’ll tell you.
See what’s actually using your disk
Before you delete anything, find the heavy stuff. The built-in tool for this is du, but it’s painfully slow on a modern SSD with millions of files. Use it on specific folders instead of /:
du -sh ~/Library/Caches/* 2>/dev/null | sort -h
du -sh ~/Library/Containers/* 2>/dev/null | sort -h
du -sh ~/Library/Application\ Support/* 2>/dev/null | sort -h
-h gives human-readable sizes, sort -h sorts them so the biggest are at the bottom. The 2>/dev/null swallows permission errors that don’t matter for a size pass.
For a faster, prettier view, install ncdu from Homebrew:
brew install ncdu
ncdu ~/Library
It’s a TUI you can navigate with arrow keys, and it caches results so you can scan a 200 GB home folder in a few minutes.
Clear user-level caches
~/Library/Caches/ is the big one. Apps drop their throwaway data there, and most of them never clean up after themselves. Slack, Spotify, Chrome, Xcode, and the JetBrains IDEs are usually the worst.
du -sh ~/Library/Caches/* | sort -h | tail -20
That gives you the 20 biggest cache folders. To nuke a specific one safely:
rm -rf ~/Library/Caches/com.spotify.client
Don’t rm -rf ~/Library/Caches as a whole — some apps store small state files there that aren’t strictly caches, and recreating them isn’t always graceful. Pick the ones you recognize.
Empty the system caches that you actually own
Some caches in /Library/Caches/ are user-owned and safe to clear:
sudo rm -rf /Library/Caches/com.apple.iconservices.store
That clears the icon cache, which sometimes goes corrupt and shows generic icons everywhere. After running it, log out and back in.
The font cache is another one:
sudo atsutil databases -remove
If your Mac is randomly forgetting font weights or showing tofu boxes in apps, that’s the fix.
Trim the developer caches
If you’ve ever opened Xcode, you’ve probably got 30–80 GB of cruft hiding in ~/Library/Developer. The biggest offenders:
du -sh ~/Library/Developer/Xcode/DerivedData
du -sh ~/Library/Developer/Xcode/iOS\ DeviceSupport
du -sh ~/Library/Developer/CoreSimulator
DerivedData is rebuildable; nuke it:
rm -rf ~/Library/Developer/Xcode/DerivedData/*
Old simulator runtimes for iOS 15 and 16 are usually safe to remove if you only build for current versions. Use xcrun simctl delete unavailable to drop simulators tied to runtimes you no longer have.
Homebrew accumulates stale downloads:
brew cleanup -s
brew autoremove
-s clears the entire download cache including the latest versions. Skip it if your internet is metered.
Docker and node_modules
If you use Docker Desktop, its disk image grows and never shrinks unless you tell it to:
docker system prune -a --volumes
That removes unused images, networks, build cache, and volumes. The --volumes flag is the aggressive one — read it before agreeing.
For Node projects, node_modules directories are the single biggest source of “where did all my space go” on developer Macs. Find them:
find ~ -name "node_modules" -type d -prune -exec du -sh {} \; 2>/dev/null
-prune stops find from recursing into them, which would take hours. Delete the ones from old projects with rm -rf path/to/node_modules.
Old log files
/var/log and ~/Library/Logs fill up over time, especially if you have a crashing app. Apple rotates the system logs, but the unified log database (~/Library/Logs/DiagnosticReports) keeps every crash report.
du -sh ~/Library/Logs/DiagnosticReports
rm ~/Library/Logs/DiagnosticReports/*
Some apps also keep verbose logs in ~/Library/Application Support/<app>/Logs. Worth checking if a particular app’s been misbehaving.
Clear the Quick Look thumbnail cache
If Finder is slow to preview files, or Quick Look is showing the wrong thumbnail:
qlmanage -r cache
This rebuilds the thumbnail database. No sudo needed.
The DNS cache (when networking acts up)
Not a disk-space command, but it belongs on this list. If a site won’t load and you’ve already restarted Wi-Fi:
sudo dscacheutil -flushcache
sudo killall -HUP mDNSResponder
Run both. The first flushes the directory services cache, the second restarts Bonjour, which handles .local resolution.
Time Machine local snapshots
macOS keeps “local snapshots” of your Time Machine backups on your boot drive when the external drive isn’t connected. These can eat 50+ GB without you noticing. List them:
tmutil listlocalsnapshots /
You’ll see entries like com.apple.TimeMachine.2025-09-12-183022.local. To delete one:
sudo tmutil deletelocalsnapshots 2025-09-12-183022
The system also auto-purges these when free space drops below a threshold, so it’s not strictly cleanup — but if you’re trying to recover space right now, this is a fast win.
Force a Spotlight reindex (when search is broken, not for cleanup)
Not technically a cleanup command, but it lives in the same toolkit:
sudo mdutil -E /
That erases the Spotlight index for the boot volume. macOS rebuilds it in the background — expect 30 minutes to a few hours of fan noise.
Free inactive memory (rarely needed)
sudo purge
This forces inactive memory pages to disk, freeing them for new allocations. On Apple Silicon Macs with unified memory, you almost never need it — the memory compressor handles things gracefully. On older Intel Macs with 8 GB of RAM, it can buy you a few hundred MB before a big compile.
Empty Trash (for real)
rm -rf ~/.Trash/* empties your user trash. But if a file is in your Trash and macOS is refusing to empty it because it’s “in use,” you usually want:
sudo rm -rf ~/.Trash/*
External drives have their own trash at /Volumes/<drive>/.Trashes/501/ (501 is your UID).
What to skip
A few commands floating around the internet that you should not run:
sudo rm -rf /private/var/folders/*— this nukes your in-flight system caches and will brick your current login session. macOS rebuilds it, but you’ll lose unsaved app state.sudo rm -rf /Library/Caches/*(the whole thing) — some daemons keep state here that won’t regenerate cleanly.- Anything that disables SIP “to clean up system files” — there is nothing in
/Systemthat you should be clearing manually on a modern Mac.
When to stop and use a tool
If you’re running these commands often enough that you’ve made aliases for them, you’ve crossed a line. The Terminal approach is great for surgical fixes — “this one cache is corrupt” or “Docker ate 80 GB” — but routine maintenance is better automated. A scanner that catches the long tail of garbage (orphaned login items, leftover preference files from uninstalled apps, broken symlinks, language packs you don’t need) is going to find more than a 20-line shell script.
That’s where Sweep comes in. It does this whole list and a bunch of stuff that’s tedious to script, and it shows you what it’s about to remove before it removes it.
A reasonable cleanup script
If you want to bake the basics into one command, this is what I keep in ~/bin/macclean:
#!/bin/bash
set -e
echo "Clearing user caches..."
find ~/Library/Caches -mindepth 1 -maxdepth 1 -type d \
-exec du -sh {} \; 2>/dev/null
echo "Brew cleanup..."
brew cleanup -s
echo "Pruning Docker..."
docker system prune -f 2>/dev/null || true
echo "DNS flush..."
sudo dscacheutil -flushcache
sudo killall -HUP mDNSResponder
echo "Done."
Run it monthly. Don’t run it daily — most of these caches exist because re-fetching the data is expensive.
The Terminal cleanup approach is satisfying when it works and frustrating when it doesn’t. Knowing which command does what is half the battle; knowing which command not to run is the other half.