How to Free up Space on Mac OS ?

ยท ยท 795 words ยท 4 minute read

We all panic when we see the message your startup disk is almost full, or your disk is almost full. save space by optimizing storage. So let me tell you how to free up a good space in your mac, starting from obvious ways reaching to advanced methods to gain more storage space.

Empty the trash can ๐Ÿ”—

Empty the general trash manually. Then you can use this command to delete the trash of the current user.

sudo rm -rf ~/.Trash/*

Unwanted programs ๐Ÿ”—

Delete unwanted programs and apps installed on your mac OS.

Unwanted files ๐Ÿ”—

Delete unwanted files in your download directory which is ~/Download/.

Unwanted videos ๐Ÿ”—

Delete unwanted movies and videos in ~/Movies/ and other directories.

Unwanted audio files ๐Ÿ”—

Delete unwanted music in ~/Music/ and other directories.

Unwanted documents ๐Ÿ”—

Delete unwanted documents and files located in ~/Documents/.

Unwanted screenshots ๐Ÿ”—

Delete unwanted screenshots and screencasts if you have any.

Unwanted system images ๐Ÿ”—

Delete unwanted system images of android in ~/Library/android/.

Unwanted apps and packages ๐Ÿ”—

HomeBrew ๐Ÿ”—

Delete unwanted programs installed by Homebrew. use brew list to list all installed apps, and use brew uninstall <program-name> to uninstall any unwanted program.

MacPorts ๐Ÿ”—

Delete unwanted programs installed by MacPorts. use port installed to show a list of all installed programs, and use port uninstall <some-program> to uninstall any unwanted program.

libs & programs ๐Ÿ”—

  • delete projects, library and programs installed by Python’s pip and pip3.
  • delete projects, library and programs installed by NodeJS’s npm.
  • delete projects, library and programs installed by deno.
  • delete unwanted hidden cache files of ~/.pip , ~/.android , ~/.composer , ~/.tesseract , ~/.npm , ~/.gradle , ~/.cocoapods , ~/.matplotlib , ~/.local , ~/.docker , ~/.macports and other hidden cache directories which are located in the home directory (/System/Volumes/Data/home or /home or just ~).

Remove Old XCode Simulators ๐Ÿ”—

As with Android the major culprit will be old simulators that take alot of space. The simulator files are by default located in ~/Library/Developer/Xcode/iOS/Device Support folder. You can delete old simulators using the following command.

xcrun simctl delete unavailable

Remove Cocoapod Caches ๐Ÿ”—

If you use Cocoapods in your projects you can clear them using this command. Donโ€™t worry you can rebuild them later again.

rm -rf "${HOME}/Library/Caches/CocoaPods"

Clean up Archives, Logs and DerivedData folders ๐Ÿ”—

Folders like DerivedData can take up a significant amount of space too. DerivedData is generated during each build process. This contains intermediate build results,indexes that speed up time on each build. You can use the commands below to clear all that data.

rm -rf ~/Library/Developer/Xcode/Archives
rm -rf ~/Library/Developer/Xcode/DerivedData
rm -rf ~/Library/Developer/Xcode/iOS\ Device\ Logs/

clean up Gradle ๐Ÿ”—

Your Gradle Home directory contains wrapper, caches and daemons files. The more projects with diffferent gradle versions the more subdirectories within those three folders. You can delete all three directories. This saved me a cool ~10GB of space.

Just use these two commands.

cd ~/.gradle
rm -rf caches daemon wrapper

clean up Android SDK ๐Ÿ”—

Android System images are only used by emulators. If you use a real Android device during development for debugging, you no longer need them, so you can remove them all. This could easily save you 5GB doing this.

clean unwanted android sdk

Logs ๐Ÿ”—

Logs are the logging of events of programs and apps on your mac os. Old logs are obsolete. This command will delete all logs everywhere in your mac os.

find ~/ -type f 2> /dev/null | grep '\.log$' | sed 's/[[:space:]]/\\\ /g' | xargs rm

remove cache (temporary saved files) ๐Ÿ”—

Delete all caches.

rm -rf ~/Library/Caches/*
sudo rm -rf /private/var/folders/*

delete cached fonts ๐Ÿ”—

Delete the font cache and database.

sudo atsutil databases -remove

remove all cached DNS records ๐Ÿ”—

sudo dscacheutil -flushcache && sudo killall -HUP mDNSResponder

remove thumbnails of Apple’s quicklook ๐Ÿ”—

rm -rf $TMPDIR/../C/

remove old Homebrew packages ๐Ÿ”—

Clean up old and obsolete packages/files.

brew cleanup

other helpful commands ๐Ÿ”—

Get all directories sorted by size

du -h -d 1 2> /dev/null | sort -hr

Get all files & directories, sorted by size

ls -AhlS

Get all files which have sizes more than 500MB.

find / -type f -size +500M 2> /dev/null | xargs du -h 2> /dev/null

Get all error log files ending with .err.

find / -type f 2> /dev/null | grep '\.err$' | xargs du -h 2> /dev/null

You can empty out the files instead of deleting them by this command.

echo "" > /usr/local/var/mysql/mbp.local.err

But if you still want to delete a file, use this command.

rm /usr/local/var/mysql/mbp.local.err

Future plan ๐Ÿ”—

I have a plan to create a program or script to automatically do these things to clean up mac os files and directories and remove obsolete and redundant files. Here is the github repo for the project .