How to purge previously only removed packages?
Solution 1
I just found the following command which worked:
sudo apt-get purge $(dpkg -l | grep '^rc' | awk '{print $2}')
Solution 2
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
Solution 3
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]
Solution 4
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 instdin
where a tabulation followed by adeinstall
string could be removed from the end of the line; this has the effect of printing only the lines containing a tabulation followed by adeinstall
string at the end of the line without the actual tabulation followed by thedeinstall
string at the end of the linexargs sudo apt-get --yes purge
: passes each line instdin
as an argument tosudo apt-get --yes purge
Solution 5
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.
Related videos on Youtube
Byte Commander
Ask Ubuntu moderator♦, IT student and DevOps engineer. I love Ubuntu, Python, good music and coffee, although not necessarily in that order. You can easily contact me in the Ask Ubuntu General Room most of the time, or on Discord as @ByteCommander#2800. I'd also love to invite you to my Ubuntu Hideout Discord Server btw. PS: My profile picture is derived from "Wolf Tribals" by user HaskDitex (DeviantArt.com), which is under creative Commons 3.0 License. Currently I'm using the character "Dregg Morriss" from the game "Medieval Cop" by Vasant Jahav ("Gemini Gamer"). It can be found here.
Updated on September 18, 2022Comments
-
Byte Commander over 1 year
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.
-
TellMeWhy over 8 years"Reinstalling and then purging is not an option, of course." LoL ;)
-
Byte Commander over 8 years@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...
-
TellMeWhy over 8 yearsI know - it's the of course - relates :)
-
Jos over 8 yearsI think you can do
sudo apt-get purge [package]
after you have removed them. I just tried it and it worked. -
Andrew about 6 yearsThis 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.
-
Rolf about 5 yearsHere 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 installedaptitude
-
-
ankh-morpork over 8 yearsOn my system , Ubuntu 15.04, I had to add the
-y
option toapt-get
to stop the command from aborting before the packages were removed. -
Rolf over 6 yearsHmm, I suspect this will hit the command line argument length limit if you have 50000 uninstalled package. waltinator's solution below does not. Not that I see that ever happening.
-
Rolf about 5 yearsEven easier:
aptitude purge ?config-files
-
cbarrick about 5 yearsFor those that don't awk, you can use
grep deinstall | cut -f1
instead ofawk '$2 == "deinstall" {print $1}'
. Usingcut
may actually flow better with this style write up. -
Mikko Rantalainen about 5 yearsVery 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 dosudo aptitude purge '~c'
to remove all those packages. Also tryaptitude search '~o'
to list obsolete packages (that is, packages that are no longer supported by any repository you have). -
Tobia almost 5 years
dpkg -l | awk '/^rc/{print $2}' | sudo xargs apt-get purge
You're welcome. -
Tommy Jollyboat over 3 years@Tobia that last part should be
sudo xargs apt -y purge
, to avoid it attempting to ask for confirmation in a subshell. -
Ismael Luceno almost 3 yearsOr just using dpkg:
dpkg -l | awk '/^rc/ {print $2}' | sudo xargs dpkg -P
-
mydoghasworms over 2 yearsThere really should be a standard command in apt for doing this.
-
mavavilj over 2 yearsIs there some nicer method now by 2021?