Uninstall / remove a Homebrew package including all its dependencies

431,506

Solution 1

EDIT:

It looks like the issue is now solved using an external command called brew rmdeps or brew rmtree.

To install and use, issue the following commands:

$ brew tap beeftornado/rmtree
$ brew rmtree <package>

See the above link for more information and discussion.


[EDIT] see the new command brew autoremove in https://stackoverflow.com/a/66719581/160968


Original answer:

It appears that currently, there's no easy way to accomplish this.

However, I filed an issue on Homebrew's GitHub page, and somebody suggested a temporary solution until they add an exclusive command to solve this.

There's an external command called brew leaves which prints all packages that are not dependencies of other packages.

If you do a logical and on the output of brew leaves and brew deps <package>, you might just get a list of the orphaned dependency packages, which you can uninstall manually afterwards. Combine this with xargs and you'll get what you need, I guess (untested, don't count on this).


EDIT: Somebody just suggested a very similar solution, using join instead of xargs:

brew rm FORMULA
brew rm $(join <(brew leaves) <(brew deps FORMULA))

See the comment on the issue mentioned above for more info.

Solution 2

By the end of 2020, the Homebrew team added a simple command brew autoremove to remove all unused dependencies.

First, uninstall the package:

brew uninstall <package>

Then, remove all the unused dependencies:

brew autoremove

Solution 3

brew rmtree doesn't work at all. From the links on that issue I found rmrec which actually does work. God knows why brew doesn't have this as a native command.

brew tap ggpeti/rmrec
brew rmrec pkgname

Solution 4

The goal here is to remove the given package and its dependencies without breaking another package's dependencies. I use this command:

brew deps [FORMULA] | xargs brew remove --ignore-dependencies && brew missing | xargs brew install

Note: Edited to reflect @alphadogg's helpful comment.

Solution 5

Based on @jfmercer answer (corrections needed more than a comment).

Remove package's dependencies (does not remove package):

brew deps [FORMULA] | xargs brew remove --ignore-dependencies

Remove package:

brew remove [FORMULA]

Reinstall missing libraries:

brew missing | cut -d: -f2 | sort | uniq | xargs brew install

Tested uninstalling meld after discovering MeldMerge releases.

Share:
431,506
Ory Band
Author by

Ory Band

Backend engineer. Loves Clojure, Golang and Linux. Interested in distributed systems. https://ory.band

Updated on September 23, 2021

Comments

  • Ory Band
    Ory Band about 1 year

    I have a Homebrew formula that I wish to uninstall/remove along with all its dependencies, skipping packages whom other packages depend upon (a.k.a. Cascading package removal in Package manager parlance).

    e.g. Uninstall package a which depends on packages b & c, where package d also depends on package c. The result should uninstall both a & b, skipping c.

    How can I do that?

    There must be a way to uninstall a package without leaving unnecessary junk behind.

  • Ory Band
    Ory Band over 9 years
    This isn't a good solution. You can break other packages' dependencies this way.
  • SeanJA
    SeanJA almost 9 years
    There is a command you can run afterwards to tell you if you nuked any other required dependencies brew missing which will tell you what command you need to run to get them back
  • alphadogg
    alphadogg almost 6 years
    Had to add --ignore-dependencies to remove to allow this to work.
  • dragon788
    dragon788 over 5 years
    Really LOVE this, though if you've already started down the path of uninstalling yourself and keep finding more packages you need to remove, you still need to know what are the widest touching packages, or you can just briefly reinstall the [FORMULA] and then use rmrec to recursively remove all it's dependencies properly.
  • dragon788
    dragon788 over 5 years
    Caveat: It also appears rmrec doesn't currently handle namespaced packages very well. I was using something that pulled in linuxbrew/xorg/xorg and it removed everything except that package and even explicitly naming that package it couldn't resolve it and did nothing.
  • Oleg Medvedyev over 5 years
    Confirmed that recommendation to use rmrec is bad. It does not handle dependencies at all.
  • Timmmm
    Timmmm over 5 years
    You mean rmtree? rmrec does handle dependencies.
  • sgon00
    sgon00 about 4 years
    God knows why brew doesn't have this as a native command. => Because brew developers have bad attitudes. They don't even allow people to open any issues. Check github.com/Homebrew/brew, it only has less than 20 issues now. Such a small number. Is that a good thing? No, it's very bad.
  • Devin Rhode
    Devin Rhode almost 4 years
    I think brew clean is built-in now, try brew clean -s
  • Devin Rhode
    Devin Rhode almost 4 years
    ABOVE COMMENT WAS WRONG, editing cuts off at 5 minutes. try brew cleanup -s && brew cask cleanup
  • Adam Erickson
    Adam Erickson almost 4 years
    The beauty of this solution is the simplicity of the script and that it works quite well. It needs to be made recursive to remove the dependencies of dependencies and their dependencies.
  • retrovertigo
    retrovertigo over 3 years
    @Timmmm it seems the rmtree issue linked to in your comment has been fixed for a while now github.com/beeftornado/homebrew-rmtree/issues/14
  • Marco Pappalardo
    Marco Pappalardo about 3 years
    This broke my brew configuration, as it did not reinstall the deps that are used in other packages. DONT USE
  • webtweakers
    webtweakers over 2 years
    I had installed ffmpeg with brew, which messed things up severely. Cleaning up ffmpeg, including its extended list of dependencies, with the method described above, worked for me.
  • alper
    alper over 2 years
    This started to uninstalled my packages randomly and broke my brew configuration, and zsh
  • ikaerom
    ikaerom over 2 years
    In 2020 this solution seems to be the best of all worlds presented herein. It's worthwile to mention that most of the time after purging a lot of packages, the following is needed brew cleanup -s && \rm -rf "$(brew --cache)"
  • mrgloom
    mrgloom about 2 years
    For me brew deps show different packages than those that shown after brew uninstall attempt.
  • stevemao
    stevemao over 1 year
    This also removes some packages that are listed in brewfile, why is that?
  • hsym
    hsym over 1 year
    @stevemao Try asking the Homebrew team.