How can I remove outdated installed versions of Homebrew packages?

221

53

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

aknuds1

Posted 2015-09-20T18:42:37.377

Reputation: 8 668

3

Since version 2.0.0, cleanup happens automatically, unless you opt-out.

– Katrin Leinweber – 2019-03-06T19:24:43.440

Answers

338

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

Posted 2015-09-20T18:42:37.377

Reputation: 8 668

34For the record: brew cleanup only cleans outdated package versions if you have the newest version installed, so first brew upgrade then brew cleanup – Jan – 2016-10-14T14:15:03.927

39Holy crap. That just gave me more than 3G back. Who knew… – Alper – 2017-01-22T14:58:19.453

39This operation has freed approximately 17.2GB of disk space. – fvgs – 2017-04-17T07:30:34.097

12OMG! I just got back more than 12GB of space. WooHoo! – Richard Hurt – 2017-06-27T17:12:25.263

3Wow, one year's worth of upgrade without cleanup resulted in 17.6GB unused software for me! (Check this with brew cleanup -n, which doesn't delete anything.) Thanks for this answer. :) – Erik – 2017-10-13T18:07:21.047

how long does cleanup run? just out of curiosity. – Kishor Pawar – 2019-01-24T17:56:49.170

82

@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

Posted 2015-09-20T18:42:37.377

Reputation: 3 181

3

You don't need to do brew upgrade brew-cask anymore as since this pull request brew update will also now upgrade homebrew-cask automatically.

– Lantrix – 2016-07-12T06:20:40.663

1@Lantrix Thanks for that update. I've edited my answer to reflect this change. – SaxDaddy – 2016-07-12T15:28:36.183

2brew upgrade --cleanup will remove old packages as new packages are installed. – Zenexer – 2018-01-31T05:45:08.057

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 – 2018-03-24T11:13:23.787

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 – 2018-03-27T22:52:37.143

brew upgrade runs brew update before looking for package updates. Thus, brew update is unnecessary. – MCCCS – 2018-05-03T12:41:44.077

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 – 2019-01-20T12:02:19.490

36

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

Posted 2015-09-20T18:42:37.377

Reputation: 480