8

I've noticed a ton of packages with the residual-config status in the apt list and before deleting them I'd like to make sure it will not delete any important information, how can I show the list of files that will be deleted?

5
  • Re. "how can I show the list of files that will be deleted?" Please add the command you intend using to perform the deletion.
    – DK Bose
    Jan 11, 2016 at 1:42
  • Hi, thanks for trying to help, but I've already found the answer, just posted it here to share. Unfortunately, I still can't mark it as answered for 2 days.
    – int_ua
    Jan 11, 2016 at 1:59
  • But there is still room for improvement. You can try writing a script that concatenates all the relevant postrm scripts, for example. As for the command: I usually use apt-get purge.
    – int_ua
    Jan 11, 2016 at 2:02
  • When I run apt list, I just get a list of packages like this: package_name/version/(amd64|i386|all). No mention of the status. How do you get that with apt list? I can get the same information using dpkg --list | grep ^rc.
    – DK Bose
    Jan 11, 2016 at 2:25
  • It just lists all available packages, at least with apt 1.0.10.2ubuntu1 this should show you all packages with a status: apt list | grep "\[.*\]"
    – int_ua
    Jan 11, 2016 at 4:19

3 Answers 3

9

To see what will be removed you'll have to check the corresponding postrm script, as in /var/lib/dpkg/info/packagename.postrm

From man dpkg (shortened):

Note: some configuration files might be unknown to dpkg because they are created and handled separately through the configuration scripts. In that case the package's postrm script has to take care of their removal. This only applies to files in system directories.

Purging of a package consists of:

  1. Removing the package.

  2. Running postrm script.

2
  • 4
    Might be worth adding the following command to this answer: for i in $(dpkg --list | awk '{if ($1 == "rc") {print $2}}'); do if [ ! -f /var/lib/dpkg/info/$i.postrm ]; then sudo apt-get purge -y $i; fi; done Jan 3, 2017 at 13:27
  • 1
    It filters out all the packages that are in rc status, but don't have a postrm script (and hence do nothing). (I've just removed 20 or so such packages). Jan 3, 2017 at 13:29
8

Here is a one-liner to show you all packages with residual configuration files and a list of said files per package:

apt-cache show '?config-files' | grep -oP '^Package: \K(.*)$' | uniq | xargs -I '0' dpkg-query -W --showformat "\${binary:Package}:\n\${db-fsys:Files}" '0'

Example output:

postgresql-13:
postgresql-common:
 /etc
 /etc/init.d
 /etc/init.d/postgresql
 /etc/logrotate.d
 /etc/logrotate.d/postgresql-common
 /etc/postgresql
 /etc/sysctl.d
 /etc/sysctl.d/30-postgresql-shm.conf
 /var
 /var/lib
 /var/lib/postgresql
 /var/log
 /var/log/postgresql

Explanation:

  • apt-cache show '?config-files': Show obsolete config files as per apt search patterns.
  • grep -oP '^Package: \K(.*)$' | uniq: filter package names and remove duplicate outputs from the previous step.
  • xargs -I '0' dpkg-query -W --showformat "\${binary:Package}:\n\${db-fsys:Files}" '0': forward the package names to dpkg-query and print the fields Package (the package name) and a special db-fsys:Files field which lists all installed files from that package currently on your system.

Note: you can also purge your system of all residual configuration files using the command:

sudo apt purge '?config-files'

This doesn't touch installed packages, but I advise caution.

WARNING: apt purge '?config-files' CAN DELETE ARBITRARY THINGS YOU DON'T EXPECT. Please be careful when using it and make backups of your important data first (databases, credentials, etc). Try using --dry-run first if you're unsure.

6
  • Thanks, that's a great answer. Unfortunately, I cannot mark it as the main accepted, at least not yet. There are files mentioned in postrm scripts that are not in the db-fsys:Files For example, I checked the tor package and the postrm contains rm -f "/etc/apparmor.d/disable/system_tor" while it's not in the dpkg-query -W --showformat "\${binary:Package}:\n\${db-fsys:Files}" tor. Or is it expected?
    – int_ua
    Jan 27, 2022 at 21:26
  • @int_ua You're welcome, and that's the expected behaviour since anything not explicitly installed by the .deb package is something that apt doesn't know about, but it gives you a general idea of where to look since most packages will explicitly create their configuration directories upon installation using this method. Jan 28, 2022 at 10:36
  • 1
    Thank you so much! One quick note: some packages may not show up with configuration files. In my example, I had LXD installed and I was uncertain about the effects of the suggested action to remove the package. I went to packages.ubuntu.com searched for the package, and after a bit of more research (switching between past releases), guessing, and looking at the output of snap list I felt confident that it became a transitional package and the functionality is now provided by a snap, thus the package was indeed safe to remove.
    – LiveWireBT
    Apr 30, 2023 at 10:58
  • 1
    Do back up everything before running apt purge '?config-files'; it erased my maria database!
    – Lamp
    Jun 5, 2023 at 6:40
  • 1
    @Lamp yep. It's a terrible design imo. They should add a '?data-files' or something for treating things like that separately from config files. I'll add a warning to the answer. Jun 6, 2023 at 9:07
2

Combining answers from @voodooattack and @int_ua

#!/bin/bash

set -eo pipefail

apt-cache show '?config-files' | grep -oP '^Package: \K(.*)$' | uniq | while read -r pkg; do
    dpkg-query -W --showformat "\${binary:Package}:\n\${db-fsys:Files}" "${pkg}"
    if [[ -e /var/lib/dpkg/info/${pkg}.postrm ]]; then
        echo " postrm:"
        sed 's/^/  /' /var/lib/dpkg/info/${pkg}.postrm
    fi
done

Example output on my system:

$ apt-list-residual-files.sh 
libatk-adaptor:amd64:
 /etc
 /etc/X11
 /etc/X11/Xsession.d
 /etc/X11/Xsession.d/90atk-adaptor
 /etc/environment.d
 /etc/environment.d/90atk-adaptor.conf
xbrlapi:
 /etc
 /etc/X11
 /etc/X11/Xsession.d
 /etc/X11/Xsession.d/90xbrlapi
 postrm:
  #!/bin/sh
  set -e
  # Automatically added by dh_installdeb/13.11.6ubuntu1
  dpkg-maintscript-helper rm_conffile /etc/X11/Xsession.d/60xbrlapi 6.1\+dfsg-2\~ xbrlapi -- "$@"
  # End automatically added section

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .