531

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.

Nimesh Neema
  • 1,528
  • 2
  • 17
  • 44
Ory Band
  • 14,716
  • 14
  • 59
  • 66
  • 20
    Isn't this what any package manager, worth this name, should do out-of-the-box?! LOL – mljrg Sep 26 '19 at 09:54
  • 25
    As of 2021, [`brew autoremove`](https://stackoverflow.com/a/66719581/619961) is what you're looking for: – ikaerom May 19 '21 at 09:53

12 Answers12

502

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.

Urs
  • 4,984
  • 7
  • 54
  • 116
Ory Band
  • 14,716
  • 14
  • 59
  • 66
  • 3
    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)"``` – ikaerom Aug 14 '20 at 11:06
  • 1
    brew rmtree does not work at all! – Ajay Gautam Aug 25 '23 at 10:17
348

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

hsym
  • 4,217
  • 2
  • 20
  • 30
46

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
Timmmm
  • 88,195
  • 71
  • 364
  • 509
  • 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 Jul 05 '17 at 18:37
  • 3
    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. – dragon788 Jul 05 '17 at 18:57
  • Confirmed that recommendation to use `rmrec` is bad. It does not handle dependencies at all. – Oleg Medvedyev Aug 10 '17 at 03:58
  • You mean `rmtree`? `rmrec` does handle dependencies. – Timmmm Aug 10 '17 at 12:16
  • 9
    `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 https://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. – sgon00 Nov 30 '18 at 06:16
  • 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**. – Adam Erickson Feb 23 '19 at 06:43
  • @Timmmm it seems the `rmtree` issue linked to in your comment has been fixed for a while now https://github.com/beeftornado/homebrew-rmtree/issues/14 – retrovertigo May 21 '19 at 22:30
38

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.

jfmercer
  • 3,641
  • 3
  • 29
  • 35
22

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.

vault
  • 3,930
  • 1
  • 35
  • 46
  • 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. – webtweakers Apr 09 '20 at 13:21
  • For me `brew deps` show different packages than those that shown after `brew uninstall ` attempt. – mrgloom Nov 13 '20 at 15:54
13

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.

brew leaves > brew_packages

Then you can remove all installed, but unwanted packages and any unnecessary dependencies by running the following command

brew_clean brew_packages

brew_clean is available here: https://gist.github.com/cskeeters/10ff1295bca93808213d

This 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-run brew_clean brew_packages. It will remove b, but not c.

Chad Skeeters
  • 1,468
  • 16
  • 19
11

Save the following script as brew-purge

#!/bin/bash
#:Usage: brew purge formula
#: 
#:Removes the package and all dependancies.
#: 
#: 
PKG="$1"
if [ -z "$PKG" ];then
   brew purge --help
   exit 1
fi
brew rm $PKG
[ $? -ne 0 ] && exit 1
while brew rm $(join <(brew leaves) <(brew deps $PKG)) 2>/dev/null
  do :
done
echo Package $PKG and its dependancies have been removed.
exit 0

Now install it with the following command

sudo install brew-purge /usr/local/bin

Now run it

brew purge package

Example using gpg

$ brew purge gpg
Uninstalling /usr/local/Cellar/gnupg/2.2.13... (134 files, 11.0MB)
Uninstalling /usr/local/Cellar/adns/1.5.1... (14 files, 597.5KB)
Uninstalling /usr/local/Cellar/gnutls/3.6.6... (1,200 files, 8.9MB)
Uninstalling /usr/local/Cellar/libgcrypt/1.8.4... (21 files, 2.6MB)
Uninstalling /usr/local/Cellar/libksba/1.3.5... (14 files, 344.2KB)
Uninstalling /usr/local/Cellar/libusb/1.0.22... (29 files, 508KB)
Uninstalling /usr/local/Cellar/npth/1.6... (11 files, 71.7KB)
Uninstalling /usr/local/Cellar/pinentry/1.1.0_1... (12 files, 263.9KB)
Uninstalling /usr/local/Cellar/libassuan/2.5.3... (16 files, 444.2KB)
Uninstalling /usr/local/Cellar/libtasn1/4.13... (59 files, 436KB)
Uninstalling /usr/local/Cellar/libunistring/0.9.10... (54 files, 4.4MB)
Uninstalling /usr/local/Cellar/nettle/3.4.1... (85 files, 2MB)
Uninstalling /usr/local/Cellar/p11-kit/0.23.15... (63 files, 2.9MB)
Uninstalling /usr/local/Cellar/gmp/6.1.2_2... (18 files, 3.1MB)
Uninstalling /usr/local/Cellar/libffi/3.2.1... (16 files, 296.8KB)
Uninstalling /usr/local/Cellar/libgpg-error/1.35... (27 files, 854.8KB)
Package gpg and its dependancies have been removed.
$ 
Kevin Davies
  • 155
  • 1
  • 4
4

You can just use a UNIX pipe for this

brew deps [FORMULA] | xargs brew rm
shapeshed
  • 89
  • 1
  • 1
  • 14
    This isn't a good solution. You can break other packages' dependencies this way. – Ory Band Mar 12 '13 at 20:39
  • 12
    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 – SeanJA Jan 24 '14 at 22:25
4

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.

unbrew () {
    local formula
    for formula in "$@"; do
        brew deps "$formula" |
        xargs brew uninstall --ignore-dependencies --force
        brew uninstall --force "$formula"
    done
    brew missing | cut -f2 -d: | sort -u | xargs brew install
}

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).

Todd A. Jacobs
  • 81,402
  • 15
  • 141
  • 199
  • This started to uninstalled my packages randomly and broke my brew configuration, and zsh – alper Jul 18 '20 at 10:34
3

Other answers didn't work for me, but this did (in fish shell):

brew remove <package>
for p in (brew deps <package>)
    brew remove $p
end

Because brew remove $p fails when some other package depends on p.

Luke Miles
  • 941
  • 9
  • 19
0

The answer of @jfmercer must be modified slightly to work with current brew, because the output of brew missing has changed:

brew deps [FORMULA] | xargs brew remove --ignore-dependencies && brew missing | cut -f1 -d: | xargs brew install
freytag
  • 4,769
  • 2
  • 27
  • 32
0

Slightly refined; can supply multiple packages; has usage when none supplied.

#!/bin/bash
# Removes the package and all dependancies.

if [ $# -eq 0 ]; then
   echo "$(basename $0) <pkg> [<pkg> [...]]"
   exit 1
fi

function tree() {
    pkg="$1"
    join <(brew leaves) <(sort <(brew deps ${pkg}; echo ${pkg}))
} 

let e=0
for pkg in "$@"; do
    printf "Purging %s and its dependencies...\n" "${pkg}"
    deps=( $(tree ${pkg}) )
    while (( ${#deps[@]} > 0 )); do
        brew rm "${deps[@]}"
        deps=( $(tree ${pkg}) )
    done
done
khosrow
  • 8,799
  • 4
  • 21
  • 24