Removing package cruft from a Debian or Ubuntu system
Over time, especially with Ubuntu and it’s 6 month release cycle, a system can contain a number of packages that are no longer needed but still present on the system in some way. I have a number of techniques I use to locate these packages and remove them and I thought I would share them.
apt-get autoremove
A good first step is to remove any packages that the system can detect are no longer needed. Of course, there can be false positives in this detection so, with all of these commands be careful.
sudo apt-get autoremove --purge
deborphan
Another useful tool is the deborphan command which can show libraries (or more) with no dependencies. Unfortunately I have to caution that this tool has a fair number of false hits on my desktop. So remove packages with caution. Sometimes dependency information is not correct and packages which are needed are listed as orphans.
sudo apt-get install deborphan- This will ensure that deborphan is installed on the system.
deborphan- This will show libraries that are not depended on by any installed package.
deborphan --guess-all- This will be a much larger list that will include development packages.
These listings can be fed into an apt-get remove statement thusly:
deborphan -e exludedpackages | xargs sudo apt-get remove- This statement will fail but will show the results of your actions. It’s important there are no unwanted side-effects before continuing.
deborphan -e exludedpackages | xargs sudo apt-get remove --purge -y- This will not prompt before removing packages so be sure that you are aware of exactly what will be done.
Transitional Packages
Another set of packages to remove are transitional packages. These are packages that have new names and the old packages are just placeholders. I found these by using the following command:
dpkg -l | grep transitional
Unpurged Packages
To find packages which have been removed but not purged (deleted completely from the system) I use the following command:
dpkg -l | grep ^rc
These packages can be purged with the following command:
dpkg --purge packagename
To find all improperly installed packages the following command will work:
dpkg -l | grep -v ^ii
Depending on the nature of the problem will determine how to deal with these packages. Some can be removed, others may need to be reinstalled.
Update: To purge all removed packages the following can be used, though it should be used with the utmost caution:
dpkg -l | grep ^rc | cut -c5-39 | xargs sudo dpkg --purge
Order and Repetition
The order with which these commands are entered does matter. Often I have to cycle through them repeatedly to completely clean up a system. But every so often the effort is worth it to keep from having to download updates for software that is not needed and to keep the system as trim as possible to avoid bugs and security issues.




another command for removing (non-purged) packages
Hi,
another way to get a list of packages that have been removed (but not purged) from your system is:
dpkg -l | grep ^rc | awk '{printf $2 " "}'
so if you would like to (completly) remove them just use:
aptitude purge `dpkg -l | grep ^rc | awk '{printf $2 " "}'`
btw, aptitude is recommended way of updating .deb based systems for some time ;-)