Reputation: 15714
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.
Upvotes: 583
Views: 682119
Reputation: 5387
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 (not needed from v4.3.0):
brew autoremove
Upvotes: 465
Reputation: 1086
As of May 2024 with Homebrew 4.3, the previously optional HOMEBREW_AUTOREMOVE
flag is enabled by default, so uninstalling a package will also automatically remove its dependencies.
See: https://brew.sh/2024/05/14/homebrew-4.3.0/
Upvotes: 4
Reputation: 31
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.
(Analyze formula.jws.json, cask.jws.json, INSTALL_RECEIPT.json)
The tool also reads runtime_dependencies, allowing for complete uninstallation,
It also checks for build_dependencies with the . option to check build_dependencies
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 7 million lines.
This is one of the features of the tool, but the source of the dependency check is long, so I will paste a link to it.
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
( . Option build included )
bl -ddd efl ffmpeg or bl -ddd efl . ffmpeg
Affiliation Producer
https://github.com/konnano/brew_list
Upvotes: 0
Reputation: 15714
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
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.
Upvotes: 523
Reputation: 9299
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
Upvotes: 1
Reputation: 323
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.
$
Upvotes: 13
Reputation: 84333
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.
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).
Upvotes: 4
Reputation: 4819
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
Upvotes: 0
Reputation: 1160
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
.
Upvotes: 3
Reputation: 96537
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
Upvotes: 47
Reputation: 4087
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.
Upvotes: 24
Reputation: 3721
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.
Upvotes: 38
Reputation: 1488
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
.
Upvotes: 13
Reputation: 87
You can just use a UNIX pipe for this
brew deps [FORMULA] | xargs brew rm
Upvotes: 3