Skip to content

Instantly share code, notes, and snippets.

@gtklocker
Created May 9, 2020 21:18
Show Gist options
  • Save gtklocker/5b6fe5d781c414410874bae3cee473d9 to your computer and use it in GitHub Desktop.
Save gtklocker/5b6fe5d781c414410874bae3cee473d9 to your computer and use it in GitHub Desktop.
Recursively remove homebrew packages
#!/bin/zsh
set -euo pipefail
local preall=$(brew list -1)
local predeps=$(brew deps --installed)
local preleaves=$(brew leaves)
recur() {
local pkg=$1
local deps=$(echo "$predeps"|grep "^$pkg:"|cut -d':' -f2|xargs|tr ' ' '\n')
echo "> Zapping $pkg..."
brew uninstall "$pkg" || {
if echo "$preall"|grep -q "^$pkg$"; then
echo "> Package depended on by another package, skipping."
return
fi
}
echo "Dependencies: $(echo "$deps"|tr '\n' ',')"
test -z "$deps" || {
echo $deps|while read dep; do
if ! echo $preleaves|grep -q $dep; then
recur "$dep"
else
echo "> Skipping $dep as it was a leaf before."
fi
done
}
}
recur "$1"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment