I've recently been reading through a lot of questions/answers/opinions about removing packages and dependencies. There's a lot of different ways to do it, some being overly convoluted and some being insufficient/incomplete. I want to ask here in one place what others think is the best ways to do go about clearing out a package and everything including configs, dependencies, and the dependencies' configs. I'll offer my method as well that came from a lot of trial and error and hair-pulling.

3 Answers 3


Warning: This can remove packages that you might still want. If this happens just reinstall them. But honestly when you're installing/uninstalling something that's gonna make big changes to your system...why not go ahead and backup first.

The most thorough, method I've ever come up with to completely remove a package PLUS its dependencies PLUS all configs including those configs of dependencies and do a little housecleaning is this where PACKAGENAME is the main package to be removed:

  • Log out from the desktop and press Ctrl+Alt+F1 then login to TTY1 and run the following commands:

    sudo apt-get purge <PACKAGENAME>
    sudo apt-get purge $(apt-cache depends <PACKAGENAME> | awk '{ print $2 }' | tr '\n' ' ')
    sudo apt-get autoremove
    sudo apt-get update
    sudo apt-get check
    sudo apt-get -f install
    sudo apt-get autoclean
  • Restart if needed

    sudo shutdown -r now

So what's going on in the second line? The pipes take the output from apt-cache depends and reformat it. The first pipe awk '{ print $2 }' takes the output of apt-cache depends and prints, or "echoes", only the second column. Without it you'd also have in the list another column which is the dependency type, i.e. "depends", "recommends", etc. Then the second pipe tr '\n' ' ' takes that result and removes, or truncates (hence tr), the newline(s) and replaces them with a space that separates the names. All of this returns a "space-delimited" list of the names of all dependency packages of PACKAGENAME that's format-friendly for use with multiple package input to purge command-option.

This works especially well for meta packages. I run the last command "sudo apt-get -f install" at the end to check for possible broken packages and fix them after making so many changes all at once. I especially do this anytime I add a "DE" and want to go back. Just recently I used this after installing GNOME and it even fixed the fact that only purging Gnome with autoremove after still left my GRUB changed and left the login option in the DM. When I had tried out lubuntu-desktop it fixed some conflicts and removed the entries from the DM login that were left by purge and autoremove.

A similar method which works well, is probably safer, but still doesn't always get everything is:

sudo apt-get --purge autoremove PACKAGENAME

Which may or may not need to be followed up with:

sudo apt-get update
sudo apt-get check
sudo apt-get -f install
sudo apt-get autoclean
  • It looks like a great way to do it, I understand all but the last part of the 2nd line "awk '{ print $2 }' | tr '\n' ' ' )" could you please elaborate on what those 2 pipes are doing?
    – TrailRider
    Dec 12, 2015 at 7:04
  • Sure. I edited to add some explanation. Dec 12, 2015 at 10:44
  • So ... you're purging the depends without checking whether other packages might also depend on them? Dec 12, 2015 at 11:57
  • Yes, there is a possibility of adverse side effects which I noted. I find myself using this method from time to time as a last ditch effort when remove PACKAGENAME, autoremove PACKAGENAME, --purge autoremove PACKAGENAME, etc, still doesn't do the job. This happens usually with meta packages. I just pay attention to what's listed to be removed before pressing "Y" and if something comes up that doesn't look right I cancel and investigate. An addition of an extra pipe, before awk, with grep recommends, grep replaces, or grep conflicts, etc, can be used also if needed to step through slower. Dec 13, 2015 at 12:30

I also use aptitude purge for that:

sudo aptitude purge <PACKAGENAME>

This command

  • removes the dependencies
  • without removing the dependencies which are also dependencies of some other installed package,
  • but i'm not sure if it removes the dependencies' configs.
  • 1
    I can confirm it does not remove confgs. Oct 18, 2018 at 5:33

This will remove specified package(s), all auto-installed packages and all their configs:

sudo apt-get -s purge <PKG> | grep '^ ' | tr -d '*' | xargs -o sudo apt-get purge

Or as root:

apt-get purge `apt-get -s purge <PKG> | grep '^ ' | tr -d '*'`

Substitute <PKG> with package or packages you want to remove completely.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.