How to Completely Remove (Purge) Previously Removed Packages on Ubuntu

Cohen Carlisle
3 min readMay 25, 2018

--

TL;DR

dpkg -l | grep ^rc

Confirm you want to purge the listed packages, then:

dpkg -l | grep ^rc | awk '{print $2}' | xargs -t sudo apt-get -y purge

Detailed Explanation

If you’re like me, have a self-diagnosed case of mild OCD, and work on Ubuntu (or other Debian-based OSes), you may from time to time noticed that you have many packages that are in a removed or deinstalled state. These packages still show up if you list packages using dpkg -l, along with other installed packages. Here’s an example:

$ dpkg -l | head
Example output of `dpkg -l | head`

When the lines begin with rc (not shown here), this indicates that the package has been removed but configuration files remain. (The first three lines of the output explain in more detail what the first three characters of each line mean.)

So to find the lines that begin with rc, we pipe the output of dpkg to grep:

$ dpkg -l | grep ^rc

Now we have a list of all removed packages and associated information, but we need to extract just their names to purge them. Pipe the output to awk to select the second word of each line:

$ dpkg -l | grep ^rc | awk '{print $2}'

Finally, with the list of removed package names, we need to pass this to apt-get to purge them. To do so, we can pipe to xargs:

$ dpkg -l | grep ^rc | awk '{print $2}' | xargs -t sudo apt-get -y purge

The last pipe is mildly interesting. We use -t to enabled verbose mode on xargs which will print the command it is executing at the beginning of its output. Because xargs is running non-interactively, we need to also pass the -y option to apt-get to tell it to automatically answer yes to all prompts.

If you want to know more about any of these commands, don’t by shy about looking them up. Search the web or read the system’s manual page. For example, to read about awk, use man awk.

Happy purging!

--

--