Using apt-get to remove packages and dependencies

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

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.