How to remove all old and outdated brew packages on MacOS

Posted on in Categories , , last updated February 5, 2017

I am using Homebrew to install packages on MacOS Sierra or Mac OS X. I noticed multiple version of bash or curl installed in /usr/local/Cellar/bash/. How can I delete an outdated version of brew packages and only keep the latest version on my system?

First, open the Terminal application. To see if multiple version of curl packages installed, enter:

$ ls /usr/local/Cellar/curl/
OR
$ brew info curl
Sample outputs:

curl: stable 7.51.0 (bottled) [keg-only]
Get a file from an HTTP, HTTPS or FTP server
https://curl.haxx.se/
/usr/local/Cellar/curl/7.50.3 (367 files, 2.6M)
  Built from source on 2016-11-02 at 03:13:19 with: --with-nghttp2
/usr/local/Cellar/curl/7.51.0 (368 files, 2.7M)
  Built from source on 2016-11-08 at 01:10:09 with: --with-nghttp2
From: https://github.com/Homebrew/homebrew-core/blob/master/Formula/curl.rb
==> Dependencies
Build: pkg-config ?
Optional: openssl ?, libidn ?, rtmpdump ?, libssh2 ?, c-ares ?, libmetalink ?, libressl ?, nghttp2 ?
==> Options
--with-c-ares
	Build with C-Ares async DNS support
--with-gssapi
	Build with GSSAPI/Kerberos authentication support.
--with-libidn
	Build with support for Internationalized Domain Names
--with-libmetalink
	Build with libmetalink support.
--with-libressl
	Build with LibreSSL instead of Secure Transport or OpenSSL
--with-libssh2
	Build with scp and sftp support
--with-nghttp2
	Build with HTTP/2 support (requires OpenSSL or LibreSSL)
--with-openssl
	Build with OpenSSL instead of Secure Transport
--with-rtmpdump
	Build with RTMP support
==> Caveats
This formula is keg-only, which means it was not symlinked into /usr/local.
 
macOS already provides this software and installing another version in
parallel can cause all kinds of trouble.
 
Generally there are no consequences of this for you. If you build your
own software and it requires this formula, you'll need to add to your
build variables:
 
    LDFLAGS:  -L/usr/local/opt/curl/lib
    CPPFLAGS: -I/usr/local/opt/curl/include
    PKG_CONFIG_PATH: /usr/local/opt/curl/lib/pkgconfig

Say hello to the cleanup option

The cleanup option can remove all installed or specific formulae, remove any older versions from the cellar. Also, old downloads from the Homebrew download-cache are deleted. Simply type the following command to free up disk space and delete outdated older versions:
$ brew cleanup
Sample outputs:

Fig.01: Delete outdated version of package using brew
Fig.01: Delete outdated version of package using brew

It is recommended that you run cleanup after update to get rid of old packages:
$ brew update && brew upgrade && brew cleanup
At the end you will see the following message on screen:

Removing: /usr/local/Cellar/android-sdk/24.2... (1,460 files, 105.3M)
Removing: /usr/local/Cellar/android-sdk/24.4... (1,471 files, 136.4M)
Removing: /usr/local/Cellar/atk/2.20.0... (205 files, 3.1M)
Removing: /usr/local/Cellar/babl/0.1.16... (37 files, 819.1K)
Removing: /usr/local/Cellar/bash/4.3.42... (59 files, 7.3M)
Removing: /usr/local/Cellar/bash/4.3.46... (59 files, 7.3M)
Removing: /usr/local/Cellar/bash/4.4... (145 files, 8.8M)
...
..
...
Removing: /Users/veryv/Library/Logs/Homebrew/libtorrent... (1.9K)
Removing: /Users/veryv/Library/Logs/Homebrew/most... (5 files, 124.5K)
Removing: /Users/veryv/Library/Logs/Homebrew/mtr... (68B)
Removing: /Users/veryv/Library/Logs/Homebrew/nmap... (6 files, 1.6M)
Removing: /Users/veryv/Library/Logs/Homebrew/openssl... (68B)
Removing: /Users/veryv/Library/Logs/Homebrew/rsnapshot... (3 files, 10.2K)
Removing: /Users/veryv/Library/Logs/Homebrew/ssh-copy-id... (68B)
Removing: /Users/veryv/Library/Logs/Homebrew/wakeonlan... (2 files, 470B)
==> This operation has freed approximately 3.9G of disk space.

A note about cask

Type the following to cleanup cask too:
$ brew cask cleanup
Sample outputs:

==> Removing cached downloads
/Users/veryv/Library/Caches/Homebrew/Cask/gimp--2.8.18-x86_64.dmg
/Users/veryv/Library/Caches/Homebrew/Cask/lisanet-gimp--2.8.14p2.dmg
/Users/veryv/Library/Caches/Homebrew/Cask/wireshark--2.2.3.dmg
/Users/veryv/Library/Caches/Homebrew/Cask/wkhtmltopdf--0.12.4.pkg
==> This operation has freed approximately 199.8M of disk space.

Posted by: Vivek Gite

The author is the creator of nixCraft and a seasoned sysadmin and a trainer for the Linux operating system/Unix shell scripting. He has worked with global clients and in various industries, including IT, education, defense and space research, and the nonprofit sector. Follow him on Twitter, Facebook, Google+.

4 comment

  1. This is my brew update command:

    $ cat /usr/local/bin/brewup
    #!/bin/bash
    /usr/local/bin/brew update
    /usr/local/bin/brew upgrade
    /usr/local/bin/brew cleanup
    /usr/local/bin/brew cask cleanup

Leave a Comment