46

I have a list of packages on my system, that were installed and removed again, but not purged, i.e. there are still a lot of conffiles etc. laying around.

The output of dpkg --get-selections | grep deinstall lists about 85 different packages which I don't need and want to be purged entirely.

So my short question, which I decided to finally ask after experimenting around has lead to this problem, is:

How do I completely purge previously installed packages that are already removed?

Reinstalling and then purging is not an option, of course.

Byte Commander
  • 107,489
  • 1
    "Reinstalling and then purging is not an option, of course." LoL ;) – TellMeWhy Oct 19 '15 at 12:45
  • @DevRobot I don't see the joke. It would be possible and pretty surely work, but don't have the time to download and install tons of packages just to get rid of them... – Byte Commander Oct 19 '15 at 12:47
  • 1
    I know - it's the of course - relates :) – TellMeWhy Oct 19 '15 at 12:51
  • I think you can do sudo apt-get purge [package] after you have removed them. I just tried it and it worked. – Jos Oct 19 '15 at 12:53
  • I often use aptitude for that -- pressing _ on the line Not installed packages will mark all of these packages for configuration file removal. Not an answer because aptitude is likely going away. – Simon Richter Oct 20 '15 at 02:18
  • 2
    This is not a duplicate. The referenced article solves the problem for a single package. This is about cleaning up multiple packages without manually fiddling around. – Andrew Apr 11 '18 at 00:13
  • Here is an easy answer to this question which is marked as a duplicate but is not one: aptitude purge ?config-files of course you will need to have installed aptitude – Rolf Feb 07 '19 at 23:46

5 Answers5

58

I just found the following command which worked:

sudo apt-get purge $(dpkg -l | grep '^rc' | awk '{print $2}')
Byte Commander
  • 107,489
28

dpkg --get-selections | grep deinstall produces a list of package names with the word "deinstall":

$ dpkg  --get-selections | grep deinstall
account-plugin-windows-live         deinstall
debarchiver                         deinstall
flashplugin-installer               deinstall
    ...

By asking awk to print only the first field we get:

$ dpkg --get-selections | awk '$2 == "deinstall" {print $1}'
account-plugin-windows-live
debarchiver
flashplugin-installer
    ...

Now that we have the list of packages, xargs will let us feed the list of packages to a command (or commands, if the list is long enough):

dpkg --get-selections | awk '$2 == "deinstall" {print $1}' | xargs sudo apt-get purge --dry-run

When you are happy with the simulated results, replace --dry-run with -y in the apt-get command.

Relevant documentation:

man dpkg awk xargs apt-get
rvighne
  • 421
  • 1
  • 3
  • 14
waltinator
  • 36,399
  • 6
    On my system , Ubuntu 15.04, I had to add the -y option to apt-get to stop the command from aborting before the packages were removed. – ankh-morpork Oct 20 '15 at 01:24
  • For those that don't awk, you can use grep deinstall | cut -f1 instead of awk '$2 == "deinstall" {print $1}'. Using cut may actually flow better with this style write up. – cbarrick Feb 15 '19 at 17:45
  • Very nice answer. A simpler alternative would be to verify that aptitude search '~c' emits the list of packages you want to remove and then you just do sudo aptitude purge '~c' to remove all those packages. Also try aptitude search '~o' to list obsolete packages (that is, packages that are no longer supported by any repository you have). – Mikko Rantalainen Mar 04 '19 at 10:58
4

My fifty cents, a simple oneliner:

First test with

dpkg --get-selections | awk '$2=="deinstall" {system("sudo apt-get --dry-run purge "$1)}'

and bye bye

dpkg --get-selections | awk '$2=="deinstall" {system("sudo apt-get -y purge "$1)}'

Example

% dpkg --get-selections | grep deinstall
nginx-common                    deinstall

% dpkg --get-selections | awk '$2=="deinstall" {system("sudo apt-get -y purge "$1)}'

% dpkg --get-selections | grep deinstall
[no output]
A.B.
  • 90,397
  • Or sudo apt-get --dry-run purge $(dpkg --get-selections | awk '$2=="deinstall" { print($1) }') to avoid spawning a new shell for every package installed. Replace --dry-run with -y once you feel comfortable about the operation. – Per Lundberg Jan 11 '23 at 08:02
3

If you just want to purge the whole list, you can use this command; it will perform a dry run, in case essential packages are going to be removed, which you probably don't want to happen:

dpkg --get-selections | sed -n 's/\tdeinstall$//p' | xargs sudo apt-get --dry-run purge

If no essential package is going to be removed, it's safe to run the actual command:

dpkg --get-selections | sed -n 's/\tdeinstall$//p' | xargs sudo apt-get --yes purge
  • sed -n 's/\tdeinstall$//p': prints only lines in stdin where a tabulation followed by a deinstall string could be removed from the end of the line; this has the effect of printing only the lines containing a tabulation followed by a deinstall string at the end of the line without the actual tabulation followed by the deinstall string at the end of the line
  • xargs sudo apt-get --yes purge: passes each line in stdin as an argument to sudo apt-get --yes purge
kos
  • 35,891
1

I asked this myself a couple of days ago. Came up with

apt-get purge $(dpkg -l | awk 'BEGIN{ORS=" "} /^rc/{ print $2}')

The removed but not purged packages appear in the output of dpkg -l with rc at the beginning. awk picks out the second column aka the name of the package and prints them space-separated.

Nephente
  • 5,595
  • 1
  • 17
  • 23