Skip to content

Instantly share code, notes, and snippets.

@akunzai
Last active July 15, 2024 15:10
Show Gist options
  • Select an option

  • Save akunzai/dd7c40ca4054db98b97208f0f3907400 to your computer and use it in GitHub Desktop.

Select an option

Save akunzai/dd7c40ca4054db98b97208f0f3907400 to your computer and use it in GitHub Desktop.
Uninstall pkg package from macOS

Run this script from GitHub Gist directly

sh <(curl -Ls https://gist.github.com/akunzai/dd7c40ca4054db98b97208f0f3907400/raw/uninstall-pkg.sh)

Passing parameters to bash when executing the script fetched by curl

curl -Ls https://gist.github.com/akunzai/dd7c40ca4054db98b97208f0f3907400/raw/uninstall-pkg.sh | sh -s com.xamarin.mono-MDK.pkg
#!/bin/sh
# Uninstall pkg package from macOS
# https://superuser.com/questions/36567/how-do-i-uninstall-any-apple-pkg-package-file/525395
PKGID="$1"
while [ -z "$PKGID" ]; do
pkgutil --pkgs | grep -v 'com.apple.' | sort
echo
printf "Please input the package ID: "
read PKGID
done
case "$PKGID" in
com.apple.*)
echo "Cannot remove Apple built-in packages!"
exit 1
;;
esac
if ! pkgutil --pkg-info "$PKGID" >/dev/null 2>&1; then
echo "Packages: [$PKGID] not found!"
exit 2
fi
pkgutil --files "$PKGID"
echo
pkgutil --pkg-info "$PKGID"
echo
printf "Are you sure to uninstall the package? (y/N) "
read response
case "$response" in
[yY][eE][sS]|[yY])
PKGPATH=$(pkgutil --pkg-info "$PKGID" | awk '/^volume:/ {vol=$2} /^location:/ {loc=$2} END {print vol loc}')
cd "$PKGPATH" || exit
pkgutil --only-files --files "$PKGID" | tr '\n' '\0' | xargs -0 sudo rm -iv
pkgutil --only-dirs --files "$PKGID" | sort -r | tr '\n' '\0' | xargs -0 sudo rmdir
sudo pkgutil --forget "$PKGID" && echo "Done"
cd - >/dev/null 2>&1
;;
*)
echo "Uninstallation cancelled."
;;
esac
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment