78

Despite opinions to the contrary, not all packages are installed cleanly in only one directory. Is there a way to reverse the install process of a PKG file, preferably with the original package (or from a repository of information about installed packages)?

Specifically I've installed the PowerPC MySQL 5.4.1 package on an intel MacBook, and would like to cleanly reverse that, recovering the 5.1 x86 install I can see is still there, but not working properly now.

Giacomo1968
  • 58,727
dlamblin
  • 10,966

7 Answers7

128

This blog post describes how to uninstall .pkg using native pkgutil.

Modified excerpt

$ pkgutil --pkgs # list all installed packages
$ pkgutil --files the-package-name.pkg # list installed files

After visually inspecting the list of files you can do something like:

$ pkgutil --pkg-info the-package-name.pkg # check the location
$ cd / # assuming the package is rooted at /...
$ pkgutil --only-files --files the-package-name.pkg | tr '\n' '\0' | xargs -n 1 -0 sudo rm -f
$ pkgutil --only-dirs --files the-package-name.pkg | tail -r | tr '\n' '\0' | xargs -n 1 -0 sudo rmdir

Needless to say, extreme care should always be taken when removing files with root privileges. Particularly, be aware that some packages may update shared system components, so uninstalling them can actually break your system by removing a necessary component.

For smaller packages it is probably safer to just manually remove the files after visually inspecting the package file listing.

Apparently, there was once an --unlink option available in pkgutil, but as of Lion it is not mentioned in the man page. Perhaps it was removed because it was deemed too dangerous.

Once you've uninstalled the files, you can remove the receipt with:

$ sudo pkgutil --forget the-package-name.pkg
Giacomo1968
  • 58,727
brablc
  • 1,640
  • 1
  • 12
  • 9
21

Built into the system there is no option to uninstall the files using an uninstaller so you can either make an uninstaller yourself or remove the files manually.

The best method to determine what files have been installed is to get a hold of the original .pkg if possible. If this is not possible you can also use the receipts instead found at /Library/Receipts. Your biggest issue is when you are dealing with a .mpkg which contains multiple .pkg files as you will then have to find all the seperate .pkg files in that folder (thankfully not that difficult when sorted by date).

Once you have the .pkg file (Receipt or the full install file) you can then use a utility to either create the uninstaller or find the files so you can remove them manually:

Uninstaller

Absolute Software InstallEase is a free program that can create uninstallers from existing .pkg files. Make the uninstaller .pkg file (note: You'll need Apple's Developer Tools installed to actually make the .pkg file)

Manually

Using a program such as Pacifist or a QuickLook plugin like Suspicious Package you can view what files are installed and at what location. Using that list you can then manually navigate to those folders and remove the files. I've used this method personally countless times before I discovered InstallEase, but this is still often faster if the install isn't spread out among many locations.

cavalcade
  • 157
  • 8
Chealion
  • 26,327
10

You can also uninstall .pkg packages with UninstallPKG.

Full disclosure: Yes, I am the author.

Giacomo1968
  • 58,727
2

I made a shell srcipt

you can try it

https://github.com/iamrToday/pkg-remove

It shows a .gif demo, you can see the source code, just wrap the brablc's command line. You can run it to search infomation , you also can remove apk. It is interactive.

1

I wrote a Python script just to do this. GitHub repo can be found here.

#!/usr/bin/env python3

run it with sudo

from pathlib import Path import sys import typing as T import os import subprocess

def main(): pkgs = list_pkgs(sys.argv[1]) for i, pkg in enumerate(pkgs): print(f"{i}: {pkg}") idx = input("Select a package to uninstall ") pkg = pkgs[int(idx)] print(f"Uninstalling {pkg}") uninstall(pkg)

def uninstall(pkg: str): to_remove = [] for file in subprocess.run( ["pkgutil", "--only-files", "--files", pkg], text=True, capture_output=True ).stdout.split("\n"): if not file.strip(): continue p = Path(f"/{file}") # assert p.is_file(), f"{p} is not a file" to_remove.append((p, False)) for d in subprocess.run( ["pkgutil", "--only-dir", "--files", pkg], text=True, capture_output=True ).stdout.split("\n"): if not d.strip(): continue p = Path(f"/{d}") # assert p.is_dir(), f"{p} is not a dir" to_remove.append((p, True))

for path, is_dir in to_remove:
    try:
        if not is_dir:
            path.unlink()
        else:
            path.rmdir()
    except Exception:
        pass

subprocess.run(["sudo", "pkgutil", "--forget", pkg], text=True)


def list_pkgs(keyword) -> T.List[str]: pkgs = subprocess.run( ["pkgutil", "--pkgs"], text=True, capture_output=True ).stdout.split("\n") return [p for p in pkgs if keyword.lower() in p.lower()]

if name == "main": main()

Giacomo1968
  • 58,727
Dilawar
  • 245
1

You can try the suggestions from this site. Also, there's an article regarding this on the Adobe support site.

Also, the apps that usually have a PKG file in the DMG usually also have another pkg that is used for uninstalling. I'm not sure if this is true here, but I wanted to let you know to keep the original dmg file.

Giacomo1968
  • 58,727
alex
  • 18,247
0

For those who have Parallels, you'll find a tool call "Uninstall apps" (Désinstaller des applis in french) provided with their tools package