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

29,574

Solution 1

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

Solution 2

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

Solution 3

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
Share:
29,574

Related videos on Youtube

Murz
Author by

Murz

Updated on September 18, 2022

Comments

  • Murz
    Murz almost 2 years

    Sometimes, when I upgrade a system via do-release-upgrade, the update process fails and the script tells me to finish the process manually via dpkg --configure -a. This is a not serious problem - after manually solving the dependencies problem and the finishing upgrade process all works well.

    But the do-release-upgrade script has a cleanup process after finishing the upgrade:

    Remove obsolete packages?  
    XXX packages are going to be removed.  
    Continue [yN]  Details [d]y
    

    which removes some obsolete packages from old version of system.

    Seems that this process doesn't execute when the automatic upgrade fails and I finish upgrade manually, so obsolete packages from old version remain installed in the system.

    How I can run the "Remove obsolete packages" process manually after finishing the upgrade by hand?

  • Murz
    Murz over 9 years
    sudo apt-get autoremove is remove only little part of packages, that removes do-release-upgrade, for example do-release-upgrade on same system removes about 150 packages, but apt-get autoremove - remove only about 5-10 packages.
  • Murz
    Murz over 9 years
    Thanks, those commands is exactly that I want, main of them is dpkg -l|grep "^rc"|awk '{print $2}' that shows packages to remove. This is strange that apt-get or aptitude dont' want to see it for removing.
  • Murz
    Murz over 9 years
    One-line cli command for remove obsolete packages after failed do-release-upgrade based on your example is: sudo dpkg -l|grep "^rc"|awk '{print $2}' | xargs sudo apt-get remove -y --purge. Attention, this command removes packages without confirmation, but is useful for automate quickly cleanup systems.
  • Iluvathar
    Iluvathar about 8 years
    On my system the '^rc'-marked packages weren't even installed, but the upgrade failed right before searching for obsolete packages. So this answer seems incomplete. This won't remove all packages which would be by do-release-upgrade, should it succeed.
  • Daniel Alder
    Daniel Alder over 4 years
    note the difference between orphaned packages and obsolete packages. And that the OT wanted to remove obsolete packages - askubuntu.com/questions/286947/…
  • Eric FD
    Eric FD over 4 years
    @Daniel Alder the OT wanted to remove the obsolete packages after a failed release upgrade. What the release upgrade does as the final step after replacing the repositories and executing a full upgrade, is it removes orphaned packages. Therefore this is the last step to be repeated manually after a failed release upgrade.
  • sc911
    sc911 about 3 years
    Is this still valid in 2021? Ubuntu 20.04 LTS?