Purge packages that are deinstalled

In cmd-fu, System Maintenance on July 25, 2009 at 12:13 pm

Before starting on the command, an explanation of why this is generally useful. When a package is deinstalled (because it’s not required or because a newer version of the package is installed), the package is removed from the system, but the configuration files remain. To remove the config files too, you need to purge the package.

If you have manually configured a package earlier, and would like to retain the configuration files for when you’ll install the package in future, this is not a command you should run.

In all other cases,  you should purge the packages in the interests of good housekeeping. If that isn’t enough motivation, it also means that future installs of the package get a blank state, and there are no potentially messy interactions of earlier version config files with newer packages.

With that out of the way, lets get down to bass tacks:

dpkg --get-selections | grep "[[:space:]]deinstall$" | awk '{print $1}' | sudo xargs dpkg -P

The first command in the pipeline returns the list of packages in the system, the second filters out the deinstalled packages, the third filters out the package names from the previous output, and the fourth purges each package. Consequently, only the fourth command requires sudo permissions.

There’s a downside to running this command – you cannot stop the command by hitting <Ctrl-C> on the terminal as the xargs command spawns an individual process for each dpkg -P

On linux systems, there are typically many ways to achieve the same result, so here’s another way to purge deinstalled packages:

sudo aptitude purge $(aptitude search ~c -F "%p")

So you know what’s happening, here’s a brief explanation of what the command does. aptitude search -c returns the list of deinstalled packages. The -F “%p” argument ensures only the package names are printed. The outer aptitude purge command operates on the returned list of packages from the inner command.

An example of why you’d run this is to purge old linux kernels.

Before running such commands, it is imperative you know what you are running, and what the results are, so I’d suggest you run it in smaller chunks and examine the results before running the actual command.


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: