How to remove all old and outdated brew packages on MacOS

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

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.
🐧 If you liked this page, please support my work on Patreon or with a donation.
🐧 Get the latest tutorials on SysAdmin, Linux/Unix, Open Source/DevOps topics:
CategoryList of Unix and Linux commands
File Managementcat
FirewallAlpine Awall CentOS 8 OpenSUSE RHEL 8 Ubuntu 16.04 Ubuntu 18.04 Ubuntu 20.04
Network Utilitiesdig host ip nmap
OpenVPNCentOS 7 CentOS 8 Debian 10 Debian 8/9 Ubuntu 18.04 Ubuntu 20.04
Package Managerapk apt
Processes Managementbg chroot cron disown fg jobs killall kill pidof pstree pwdx time
Searchinggrep whereis which
User Informationgroups id lastcomm last lid/libuser-lid logname members users whoami who w
WireGuard VPNAlpine CentOS 8 Debian 10 Firewall Ubuntu 20.04
5 comments… add one
  • alecthegeek Nov 8, 2016 @ 19:13

    and of course `brew cask cleanup` as well, because you also use cask to manage you Apple apps as well

  • reza Nov 23, 2016 @ 6:10

    thanks alot

  • E_Cooking Feb 6, 2017 @ 11:26

    My cmds:
    brew cleanup
    brew cask cleanup
    rm -rf $(brew --cache)

  • caic Apr 3, 2017 @ 9:40

    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

  • John Aug 22, 2020 @ 20:09

    August 2020
    `brew cask cleanup` is deprecated. I believe `brew cleanup` handles cask files as well now.

Leave a Reply

Your email address will not be published. Required fields are marked *

Use HTML <pre>...</pre>, <code>...</code> and <kbd>...</kbd> for code samples.