Mac maintenance
Mac Terminal Commands for Cleanup (the Useful Ones)
Real Terminal commands for Mac cleanup, with safe usage notes and what each one does. Skip the dangerous Stack Overflow snippets.
The Terminal can clean things the GUI can’t reach — log files in /private/var, Time Machine local snapshots, Docker volumes, Xcode caches with predictable paths. But there’s a real safety gap between “useful command” and “command that bricks your Mac.” This list sticks to commands that are safe, well-documented, and actually do something the GUI can’t.
Before anything else
Open Terminal (Applications, Utilities, Terminal, or Cmd-Space then “terminal”). Every command below is run as your normal user. Anything that needs sudo will be flagged. The rule: never paste a sudo command from the internet without reading what it does first.
If you want a paper trail, type script ~/Desktop/cleanup-log.txt first. Everything you do gets logged to that file until you type exit.
Free up disk space immediately
du -sh ~/Downloads ~/Desktop ~/Movies
Tells you the size of each folder. Replace with any folder you want to measure.
du -sh ~/Library/Caches/* | sort -h
Lists every cache subfolder by size, smallest to largest. Look at the bottom — that’s your worst offender.
ls -lhS ~/Downloads | head -20
Top 20 largest files in Downloads. The S flag sorts by size. Now you can decide what to delete with rm.
Time Machine local snapshots
macOS keeps Time Machine snapshots on your local drive even when your backup disk isn’t connected. They show as “Purgeable” space but consume real bytes.
tmutil listlocalsnapshots /
Lists every local snapshot. Each one looks like com.apple.TimeMachine.2025-09-20-130045.local.
tmutil deletelocalsnapshots 2025-09-20-130045
Deletes a specific one. Don’t delete the most recent — it’s actively used by Time Machine.
To free a chunk of space at once:
sudo tmutil thinlocalsnapshots / 50000000000 4
That tells macOS to free up to 50 GB of snapshots aggressively. Safe — it only removes snapshots, not your data.
Xcode garbage
rm -rf ~/Library/Developer/Xcode/DerivedData/*
DerivedData is build cache. Safe to delete entirely. Xcode rebuilds it on next compile. Reclaims 30-100 GB on most developer machines.
rm -rf ~/Library/Developer/Xcode/Archives/*
Old app archives. Only delete if you’ve already submitted these builds and don’t need them for symbolication.
xcrun simctl delete unavailable
Removes simulator devices for SDKs you no longer have installed. Reclaims 10-30 GB.
xcrun simctl --set previews delete all
Removes the SwiftUI Preview simulator data. Often hidden but can be 10+ GB.
Docker
docker system prune -a --volumes
Deletes unused images, containers, and volumes. Often reclaims 30-80 GB. Run it monthly if you Docker daily.
docker volume prune
Just volumes. Less aggressive.
Browser caches
rm -rf ~/Library/Caches/com.apple.Safari/*
rm -rf ~/Library/Caches/Google/Chrome/*
rm -rf ~/Library/Caches/com.brave.Browser/*
rm -rf ~/Library/Caches/company.thebrowser.Browser/*
Quit the browser first. These commands are safe — caches regenerate.
The unsafe version that you’ll see online: rm -rf ~/Library/Safari/*. Don’t. That deletes your bookmarks and history.
System logs
sudo rm -rf /private/var/log/*.log
sudo rm -rf /private/var/log/asl/*.asl
Old system logs. Safe to delete. macOS regenerates the structure on next boot. Reclaims 1-5 GB on Macs that have been running for a year+.
sudo log erase --all
Wipes the unified log database. Modern macOS logs go through this system, not files.
User caches you can clear
rm -rf ~/Library/Caches/*
Wipes every user-level cache. Apps regenerate what they need. Safe but you’ll see a brief slowdown for the first few minutes after as caches rebuild.
The granular version, by app:
rm -rf ~/Library/Caches/com.spotify.client/*
rm -rf ~/Library/Caches/com.tinyspeck.slackmacgap/*
rm -rf ~/Library/Caches/com.adobe.*/Cache/*
sqlite3 ~/Library/Mail/V10/MailData/Envelope\ Index 'VACUUM;'
Compacts the Mail database. The V10 may be V9 or V11 depending on your macOS version — ls ~/Library/Mail/ shows the current one.
Quit Mail first. Vacuuming a live database corrupts it.
iOS device backups
ls -lh ~/Library/Application\ Support/MobileSync/Backup/
Shows iPhone/iPad backups. Each can be 50-200 GB. Delete old ones:
rm -rf ~/Library/Application\ Support/MobileSync/Backup/00008101-001A1234567890123456
Use the actual folder name from the ls output. Make sure it’s not your current device — check the modification date.
Find big files anywhere
find ~/ -type f -size +500M 2>/dev/null
Lists every file larger than 500 MB in your home folder. The 2>/dev/null hides permission-denied errors for unreadable folders. Replace 500M with 1G for files over a gigabyte.
find ~/ -type f -size +500M -mtime +180 2>/dev/null
Same, but only files older than 180 days — the ones you’ve forgotten about.
Hidden ds_store files
find ~/ -name ".DS_Store" -delete 2>/dev/null
Removes Finder’s per-folder metadata files. macOS regenerates them. Useful before sharing folders with non-Mac users or zipping a project.
To stop them appearing on network drives:
defaults write com.apple.desktopservices DSDontWriteNetworkStores -bool true
Spotlight reindex
sudo mdutil -E /
Erases the Spotlight index for the boot volume and rebuilds it. Useful when search returns no results or stale results.
sudo mdutil -i off /
sudo mdutil -i on /
Disables and re-enables indexing. Same effect as the Spotlight Privacy folder trick in System Settings, but faster.
Purge inactive memory
sudo purge
Forces macOS to flush inactive memory caches. Almost never necessary on Apple silicon — the memory manager handles it well. On older Intel Macs that show high “memory pressure,” it can give a temporary boost.
Empty Trash from the command line
rm -rf ~/.Trash/*
Faster than the Finder dialog when there are tens of thousands of files. The Finder UI struggles with large Trash contents.
Things NOT to run
A short list of commands you’ll see online that are bad ideas:
sudo rm -rf /— obvious. Wipes everythingrm -rf ~— wipes your home foldersudo defaults delete /Library/Preferences/com.apple.loginwindow— locks you out- Anything piping
curldirectly intosh. Read what you’re running first sudo rm -rf /private/var/folders/*— breaks running apps; macOS regenerates these but only after a restart
The pattern: if you don’t know what a command does, run it through man <command> first. If you can’t find a man page, search for the exact command on Apple’s developer documentation or a known Mac admin site, never random pastebins.
When Terminal cleanup makes sense
Terminal is best for:
- Commands the GUI doesn’t expose (Time Machine snapshots, Spotlight reindex, Docker prune)
- Bulk operations across many files
- Repeatable scripts — write once, run quarterly
It’s worse than a GUI cleaner for:
- Knowing what’s safe to delete in cache folders you’ve never seen
- Mapping caches to apps (was
com.tinyspeck.slackmacgapSlack? Yes. Wascom.adobe.headlights.LogTransport2? Also Adobe, but obscure) - Avoiding deletion of files an app actively needs
A maintenance app that knows the Mac filesystem catches the long tail of caches you wouldn’t think to look for. The Terminal commands above are the half-dozen that are still worth doing manually because they’re either too aggressive (Time Machine snapshots) or too specialized (Xcode DerivedData) for general-purpose tools.