I have a Homebrew package that I wish to uninstall / remove with all its dependencies, skipping packages whom other packages depend upon.
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.
Based on @jfmercer answer (corrections needed more than a comment).
Remove package's dependencies (does not remove package):
Remove package:
Reinstall missing libraries:
Tested uninstalling
meld
after discovering MeldMerge releases.EDIT:
It looks like the issue is now solved using an external command called
brew rmdeps
orbrew rmtree
.To install and use, issue the following commands:
See the above link for more information and discussion.
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
andbrew deps <package>
, you might just get a list of the orphaned dependency packages, which you can uninstall manually afterwards. Combine this withxargs
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 ofxargs
:See the comment on the issue mentioned above for more info.
A More-Complete Bourne Shell Function
There are a number of good answers already, but some are out of date and none of them are entirely complete. In particular, most of them will remove dependencies but still leave it up to you to remove the originally-targeted formula afterwards. The posted one-liners can also be tedious to work with if you want to uninstall more than one formula at a time.
Here is a Bourne-compatible shell function (without any known Bashisms) that takes a list of formulae, removes each one's dependencies, removes all copies of the formula itself, and then reinstalls any missing dependencies.
It was tested on Homebrew 1.7.4.
Caveats
This works on all standard formulae that I tested. It does not presently handle casks, but neither will it complain loudly if you attempt to unbrew a cask with the same name as a standard formula (e.g. MacVim).
Other answers didn't work for me, but this did (in
fish
shell):Because
brew remove $p
fails when some other package depends onp
.Using this answer requires that you create and maintain a file that contains the package names you want installed on your system. If you don't have one already, use the following command and delete the package names what you don't want to keep installed.
Then you can remove all installed, but unwanted packages and any unnecessary dependencies by running the following command
brew_clean
is available here: https://gist.github.com/cskeeters/10ff1295bca93808213dThis script gets all of the packages you specified in brew_packages and all of their dependancies and compares them against the output of
brew list
and finally removes the unwanted packages after verifying this list with the user.At this point if you want to remove package
a
, you simply remove it from the brew_packages file then re-runbrew_clean brew_packages
. It will removeb
, but notc
.You can just use a UNIX pipe for this