564

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.

2
  • 25
    Isn't this what any package manager, worth this name, should do out-of-the-box?! LOL
    – mljrg
    Commented Sep 26, 2019 at 9:54
  • 27
    As of 2021, brew autoremove is what you're looking for:
    – ikaerom
    Commented May 19, 2021 at 9:53

13 Answers 13

518

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.

2
  • 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
    Commented Aug 14, 2020 at 11:06
  • 3
    brew rmtree does not work at all! Commented Aug 25, 2023 at 10:17
422

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

6
  • This also removes some packages that are listed in brewfile, why is that?
    – stevemao
    Commented Jun 24, 2021 at 3:02
  • 5
    @stevemao Try asking the Homebrew team.
    – hsym
    Commented Jul 3, 2021 at 10:03
  • 11
    If you run export HOMEBREW_AUTOREMOVE=1 then brew uninstall will automatically run autoremove
    – Carl Walsh
    Commented Apr 12, 2023 at 20:54
  • 3
    I saw gazillions of packages installed with mysql. I did brew uinstall mysql, then brew autoremove and only 4 formulae were identified for removal. Does this work like I think and/or are packages incorrectly identified as dependencies vs. not?
    – Hendy
    Commented Sep 15, 2023 at 21:18
  • this should be the first attempt/approach in 2024
    – Joneron
    Commented Mar 4 at 10:47
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
7
  • 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
    Commented Jul 5, 2017 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
    Commented Jul 5, 2017 at 18:57
  • Confirmed that recommendation to use rmrec is bad. It does not handle dependencies at all. Commented Aug 10, 2017 at 3:58
  • 10
    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.
    – sgon00
    Commented Nov 30, 2018 at 6:16
  • 1
    @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 Commented May 21, 2019 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.

2
  • Had to add --ignore-dependencies to remove to allow this to work.
    – alphadogg
    Commented Dec 28, 2016 at 16:30
  • 8
    This broke my brew configuration, as it did not reinstall the deps that are used in other packages. DONT USE Commented Dec 4, 2019 at 10:09
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.

2
  • 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. Commented Apr 9, 2020 at 13:21
  • For me brew deps show different packages than those that shown after brew uninstall attempt.
    – mrgloom
    Commented Nov 13, 2020 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.

2
  • I think brew clean is built-in now, try brew clean -s Commented Dec 19, 2018 at 22:34
  • 2
    ABOVE COMMENT WAS WRONG, editing cuts off at 5 minutes. try brew cleanup -s && brew cask cleanup Commented Dec 19, 2018 at 22:40
12

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

1
  • This started to uninstalled my packages randomly and broke my brew configuration, and zsh
    – alper
    Commented Jul 18, 2020 at 10:34
3

You can just use a UNIX pipe for this

brew deps [FORMULA] | xargs brew rm
2
  • 15
    This isn't a good solution. You can break other packages' dependencies this way.
    – Ory Band
    Commented Mar 12, 2013 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
    Commented Jan 24, 2014 at 22:25
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.

1

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
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
0

I'm not good at English, so please take this into account when reading.
Since homebrew's deep dependencies cannot be retrieved with a simple script, I created an analysis tool.
Examine the formulas that all formulas depend on.

           |-formula(B)
           |
formula(A)-|-formela(C)
           |
           |-formula(D)

formula(A).deps = formula(B),formula(C),formula(D)

Examine all formulas that depend on that formula.

formula(X)-|
           |
formula(Y)-|-formula(A)
           |
formula(z)-|

formula(A).uses = formula(X),formula(Y),formula(Z)

Save to simple database.
The heart of this tool lies in the part that creates intermediate files.

Every time you start the tool, it updates the simple database in the background every hour.
retrieve all dependent formulas from the simple database,
Check whether the formula is dependent on other formulas than the one on which it is dependent,
Select only formulas that are dependent on dependent formulas.
Formulas that are multi-level dependent in the tree structure and are higher up are not selected.
Compare all formula dependencies and extract unnecessary formulas.

Linux has special dependencies and takes time.
Linux's efl dependencies are a tree structure of over 10 million lines.
This is one of the functions of the tool, but since the source is long, I will paste a link.

command
bl -dd <formula> # dry-run
bl -ddd <formula> # [y/n]

For example, if you want to remove the efl dependency but leave the ffmpeg dependency

bl -ddd efl ffmpeg

https://github.com/konnano/brew_list

2
  • You should mention your affiliation with this link.
    – bfontaine
    Commented Apr 19 at 15:15
  • While this link may answer the question, it is better to include the essential parts of the answer here and provide the link for reference. Link-only answers can become invalid if the linked page changes. - From Review
    – CPlus
    Commented Apr 24 at 22:14

Not the answer you're looking for? Browse other questions tagged or ask your own question.