How to remove obsolete packages after failed release upgrade via do-release-upgrade on Ubuntu?

Test this:

Open a terminal (press Ctrl+Alt+T).

Run this:

sudo -i

apt-get update
apt-get autoremove
apt-get clean
 
UNUSCONF=$(dpkg -l|grep "^rc" | awk '{print $2}')
apt-get remove --purge $UNUSCONF

NEWKERNEL=$(uname -r|sed 's/-*[a-z]//g'|sed 's/-386//g')

ADDKERNEL="linux-(image|headers|ubuntu-modules|restricted-modules)"

METAKERNEL="linux-(image|headers|restricted-modules)-(generic|i386|server|common|rt|xen)"

UNUSKERNELS=$(dpkg -l | awk '{print $2}' | grep -E $ADDKERNEL | grep -vE $METAKERNEL | grep -v $NEWKERNEL)

apt-get remove --purge $UNUSKERNELS

update-grub

I believe the "Remove obsolete packages" actually just runs a sudo apt-get autoremove. Try it, see if it helps.


I find that this answer of an unrelated question might provide a utility and command that seems to remove more of the unused stuff:

  1. Install the "deborphan" package.
  2. sudo deborphan | xargs sudo apt-get -y remove --purge