Home Package management How To Remove Unnecessary Configuration Files On Debian-based Systems

How To Remove Unnecessary Configuration Files On Debian-based Systems

By sk
Published: Updated: 29.4K views

We usually use "apt remove" command to remove an installed package in Debian, Ubuntu and its derivatives like Linux Mint and Pop OS. As you probably know, the apt remove command will only remove the given package, but won't delete the dependencies or configuration files of the removed packages. This is why the intermediate and advanced users will often use "apt purge" command that deletes the given package along with all dependencies and configuration files and maintain a clean system without any leftovers. Sometimes, you might forgot to use "apt purge" and use "apt remove" command when removing a package. Or you might already have removed packages with apt remove command and left many unused/unnecessary packages and configuration files in your system. No worries! This brief tutorial explains how to remove unnecessary configuration files on Debian-based systems.

apt remove vs apt purge vs apt autoremove vs apt clean vs apt autoclean

Before we get into the topic, let us see the difference between -  apt remove vs apt purge vs apt autoremove vs apt clean vs apt autoclean. These are the commands that we use to remove the packages, config files of removed packages and clean the local cache in any Debian-based systems..

apt remove - This command is used to remove a package but leave all of it's configuration files in place. When you reinstall the same package later, all of your settings will still be intact.

apt purge - It is same as "apt remove" command but also removes all of the configuration files.

apt autoremove - It removes any packages on your Deb-based system that are no longer required. Those packages are called unused packages. So, the "autoremove" command simply removes packages that hasn't been installed manually by the user and that's not needed by any other package in your system.

apt clean - When you use apt to install or update something from the repositories, it first downloads all the required packages to your local drive and then installs them. The "apt-get clean" command removes those downloaded packages. It doesn't uninstall any package, it just cleans that cache.

apt autoclean - Like "apt clean", the "apt autoclean" command clears out the local repository of retrieved package files. The difference is that it removes all stored archives in your cache for packages that can not be downloaded anymore.

For more details, refer man pages.

$ man apt

Or,

$ man apt-get

Now, let us go ahead and find out the files that we left behind in the system.

Find all packages that are removed but not purged in Debian, Ubuntu

First, let us list the leftover files in the system using dpkg command:

$ dpkg -l | grep ^rc

Or,

$ dpkg -l | grep "^rc"

This command will list all packages that have been removed but not purged.

Sample output:

List all packages that are removed but not purged using dpkg command in Ubuntu
List all packages that are removed but not purged using dpkg command in Ubuntu

As you may have noticed, all lines starts with rc. Here, rc is short for residual config.

You can also use the following apt command to list all packages that are removed but not purged:

$ apt list | grep 'residual-config'

Sample output:

List all packages that are removed but not purged using apt command in Ubuntu
List all packages that are removed but not purged using apt command in Ubuntu

Another way to list the leftovers from your Debian system is to use Synaptic package manager.

If Synaptic package manager is not installed, you can install it using command:

$ sudo apt install synaptic

Now launch the synaptic package manager, click on Status button in the left pane and look for the residual config files as shown in the below screenshot.

List all packages that are removed but not purged using synaptic package manager in Ubuntu
List all packages that are removed but not purged using synaptic package manager in Ubuntu

Alright, we've just found the unused or unneeded configurations files that are left behind in our system. Now it is time to get rid of them!

Remove Unnecessary Configuration Files On Debian-based Systems

There are couple ways to do this. I have given three methods to remove unwanted config files from Debian and its derivatives.

Using aptitude:

This is the easiest and shortest command to delete unnecessary configuration files from your Debian-based system:

$ sudo aptitude purge ~c

Or,

$ sudo aptitude purge ?config-files
Remove Unnecessary Configuration Files On Debian-based Systems
Remove Unnecessary Configuration Files On Debian-based Systems

Please note that aptitude command is not pre-installed by default. You can install it using command:

$ sudo apt install aptitude

Alternatively, use the following command to delete the unwanted config files:

$ sudo aptitude purge `dpkg --get-selections | grep deinstall | awk '{print $1}'`

Using dpkg command:

If you don't have aptitude installed, you can use dpkg to purge unwanted config files.

To find and delete unused config files using dpkg, run:

$ dpkg -l | grep "^rc" | awk '{print $2}' | sudo xargs dpkg -P

Or,

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

Sample output:

(Reading database ... 102520 files and directories currently installed.)
[...]
Purging configuration files for linux-image-4.15.0-20-generic (4.15.0-20.21) ...
Purging configuration files for linux-image-4.15.0-23-generic (4.15.0-23.25) ...
Purging configuration files for linux-image-4.15.0-24-generic (4.15.0-24.26) ...
Purging configuration files for linux-image-4.15.0-45-generic (4.15.0-45.48) ...
Purging configuration files for linux-image-4.15.0-46-generic (4.15.0-46.49) ...
Purging configuration files for linux-image-4.15.0-48-generic (4.15.0-48.51) ...
Purging configuration files for linux-image-4.15.0-74-generic (4.15.0-74.84) ...
Purging configuration files for linux-modules-4.15.0-20-generic (4.15.0-20.21) ...
Purging configuration files for linux-modules-4.15.0-23-generic (4.15.0-23.25) ...
Purging configuration files for linux-modules-4.15.0-24-generic (4.15.0-24.26) ...
Purging configuration files for linux-modules-4.15.0-45-generic (4.15.0-45.48) ...
Purging configuration files for linux-modules-4.15.0-46-generic (4.15.0-46.49) ...
Purging configuration files for linux-modules-4.15.0-48-generic (4.15.0-48.51) ...
Purging configuration files for linux-modules-4.15.0-74-generic (4.15.0-74.84) ...
Purging configuration files for linux-modules-extra-4.15.0-20-generic (4.15.0-20.21) ...
Purging configuration files for linux-modules-extra-4.15.0-23-generic (4.15.0-23.25) ...
Purging configuration files for linux-modules-extra-4.15.0-24-generic (4.15.0-24.26) ...
Purging configuration files for linux-modules-extra-4.15.0-45-generic (4.15.0-45.48) ...
Purging configuration files for linux-modules-extra-4.15.0-46-generic (4.15.0-46.49) ...
Purging configuration files for linux-modules-extra-4.15.0-48-generic (4.15.0-48.51) ...
Purging configuration files for linux-modules-extra-4.15.0-74-generic (4.15.0-74.84) ...
[...]

Using Synaptic package manager:

If you've Synaptic installed on your system, click on the Status section from left pane, click the check box of the check of the packages that you want to remove and click Apply button.

Remove Unnecessary Configuration Files On Debian-based Systems Using Synaptic
Remove Unnecessary Configuration Files On Debian-based Systems Using Synaptic

That's it! There are no leftovers behind in the Linux system.

Conclusion

There is no harm if you don't use "apt purge" command often. But, It is always a good practice to run "apt autoremove" and "apt autoclean" commands once in a while to remove unused dependencies and clear the local cache folder in your Debian systems.

You May Also Like

2 comments

Karl Blessing January 19, 2021 - 12:14 pm

Need to be very careful with this on some packages. For example if you previously had nginx-common installed, but went with nginx-full instead, and you use the above commands, it’ll wipe out your currently in use /etc/nginx folder because both package share the same configuration path.

Reply
sk January 19, 2021 - 7:06 pm

Thanks for the tip. I didn’t think of it. Much appreciated.

Reply

Leave a Comment

* By using this form you agree with the storage and handling of your data by this website.

This site uses Akismet to reduce spam. Learn how your comment data is processed.

This website uses cookies to improve your experience. By using this site, we will assume that you're OK with it. Accept Read More