234

How can I remove outdated versions of Homebrew packages that are installed side-by-side with current versions?

aknuds1
  • 9,368
  • 6
  • 28
  • 27

3 Answers3

353

The cleanup (brew cleanup) command will remove outdated installed package versions. To affect a particular package/formula, you may supply a formula name like so: brew cleanup $FORMULA. To simulate cleanup, i.e. see what would be removed, you may use the -n option: brew cleanup -n.

aknuds1
  • 9,368
  • 6
  • 28
  • 27
  • 38
    For the record: `brew cleanup` only cleans outdated package versions if you have the newest version installed, so first `brew upgrade` then `brew cleanup` – Jan Oct 14 '16 at 14:15
  • 43
    Holy crap. That just gave me more than 3G back. Who knew… – Alper Jan 22 '17 at 14:58
  • 43
    `This operation has freed approximately 17.2GB of disk space.` – fvgs Apr 17 '17 at 07:30
90

@aknuds1 is right about brew cleanup so I'll just add that I have an alias in my ~/.profile that does all my brew-related cleanup, including cask update/cleanup

alias brewski='brew update && brew upgrade && brew cleanup; brew doctor'

I end with brew doctor to make sure all packages are correctly symlinked, e.g., awscli seems to have a problem with this on the regular so I constantly have to unlink/relink. Hope this helps.


EDIT: As it was pointed out to me in the comments, brew cask functionality has been merged into the homebrew core. These commands were also throwing errors, so references to brew cask have been deleted.

EDIT2: As I run this daily, I noticed the following comment today. I've updated the command accordingly

    Warning: We decided to not change the behaviour of `brew upgrade` so
    `brew upgrade --all` is equivalent to `brew upgrade` without any other
    arguments (so the `--all` is a no-op and can be removed).
SaxDaddy
  • 3,809
  • 20
  • 19
  • 3
    You don't need to do `brew upgrade brew-cask` anymore as since [this pull request](https://github.com/caskroom/homebrew-cask/pull/15381) `brew update` will also now upgrade homebrew-cask automatically. – Lantrix Jul 12 '16 at 06:20
  • 1
    @Lantrix Thanks for that update. I've edited my answer to reflect this change. – SaxDaddy Jul 12 '16 at 15:28
  • 3
    `brew upgrade --cleanup` will remove old packages as new packages are installed. – Zenexer Jan 31 '18 at 05:45
  • I dont advise to use `brew upgrade` but instead to use `brew outdated` and then upgrade each one of the packages by `brew upgrade PACKAGE` that way you have more control – Pedro Luz Mar 24 '18 at 11:13
  • As `brew upgrade` is a single command to upgrade all packages, it works well for automation. Optionally, you can `pin` package versions you want to keep and upgrade the rest all at once. – SaxDaddy Mar 27 '18 at 22:52
  • 1
    `brew upgrade` runs `brew update` before looking for package updates. Thus, `brew update` is unnecessary. – MCCCS May 03 '18 at 12:41
  • Would it make sense to run `cleanup` before `upgrade`? Would that keep the least-outdated version remains? I'm guessing this could be useful for emergencies or something, while still ensuring that no old stuff accumulates? – Katrin Leinweber Jan 20 '19 at 12:02
  • `Unless HOMEBREW_NO_INSTALL_CLEANUP is set, brew cleanup will then be run for the upgraded formulae or, every 30 days, for all formulae. // Error: invalid option: --cleanup` – alper Jul 18 '20 at 11:30
39

Just for the records, you can clean the outdated versions of the packages that you are about to upgrade using the --cleanup flag:

brew upgrade --cleanup <package_name>

bergercookie
  • 520
  • 4
  • 5