Plain apt autoremove clears out dependencies that nothing needs anymore. It leaves their config files behind.
sudo apt autoremove --purge
The man page says --purge makes removals use purge instead of remove, which deletes the configuration files as well. Over a few years of installing and removing things, that adds up to a lot of stale files in /etc.
I look first. Run it once with the simulate flag:
apt-get -s autoremove --purge
Read the list. If a package in there surprises me, I stop and ask why apt thinks it is unused. Usually it was pulled in as a dependency of something I removed. Sometimes it is a thing I installed by hand and never marked as manual, and then apt-mark manual saves it.
Run it after a big cleanup too. Removing a desktop app tends to leave a long tail of libraries behind, and this is the command that sweeps them up.
Caveat: purge is permanent. If you ever want that package back with your old settings, you will not get them. Take a copy of any config you care about first.
Further reading: apt-get(8) man page (Ubuntu 24.04), On this site: apt changelog shows why a package wants to update, On this site: –no-install-recommends stops apt from dragging in extras