How I saved 100 GB of disk space on my Mac

Rakesh Arunachalam

Disk-full

As a mobile developer, the dreaded “Your disk is almost full” message on my Mac scares me like cats spotting a cucumber. But fear not! In this post, I’ll share how I managed to reclaim a whopping 100 GB of precious disk space on my MacBook. Although the examples in this post might be specific to data generated during mobile applications development, the strategies outlined for disk space optimisation are universally applicable to developers across various technologies. Let’s dive in!

Big wins matter

Deleting large project build files such as “Xcode developer data” might provide short-term gains, but these won’t last as the space will be used again as soon as we restart development.

Delete Xcode developer data

Similarly, meticulously deleting numerous small files may yield minor gains, but the time invested often outweighs the benefits. Example: By selectively deleting from Documents (screenshot), I might eventually gain 1.5 GB but lost 20 minutes in the process while that disk space warning and the new version of Xcode requiring 27 GB (~13 GB compressed) are still smiling at me.

Small gains

So focus on the big wins that gain maximum disk space first. We can revisit the minor victories later.

Time machine backups

According to Apple, “With Time Machine, you can back up files on your Mac that weren’t part of the macOS installation, such as apps, music, photos and documents.”

Essentially, Time Machine backups provide automatic, incremental backups of a Mac, allowing users to restore their machine’s data to a previous state. While this feature is useful, the backups (local snapshots) can gradually consume significant disk space over time. Therefore, periodically clearing older backups can help reclaim valuable storage space on the disk.

These are some commands to check and manipulate your Time machine backups:

# List snapshots
tmutil listlocalsnapshots /

# Delete snapshots
tmutil deletelocalsnapshots /

# Disable Time machine backups
sudo tmutil disable

# Enable Time machine backups
sudo tmutil enable

Personally, my excitement waned when I discovered that Time machine was already disabled on my MacBook and I ended up freeing no space at the end of this exercise.

Delete Node Modules

As a React Native and JavaScript developer, Node modules are an integral part of my projects. However, if left unchecked, these can occupy a significant amount of disk space. It’s essential to periodically clean up the node_modules directories across all project repositories on our machine.

Node Modules

Delete ALL node_modules is a fantastic post detailing the process of deleting node_modules. The crux is to run the command below to delete node_modules recursively on all the project repos.

# Run in a directory where we have all our code
find . -name 'node_modules' -type d -prune -print -exec rm -rf '{}' \;

There are two parts to this command:

find . -name 'node_modules' -type d -prune -print is safe to run and looks for directories with the name node_modules.

  • -type d only looks for directories ignoring files.
  • -prune is smart and ignores recursive occurences of node_modules in projects and stops at the parent directory.
  • -print prints each result on the screen.

-exec rm -rf '{}' \; executes the rm -rf command on each result of find where {} is replaced by the result. \; terminates the command.

Reclaiming nearly 10 GB of free space with just one command felt as satisfying as a GitHub PR that removes 1000 lines of code.

If you are a Ruby on Rails developer or use technologies that create a lot of log files, running this command might help you find a few large log files that can safely be deleted.

# Run in a directory where we have all our code
du -h ./**/log | sort -rh

Sometimes Homebrew creates unnecessary files and dependencies that need to be cleaned up. Running the cleanup command periodically will help keep the Macbook organised and efficient.

brew cleanup

Finder tips

Storage basics

Apple tends to limit what users can do, and the “Storage” in “System Preferences -> General -> Storage” is no exception. While it appears promising, it has limitations.

  • The “Applications” section allows straightforward app uninstallation.
  • The “Developer” section contains Xcode developer data and as mentioned previously can provide temporary storage gains.
  • For deeper cleaning, the “Documents” section presents various options like Large Files, Downloads, Unsupported Apps, and File Browser. All these four options are useful in their own way.

Storage options

At the end of this exercise, we realise that not much space has been freed. The files in Documents are needed and can’t be deleted.

Advanced Finder

Based on the screenshots, we notice that “System Data” is using a lot of storage (~172 GB) but “Storage” does not show any more details making us wonder if there is a magical way to get more information. Follow these steps to know space usage of “System Data” on the GUI:

  • Restart your Mac and let the OS reindex the file system
  • Open Finder, click on the Go menu, and hold down the Option (⌥) key.
  • This will reveal a hidden “Library” option. Click and choose that option. What we see here is the majority of the items shown as “System Data” by Mac.
  • In the “Library” folder, switch to the “List view” and sort by size.
  • Click on the “View” menu and select “Show View Options”.
  • Select “Calculate All Sizes” and keep this window open throughout this exercise.
  • Use Finder like a file tree and keep opening directories you suspect. If the sizes are not shown, select the “Calculate All Sizes” for each directory.

Warning: Avoid making changes to the “Library” folder unless you’re well-versed as a developer. Modifying this folder without proper understanding can lead to unintended malfunctioning of your machine.

This exercise pointed me to a lot of interesting directories occupying quite a lot of disk space on my machine.

Hidden Dot files

Developers often use dotfiles to customise their machine, including settings for unix shell (.zshrc), text editors (.vimrc), version control systems (.gitconfig), and so on. These files allow developers to tailor their workflow and preferences to suit their needs across different machines or environments.

# List all files including dotfiles
ls -a ~/

These files are typically hidden and Mac “Storage” preferences will not show up such directories when trying to free up space. Using Finder, press “Command (⌘) + Shift (⇧) + . (period)” keys which starts showing all the hidden and dotfiles. Now, follow the approach outlined in the previous section to recursively find large files that you can investigate and delete.

My Free space

Identifying the biggest disk space consumers on my Mac wasn’t easy unless you’re a command-line pro. But I managed to do it just by using Finder and the steps mentioned previously.

Goodbye Docker

Docker raw

According to this Docker Github issue, Docker typically reserves at least 64 GB of storage space even if you are not using it actively or have that many containers. While the thread provides methods for reducing the size of this file, hobbyists like myself, may find that deleting the Docker.raw file in ~/Library/Containers/com.docker.docker/Data/vms/0/data/Docker.raw, which consumes 64 GB of disk space, works. If you’re not actively using Docker, consider uninstalling it and then delete the Docker.raw file.

64 GB gained!

Unused Android SDKs

Android SDK that includes emulators, platform tools, and various SDKs, can accumulate consuming significant disk space on your system over time. While these resources are essential for Android development and function seamlessly within Android Studio, it’s easy to overlook their cumulative storage impact over the years.

System images in Android are implementation codes utilised by various Android emulators. During my cleanup, I discovered numerous outdated system images for Android versions 8.0, 8.1, 9.0, and so on, occupying unnecessary space on my machine. Each image weighed approximately 5 to 10 GB, collectively totalling around 40 GB of storage. I gained roughly 20 GB by deleting the old unused system images from Library/Android/sdk/system-images/.

20 GB gained!

Android Virtual Devices (AVDs)

According to Android, “The Android Emulator simulates Android devices on your computer so that you can test your application on a variety of devices and Android API levels without needing to have each physical device.”

Android AVD

Over time, I noticed that my Emulators in ~/.android/avd directory grew significantly to ~66 GB with one particular AVD, my primary “Pixel 3A” emulator, occupying a whopping 45 GB of storage. This gradual increase in disk usage is not uncommon for AVDs, as they tend to accumulate cache, snapshots and other temporary files over time. Just a reminder, the directory where these emulators live is a dotfile called .android, which can be easily spotted using the Finder tips mentioned earlier.

# List your Android AVDs
emulator -list-avds

# Delete user data in an AVD
emulator -avd "Pixel_3a_API_31" -wipe-data

Using the -wipe-data flag when starting the emulator significantly reduced the storage usage of the “Pixel 3A” emulator to just 10 GB. Starting the emulator with this flag periodically can help keep storage usage in check.

35 GB gained!

Cheat code (Grand perspective)

According to GrandPerspective, “GrandPerspective is a small utility application for macOS that graphically shows the disk usage within a file system.”

Grand perspective

I found GrandPerspective to be incredibly user-friendly and effective for visualizing directory storage usage. Its intuitive tree map format provided a clear overview of storage space allocation, making it easy to identify large directories of Docker and Android that were consuming significant storage. This application considers hidden files, dot files, system files that we covered in the blog post so far thereby serving as the cheat code to free up storage space on a Mac.

The application is available for free but consider supporting the developers by downloading from the Mac App Store.

# To install GrandPerspective using Homebrew
brew install —cask grandperspective

Conclusion

Implementing the solutions outlined in this post helped me successfully reclaim approximately 100 GB of free space on my Mac. While the results may vary depending on what’s used for Software development, the methods hopefully serve well in assisting developers to optimise disk storage.