Why doesn't Ubuntu remove old kernels automatically?

Is there a reason why Ubuntu wont do this automatically?

I can see only 1 reason: it does not work flawlessly; there is no clean way to currently decide what the definition of "old kernels" is. "old" does not mean "unused" nor does it mean "unwanted". And any mistake in this will kill a users' machine.

So up to now the manual method is preferred since this puts the power of removing into the users' hands.


Resources for this conclusion:

Ubuntu WIKI: Proposal for removing old kernels

Last-good-boot is implemented fully in Intrepid/8.10 final, however it has been disabled because it was not considered stable enough. The setting is a single line in the file /etc/default/kernel-helper-rc.


Launchpad: Should aptitude provide a way to remove old kernel versions ?

apt-get has an autoremove feature that uninstalls all packages that are not needed as dependencies and have not been installed manually. Since Ubuntu 14.04 all obsolete kernels and headers should automatically be flagged as no more needed, and thus can be purged with the apt-get autoremove command. (There are reports that this does not yet fully work). I am not aware of a feature in aptitude that is similar to apt-get's autoremove.


Ubuntu-devel: Distro-provided mechanism to clean up old kernels

While agreeing that it would be quite helpful and seems appropriate to have the cleanup automatic, there is a slight potential pitfall (or two). There are various flavours of kernels and people may or may not deliberately have those installed in parallel. Also various releases had sometimes a changing set of depending packages. For a while this should be only linux-backports-modules (there had been linux-ubuntu-modules and linux-restricted-modules). Though this is not so much of a problem. From a pattern matching point of view the generic-pae kernels are a bit of a pain as they tend to ruin the "use the last part of a split by "-" for the flavor". But anyway, I think the main issue is the various flavours, so a cleanup that is automatic should retain the last three of each, even though this may tend to leave more kernels around.


There are two things going on here:

  1. New kernels are completely new packages, not updated packages with a newer version number, so installing a new kernel image doesn't replace any older ones. They coexist.

  2. Ubuntu uses a bunch of apt magic to protect the last two kernel versions from 'apt-get autoremove' (the latest one, and the one that was last booted). See /etc/apt/apt.conf.d/01autoremove-kernels for details.

What this all means is that once you've rebooted into the latest kernel, 'apt-get autoremove' should remove all but the current and last booted kernel packages.

Old kernels still accumulate in /boot because autoremove is not enabled in Ubuntu by default - you must either run it manually every so often, or you must enable it.

This may change in 16.04 - a bugfix to the unattended-upgrades package will enable autoremove of old kernels by default.


This script will do the job. It ll remove all kernels that are not in use.

sudo dpkg -l 'linux-*' | sed '/^ii/!d;/'"$(uname -r | sed "s/\(.*\)-\([^0-9]\+\)/\1/")"'/d;s/^[^ ]* [^ ]* \([^ ]*\).*/\1/;/[0-9]/!d' | xargs sudo apt-get -y purge

Other then that, not removing kernels must be a fairly new thing. I have one Ubuntu 14 system after the other running into this issue of a full boot partition due to old kernel garbage.

It can make the life of less experienced users extremely complicated. It is actually a noob trap. Canonical should fix this.

Tags:

Kernel