[Homebrew] Remove packages and unused dependencies to free up spaces

I use my MacBook everyday to work on development and I use Homebrew to manage the packages on it. They work fine throughout the days but it’s such a plain to free up spaces on my machine by removing the packages I don’t need anymore.

If you’re a developer who use macOS as your daily driver, you probably familiar with Homebrew and understand my plain. For those of you who don’t know what’s Homebrew, ~~you could just skip the rest of this~~ it’s the most common, and probably the best package manager on macOS.

The only thing is lacking on Homebrew is the ability to auto remove the unused dependencies. There isn’t any official way to do the clean up. You might end up having lots of unused dependencies remain in you machine and take up a lot of storage. And trust me, you don’t want to remove those dependencies one by one manually or by using a set of commands to clean them up. Therefore we need a simple and elegant solution to get the job done. rmtree is what I’ve found for this purpose.

rmtree is a Homebrew external command that helps to remove the formula and its unused dependencies. Compare to the other methods I’ve found which invoke brew leaves to list out the dependencies, use the join option to combine the list with other commands etc… rmtree invoke one single command only.

$ brew rmtree {package name}

Simple enough? Alright, let’s install it.

$ brew tab beeftornado/rmtree

Use the command above to install rmtree. Remember we’re using brew tap instead of brew install here. Brew can’t search any package call rmtree to install as it isn’t on the list. So, don’t try, I feel so stupid after trying to search the package for a few times by calling brew search rmtree

After the installation, it’s time to clean up. Here I use Nginx as example to show you how does it work.

$ brew rmtree nginx

Remove nginx.png

As you can see, rmtree will list out the dependencies which won’t be used anymore after remove the target package (Nginx in this case). Once the target package has been removed, the dependencies on the list above will be removed as well.

Also, rmtree checks is the target package using by other package(s). If yes, it will tell you which package(s) is/are using the target package and terminate the remove process. Just like this.

Remove rejected.png

Although rmtree has this safety feature that prevent packages to be accidentally deleted, the author also said that the command may remove something you still need as there are formulae do not specify all of their dependencies. Just keep that in mind.

There you go, we’ve cleanup the packages and unused dependencies. If you want to free more spaces from brew, you can use the cleanup in brew to do a bit more.

$ brew cleanup

Run the command above. This command will remove outdated installed package versions remaining in the storage and may free up a few gigabytes of storage at the end.

Warning: Skipping vim: most recent version 8.0.0373 not installed

You will see something like this if you don’t have the newest version installed on your machine. Simply update the package(s).

$ brew upgrade

Type the package name after upgrade if you don’t want to update all your packages. Run the cleanup command again after updated.

There you go. You’ve reclaimed the last bit of spaces from Homebrew now. Hope you find this note helpful. Leave a comment below if you have any question or any other better solution. And I’ll end this up here, bye.

Leave a Reply