When yo installed a .pkg file on your mac, the question is how to remove it again.

Here's how to do it (using Go as an example).

We start with listing the installed packages:

$ pkgutil --pkgs

For Go, the package name is org.golang.go

Then we can inspect which files the package installed by executing:

$ pkgutil --files org.golang.go

As you can see, these paths are relative, not absolute. They are relative to the install location of the package. To get the install location of the package, you can execute:

$ pkgutil --pkg-info org.golang.go
package-id: org.golang.go
version: go1.16beta1
volume: /
location: /
install-time: 1608468554

As they are installed in the root, we first need to go to that directory. This is important as the remaining commands use the relative paths to this location.

$ cd /

Beware! Modifying the filesystem with root privileges can be hazardous.

We can then start with removing the actual files:

$ pkgutil --only-files --files org.golang.go | tr '\n' '\0' | xargs -n 1 -0 sudo rm -f

Then, we can also remove all empty directories with:

$ pkgutil --only-dirs --files org.golang.go | tail -r | tr '\n' '\0' | xargs -n 1 -0 sudo rmdir

Note that we are using the rmdir command here instead of rm -r. The rmdir command only removes the folders if they are empty. We want to keep the non-empty folders as there are folders in the list which are shared between different applications (e.g. the /usr/local folder which you don't want to remove).

The last step is to make the system forget that the package was installed:

$ sudo pkgutil --forget org.golang.go

Before you execute this, please be aware that if you don't use the commands correctly, you might bring your system into an unusable state, so proceed with caution.

Related Posts

  • SSH Tunnel
  • Installing Packages from a file on Linux
  • Setting up an NFS share on Linux
  • Golang vs Apple Silicon
  • Cleaning up Xcode data