Skip to content

Instantly share code, notes, and snippets.

@adriansr
Created February 6, 2018 22:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save adriansr/94230f7b21b7e278a9c98c2ecc897077 to your computer and use it in GitHub Desktop.
Save adriansr/94230f7b21b7e278a9c98c2ecc897077 to your computer and use it in GitHub Desktop.
Uninstall macOS pkg removing all files
#!/bin/bash
die() {
echo "error: $@" >&2
exit 1
}
test "$#" -gt 0 || die "Usage: $0 <package identifier>"
test "$(uname -s)" = "Darwin" || die "Must be run under macOS"
test $(id -u) -eq 0 || die "Must be root"
which -s pkgutil || die "pkgutil not available. Install XCode"
APP="$1"
pkgutil --pkg-info "$APP" > /dev/null || die "No found app by this package identifier"
for key in volume location
do
EXP="^$key: "
VAL=$(pkgutil --pkg-info "$APP" | grep "$EXP" | sed "s/$EXP//")
eval $key=\$VAL
done
BASE="$volume$location"
test -d "$BASE" || die "Resolved base directory '$BASE' doesn't exist"
pushd "$BASE"
pkgutil --only-files --files "$APP" | tr '\n' '\0' | xargs -0 -n 1 rm
pkgutil --only-dirs --files "$APP" | sort -r | tr '\n' '\0' | xargs -0 -n 1 rmdir
popd
pkgutil --forget "$APP" || die "Failed to remove the package from the database"
echo "Done."
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment