Purge Removed Packages On Ubuntu/Debian

debian
When a package is uninstalled, its configuration files are left on the system, in case you’ll want to install it again. Purging a package gets rid of these configuration files. This not only frees disk space, but also helps maintain the system clean. It’s easy to forget to add –purge to each apt-get or aptitude remove command you run, but there’s an easy way to purge packages after you’ve removed them:

Here’s a trick you can use to purge packages that have already been removed with apt-get remove or aptitude remove.

Check results of the following command.

$ aptitude search '~c'

If you think listed packages are OK to be purged, execute the following command.

$ sudo aptitude purge '~c'

Another way to using dpkg:

 $ dpkg -l |awk '/^rc/ {print $2}' |xargs sudo dpkg --purge

You may want to do the similar in the interactive mode for fine grained control.
Start aptitude in interactive mode.

$ sudo aptitude

You provide the regex “~c” in the “New Package Tree Limit” view with “l” prompt.

Screenshot-1
This limits the package view only to regex matched packages, i.e., “removed but not purged”.

Screenshot-2

All these regex matched packages can be shown by pressing “[” at top level headings.
Screenshot-3

Then you press “_” at top level headings such as “Installed Packages”. Only regex matched packages under the heading are marked to be purged by this. You can exclude some packages to be purged by pressing “=” interactively for each of them.

Screenshot-4

Finally press “g” to perform all pending installations, removals, and upgrades.

Screenshot-5

This technique is quite handy and works for many other command keys.