How to Free up Disk Space on Mac OS ?

ยท 1192 words ยท 6 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 ~/Downloads/.

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/com.apple.QuickLook.thumbnailcache

remove old Homebrew packages ๐Ÿ”—

Clean up old and obsolete packages/files.

brew cleanup

Here is the result of running this command on my macbook pro.

$ brew cleanup
Removing: /usr/local/Cellar/numpy/1.26.4... (1,708 files, 39.3MB)
Removing: /usr/local/Cellar/svt-av1/2.1.1... (19 files, 5MB)
Removing: /Users/mbp/Library/Caches/Homebrew/svt-av1_bottle_manifest--2.1.1... (7.8KB)
Removing: /Users/mbp/Library/Caches/Homebrew/svt-av1--2.1.1... (1.9MB)
Removing: /Users/mbp/Library/Logs/Homebrew/glib... (64B)
Removing: /Users/mbp/Library/Logs/Homebrew/librsvg... (194B)
Removing: /Users/mbp/Library/Logs/Homebrew/node... (64B)
Removing: /Users/mbp/Library/Logs/Homebrew/gnupg... (64B)
Removing: /Users/mbp/Library/Logs/Homebrew/ruby... (64B)
Pruned 0 symbolic links and 6 directories from /usr/local
Removing: /usr/local/Homebrew/Library/Homebrew/vendor/portable-ruby/3.3.2... (2,391 files, 41MB)
==> This operation has freed approximately 87.3MB of disk space.

As you can see, it freed up around 87MB of disk space.

remove unused docker images ๐Ÿ”—

If you are using docker for your local development, use this command docker image prune to delete obsolete Docker images downloaded on your machine.

$ docker image prune
WARNING! This will remove all dangling images.
Are you sure you want to continue? [y/N] y
Deleted Images:
untagged: redis@sha256:2e9892520a3d5edc5cf7090056c7ab372b4cc26d8ee8108205977235e92a65c8
deleted: sha256:4733b14e6f4cedc8f3a5069939351055aa4faa6c9e0e8d3add0e641c7617d18e
deleted: sha256:df1913e7ee52dcf15a5a74858c2145c8e1903ca5ca621111718333d96bb3d398
deleted: sha256:302eaa329659c891f5137e6eb5d9a7c0e12a2391270e793b66d70bac1ee7d7cb
deleted: sha256:f9cecf9e9161e5eeb01e2f2c02b8690835529abb34c2950f4357165dcf8c400e
deleted: sha256:df16bb6ee1385d259108594853574ccfac6618251524f1f322e677016350aaba
deleted: sha256:94056d6f643e7d20c1c8d0804b78561d7b3854d6abed4f205e0ccec3fd7994cb
deleted: sha256:24302eb7d9085da80f016e7e4ae55417e412fb7e0a8021e95e3b60c67cde557d
untagged: getmeili/meilisearch@sha256:2a8bda1f316d01d908957928e18b276b6fa00c3bd896414901377b6d618c9fdc
deleted: sha256:2d47191720962da801ef14473dafe09a090629c78fe32df8d6746064723dc22e
deleted: sha256:b042ac4ed43132e0049fff0a890967860a879e05ceef260e6b7fd3657574d7b6
deleted: sha256:eee83123ab2c04d600b97f7d07265cf2b8f53cc9ca1cad2ecf4e04a192925c8e
deleted: sha256:945f073e5c96258fc809f0418d509bb9592b50b15aab8a2676cf5b5fe64dc825
deleted: sha256:97615b9b81c02455b3212af58a44e329ac27321c72aaeb33c5fd8409df11a9f2
deleted: sha256:5bc340f6d4f5a3bc999dfbc790a0bdf0920b9103ef794645034de4260ee4e9c8
untagged: selenium/standalone-chrome@sha256:38bb75fb9c8ad82e1eee6f299b612a09d9c874ecde1d776beddc2eb0bdcb0bea
deleted: sha256:1ac7ccac8ba0f009813fe25278de5ba919f10e636d54531ce48c23474c706f2f
deleted: sha256:10f4fb17c48edfbdb79eeaa5ca94cad1e5171fcf520d49f3917be9f40d2e878c
deleted: sha256:76e5589dce9939872963f73cece5e36cdb383978b9e66481af5dfc31aef4b4b8
deleted: sha256:95b0e4f3714edb62960a4e13f8c054c9809dab4ebdcb6115e2890f840028cde9
deleted: sha256:7c5f760d6db19c7ed60038e5a13cdd9073ab816deedd73f3c01f3995b90989e1
deleted: sha256:eb1ec92eafe5e11a6b83bcf6f1a16eb4a5024baabb3460e1e0b3bcf18d250b05
deleted: sha256:9af256269cd1198f665bb3b02d1774c33e30f44d148dfa318b410c44893148b8
deleted: sha256:cbb212757eb29cffb070861b8052e8943c44614e09300436a841abad2929cbfc
deleted: sha256:429928dfade1d2bd23de19ab660209c4a13c8bf33f9baafb4078bd532196b345
deleted: sha256:18e09d185eb907fbf0a557a94f40ec1af021e1172f88eb09ea4e82e5198010fe
deleted: sha256:7a8626a6b7a442273c7fb28d3110a7c15b0fc6b12757a8de7773b25ef5530ea7
deleted: sha256:fa32e7f5caa1ef16ac93377090fcd8bfa0d5c652fde5d673136af4d875e4b27f
deleted: sha256:7a0ac3af95b12ff9e0333fbd2f88184476c46393f8a7aa56dc92990b624f4582
deleted: sha256:a709ca365e9a3bc353573257325a4300805d6325a9b86d1b6687ed1ad53e2c06
deleted: sha256:0d10483ec093adb3258df04ce1c3b77d144a4066de761a59bb5ed9a829bd258d
deleted: sha256:7825aefca979f4a2b534cd99490595e34aa1a93fd79057568bb41592d3213c94
deleted: sha256:8258ae53da65f9c9ae90aa8be858aaf0ea0ad9429358032a03e439b2fe1ee920
deleted: sha256:fe4c97d9a155bcd92e5ec53e92a03faacb5dba97c5c020e2edd19aa7e9e97747
deleted: sha256:87391c707b7c6a71af6dfb4456ae736b5f2b3903ef0516412b5a62e9f5d1d911
deleted: sha256:deeebce90949366d65acaeae1d383682a8ef8ab8eef7c583449502902634e3b3
deleted: sha256:b0e2461905b58934a0fe82667c1b0b8e6ebb401123052fe3847e25fe09e0070a
deleted: sha256:e2162b38474938bdea40a7b5eddcd700a3a1b1c1caf98c258dfea1799477647f
deleted: sha256:df6491f6dc5e0dfb0e5bce9141cdf337cbef4bc36443738a2c8470cb2b6b9c1f
deleted: sha256:7b3085d0361c7dfaed2c497ec242662feeeb7755c3edcf557157835fc11a3f7f
deleted: sha256:5f8d42ca32302df8b1ce68bcbb159682a794379513692b6cc42b0e07261bc2ed
deleted: sha256:8dbe55f747aa65f3b160afab239182b60424bdad700bd1432f9cb61eaf730ad3
deleted: sha256:e8976f47820bbd3d15915fa4aab68cd7f6f9910e4725933c18075b985e811543
deleted: sha256:718613ce031b4a4a4c4a400ae209a75c32b2faa9b4636d514a722dc39c65e437
deleted: sha256:16a3a585d14e65ee438c2c6cc38046c58c83a790a50cfe68fb43324d25ce05d0
deleted: sha256:fb0604cab45a3499ee04c9bc7e3e46e0376bfcef5b57e40f8fbc5ab9b4bb0288
deleted: sha256:9174882773064aea5b8a9d2f6f9969b925dee70bec7e792feaa3ca4c2a7fed2e
deleted: sha256:29487e3552bd1a9d945543310db92d1ff14970d0e5fdc71f486d8367a2fb308b
deleted: sha256:b1dc4377f1515557f30f7feaf168e8316dc75b2b7fd76c36159d0b1317164f26
deleted: sha256:6021993d84a2d9db89ec74cf44366ae3f2d9f9810f3c77b2bd0cd5ddf14825b1
untagged: getmeili/meilisearch@sha256:94bb999a7c7afa81a64872186b553699213614f705824dd73909a2556fd9b132
deleted: sha256:9e8344e03fced960d79f7bc104b72ca99805b9e829ef192b0cdcbde849821779
deleted: sha256:f2f9933444bd21923f1e197f9e4ec0390778049d5c04a192e17dbda1de1374ad
deleted: sha256:438e5e9031a80c9a213723e43bfe61ae111ee7784b2c3c60002707cb6247d78e
deleted: sha256:a2cf4261d9af664cbefa7fe09a10f4ed41b0998f73a1a0e52ec1e0e21c4a0bd9
deleted: sha256:1cce3450bd49ca7bf0bb55efc3753c99704bd51bf9e9b1c148020ea8244c8503
deleted: sha256:b541d28bf3b491aeb424c61353c8c92476ecc2cd603a6c09ee5c2708f1a4b258
untagged: redis@sha256:0859ed47321d2d26a3f53bca47b76fb7970ea2512ca3a379926dc965880e442e
deleted: sha256:1c1b270ed4205d69c4718b9a31a9dfac8565979ec2f404e4a4102cd053cb7b14
deleted: sha256:17e96dc22bb290787876759e528500f564ec7508025b96da34735bf88c1f4c9d
deleted: sha256:97dbe1900eef057918dcce284a6699f1a2a0c4b44d0df26894d73cdb4044029c
deleted: sha256:d3596a5d002d96a3cc27664ea0a09b73d15691423fe1db4db433584803559e7d
deleted: sha256:98b397c310bc428f59901328b34322788f96a7c9955dd95026a5d00dc245d67c
deleted: sha256:8f2b74175c09cdf2332343e83eb52f26e008c5e4f37d55bf3a8c1c632bebffa6
untagged: mysql/mysql-server@sha256:1a8d2a5584e53a42a43cbd430ae340a36942afee9e14a86624a2cb2d90ce655b
deleted: sha256:5a9594052aecd873d2dc606c7148ac0983ebfbe1604eae9a215071a9cda0fb56
deleted: sha256:4e13c6ba21197af1227b5f8756e6f922e2fc78fc4426e5fe29020c3a233e7d65
deleted: sha256:95ce703aaf1ecd17cf4649c8bc7c5c172abb01e81ed0f2dc094558d8683debe0
deleted: sha256:0000b463a5f1349913099b9d1cb538055c3f313836b4a44441aea0f0dd8ea42b
deleted: sha256:00e8ddc2f20e94154458e959b058fa813ee0c61ac4bd72e489482d40339bc06c
deleted: sha256:e2f249a3d350f2ac61d07dac50a5afc7806d5e2c6f4d702634010817e3e66fd4
deleted: sha256:c4b2de44f1992d196584ad41b29ff2868a55598b2c5b5a46739376aa2696eab0
deleted: sha256:d2db75568ee66eaf993c0287d9d570a94ead0b249cdb311d21ff14cd80fd03e4
deleted: sha256:3eca945cf19f1d55bde2a591a10e7a6af1be5ea4d64a385d71a6efeaa02f5965

Total reclaimed space: 2.016GB

That saved me 2GB of storage.

remove unused node_modules ๐Ÿ”—

Use pnpx npkill or npx npkill to list all node_modules, move by arrows keys and use โฃ Spacebar to delete the node_modules you are on. Just use it to remove old projects’ node_modules directory.

Here are my savings on node_modules.

remove obsolete node_modules

I freed up 1.99 GB disk space.

remove yarn cache to get rid of old ones ๐Ÿ”—

Use the command yarn cache clean to remove all cached packages from your computer. So, when you use yarn, the cache will be rebuilt with the wanted latest packages. The disk space saved by this command will vary according to your usage of yarn .

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 .

Share:
waffarx cash back