Here is a simple command that will meet your request:
dpkg --purge $(dpkg --get-selections | grep deinstall | cut -f1)
Explanation:
dpkg --purge: remove package if present and remove config files
dpkg --get-selections: Get list of package selections and write it to stdout
grep deinstall: limit to packages that have been removed but config kept
cut -f1: only show first column (package name)
You can simulate the command by adding the --dry-run flag before the purge action:
dpkg --dry-run --purge $(dpkg --get-selections | grep deinstall | cut -f1)
... grep 'deinstall$' .... – Murphy Jul 16 '24 at 10:06