Uninstall / remove a Homebrew package including all its dependencies
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.
Ory Band
Backend engineer. Loves Clojure, Golang and Linux. Interested in distributed systems. https://ory.band
Updated on September 23, 2021Comments
-
Ory Band about 1 yearI 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
awhich depends on packagesb&c, where packagedalso depends on packagec. The result should uninstall botha&b, skippingc.How can I do that?
There must be a way to uninstall a package without leaving unnecessary junk behind.
-
Ory Band over 9 yearsThis isn't a good solution. You can break other packages' dependencies this way. -
SeanJA almost 9 yearsThere is a command you can run afterwards to tell you if you nuked any other required dependenciesbrew missingwhich will tell you what command you need to run to get them back -
alphadogg almost 6 yearsHad to add--ignore-dependenciesto remove to allow this to work. -
dragon788 over 5 yearsReally 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 over 5 yearsCaveat: It also appearsrmrecdoesn't currently handle namespaced packages very well. I was using something that pulled inlinuxbrew/xorg/xorgand it removed everything except that package and even explicitly naming that package it couldn't resolve it and did nothing. -
Oleg Medvedyev over 5 yearsConfirmed that recommendation to use
rmrecis bad. It does not handle dependencies at all. -
Timmmm over 5 yearsYou meanrmtree?rmrecdoes handle dependencies. -
sgon00 about 4 yearsGod 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 almost 4 yearsI think brew clean is built-in now, trybrew clean -s -
Devin Rhode almost 4 yearsABOVE COMMENT WAS WRONG, editing cuts off at 5 minutes. trybrew cleanup -s && brew cask cleanup -
Adam Erickson almost 4 yearsThe 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 over 3 years@Timmmm it seems thermtreeissue linked to in your comment has been fixed for a while now github.com/beeftornado/homebrew-rmtree/issues/14 -
Marco Pappalardo about 3 yearsThis broke my brew configuration, as it did not reinstall the deps that are used in other packages. DONT USE -
webtweakers over 2 yearsI 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 over 2 yearsThis started to uninstalled my packages randomly and broke my brew configuration, and zsh -
ikaerom over 2 yearsIn 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 neededbrew cleanup -s && \rm -rf "$(brew --cache)" -
mrgloom about 2 yearsFor mebrew depsshow different packages than those that shown afterbrew uninstallattempt. -
stevemao over 1 yearThis also removes some packages that are listed in brewfile, why is that? -
hsym over 1 year@stevemao Try asking the Homebrew team.