43

How can you completely uninstall (remove files that belong to a certain package) in Mac OS X? Can this be done using a command in the terminal?

I have installed a .pkg package on my Mac and I am wondering as to how I can uninstall the entire package without using a third party application such as UninstallPKG?

I am wondering whether uninstalling .dmg files also require third party applications or is it possible to uninstall them entering a command in the terminal?

newbieMACuser
  • 687
  • 1
  • 5
  • 17
  • 4
    This question appears to be off-topic because it's not about programming. Try http://apple.stackexchange.com – Droppy Dec 18 '14 at 12:03
  • 1
    Specifically, this is the same question as http://superuser.com/questions/36567/how-do-i-uninstall-any-apple-pkg-package-file. I have recommend that we close this question since it's off topic for StackOverflow. – James McMahon Nov 25 '16 at 21:34

3 Answers3

36

Use this command in terminal for check the list of package and uninstalled your files.

$ pkgutil --pkgs # list all installed packages

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

$ sudo pkgutil --forget the-package-name.pkg

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 -i

Be careful of this last step. The list of directories output by pkgutil --files can include important shared directories like usr, which you don't want to remove.

$ pkgutil --only-dirs --files the-package-name.pkg | tr '\n' '\0' | xargs -n 1 -0 sudo rm -ir

Copied from here (Wayback Machine snapshot of the original)

TheDudeAbides
  • 1,238
  • 1
  • 12
  • 25
  • 1
    I tried the above command to remove files from a package, but it didn't work. `rm -i` prompts the user for confirmation, but stdin is coming from `/dev/null` thanks to xargs (http://man7.org/linux/man-pages/man1/xargs.1.html), so you can't confirm anything. You just get a series of many prompts, and no files removed. Did anybody else have success? – LarsH Feb 08 '17 at 17:14
  • How do you uninstall the files/how do you know which files in which directories are related to a specific package? – Tadej Aug 16 '17 at 07:09
  • @LarsH you need to use `xargs -n 1 -0 sudo rm -if`. It may take a while too. – Jorge Orpinel Oct 28 '19 at 03:49
  • @Jorge Thanks. I figured that out later; see my answer. – LarsH Oct 28 '19 at 13:08
  • If you are only removing directories in the final step, why not use `sudo rmdir` as the command? That way it will only remove directories which are empty. – Victor Zamanian Mar 13 '20 at 12:47
22

I'm modifying @karthikeyan's answer, which didn't work for me.

At a command line, use the following to find the desired package name:

$ pkgutil --pkgs | grep -i {keyword} | less

where {keyword} is a string you expect to see in the package name.

To find the package location (the root directory that all file listings will be relative to), use

$ pkgutil --pkg-info package-name.pkg

Use this to list the package's installed files:

$ pkgutil --files package-name.pkg

After visually inspecting the list of files you can do something like this to remove them:

$ cd / # assuming the package location is /
$ pkgutil --only-files --files package-name.pkg | tr '\n' '\0' | xargs -n 1 -0 -p sudo rm

Be careful of the next (final) step, which removes directories. The list of directories output by pkgutil --files can include important shared directories like usr, which you don't want to remove. -p causes xargs to prompt for confirmation, but don't get trigger-happy. (You should be safe with rmdir too, because it will only remove empty directories. But some people will need to tweak the command line, so it's better to be clear!)

$ pkgutil --only-dirs --files package-name.pkg | tr '\n' '\0' | xargs -n 1 -0 -p sudo rmdir

Once you've uninstalled the files, you can remove the system record of that package:

$ sudo pkgutil --forget package-name.pkg

Sources: pkgutil man page and this post.

LarsH
  • 25,732
  • 8
  • 77
  • 136
  • 2
    The `.pkg` extension isn't needed, at least on the pkg-info step. – Jon L. May 14 '17 at 03:45
  • Presumably, if the info step says `location: Applications/xxx.app` and the `--files` step shows me nothing outside `Contents/` I can feel fairly confident that just dragging the app to trash has removed everything? – Chris F Carroll Mar 08 '18 at 14:35
  • There is also `pkgutil --unlink `, although not documented – ᆼᆺᆼ Jan 21 '19 at 20:11
  • See also https://gist.github.com/githubutilities/b5318d08a4b970d104f1. (very similar, more compact) – Jorge Orpinel Oct 28 '19 at 04:07
  • @Jorge Please explain your edit: why do you consider `-i` on the `rm` preferable to `-p` on the `xargs`? – LarsH Oct 28 '19 at 13:14
  • @LarsH I tied both and `-if` worked (no prompt). Actually `-i` may not even have an effect given `-f`... Should we get rid of `-i`? Would have to try it first to double check but I already uninstalled the package in question. – Jorge Orpinel Oct 28 '19 at 23:02
  • @Jorge Are you saying that `-p` did or didn't prompt you? The purpose of the `-p` is to prompt the user. If you're not sure what the purpose of the code is, please don't change it. – LarsH Oct 29 '19 at 05:04
  • So your code's purpose is to prompt the user for confirmation to delete thousands of files one by one? Yes, I definitely never imagine anyone would want that. Feel free to worsen your answer again if you want, lose some votes. Cheers – Jorge Orpinel Oct 31 '19 at 18:44
  • @Jorge There's no need to descend into insults. If you don't want prompts, why did you add `-i` on the `rm` command? The purpose of `-p` on `xargs` is to prompt before the removal of a group of files. Whether there are thousands of files, or three, depends on the package. They're grouped by xargs, so you don't get a prompt for each one. A user who wants to remove the prompting can do so by removing `-p`. This is a much safer default than the other way around. Removing the `-p` and adding `-f` to the `rm` is profoundly unsafe for the user who copies and pastes the commands given here. – LarsH Oct 31 '19 at 19:40
  • Forgive me. I never meant to insult you by trying to improve the answer. I'm just a very honest person. Again, please re-edit your answer as you best see fit, I will stay out of it. Cheers – Jorge Orpinel Nov 01 '19 at 01:37
7

The concept of PKG uninstallation is not there in OS X. A PKG/MPKG can have certain pre install and post install scripts associated with that. What is done in PKG scripts is always upto the PKG creator. As a layman we cannot go ahead and uninstall a PKG.

But at the same time there are command lines that can do a complete reverse engineering on PKG files. It can extract the scripts and other related information. The links given below will give you some info

http://s.sudre.free.fr/Stuff/Ivanhoe/FLAT.html http://www.mactech.com/articles/mactech/Vol.26/26.02/TheFlatPackage/index.html

There are few uninstall/cleaner apps available on OS X. Most of them do a search in known directories (like /Library/Preferences, /Library/Application Support etc) with the app name/pkg name or bundle identifier. This is one way to do a complete removal.

DMG are not installer files. They are Disk Image files. It can inturn have .apps, pkg, mpkgs, other files etc. What gets installed is the installers inside DMG. DMG has to be mount to read the contents and un-mount when done.

http://osxdaily.com/2011/12/17/mount-a-dmg-from-the-command-line-in-mac-os-x/

This link gives info on how to mount a DMG

Seema Kadavan
  • 2,118
  • 14
  • 24