99

I wonder why Ubuntu won't remove the old kernels automatically.

Surely nothing is perfect and things can go wrong, so in case a update doesn't work, having a backup kernel maybe great. But it would also suffice to just keep the latest kernel and the one before the latest and delete all the older ones.

Is there a reason why Ubuntu won't do this automatically?.

Volker Siegel
  • 13,065
  • 5
  • 49
  • 65
Registered User
  • 9,631
  • 14
  • 53
  • 85
  • See if below is good enough. I pulled some resources together and there is a lot of doubt on what should be considered to be deleted. – Rinzwind Feb 27 '15 at 15:03
  • Funny though: other Linux systems DO delete older kernels... – Rinzwind Feb 27 '15 at 15:08
  • 1
    @Rinzwind delete? Arch simply overwrites them - and then some things remain unusable until you restart (like iptables). – muru Feb 27 '15 at 15:29
  • 3
    "Why doesn't Ubuntu remove old kernels automatically?" it does. "I wonder why Ubuntu won't remove the old kernels manually." I have no idea what you mean with this. – Braiam Feb 27 '15 at 18:12
  • Fedora keeps a configurable number of kernels, and when installing a new one, removes the oldest, except the one currently running. – Davidmh Feb 28 '15 at 18:21
  • @Braiam Sorry, that was a typo. – Registered User Mar 01 '15 at 13:09
  • 1
    It seems extremely silly to me that I get the message 'boot out of disk space' every second month, and then have to fix this rather technical issue. I really understand why ubuntu is not a system for non-technical users. – sunew Oct 16 '16 at 17:32
  • 2
    @sunew yes, this issue completely killed my parents' machine running 12.04 for the full LTS period - filled up the disk's inode tables! There should be a sensible default here that technical users (who deliberately install different kernels) can overwrite easily. – artfulrobot Oct 26 '16 at 17:59
  • @EliahKagan: That question and its answers have a different intention and scope. – David Foerster Dec 10 '17 at 10:47
  • sudo apt install byobu && sudo purge-old-kernels see http://ubuntuhandbook.org/index.php/2016/05/remove-old-kernels-ubuntu-16-04/ – Akira Yamamoto Jun 24 '20 at 07:20

8 Answers8

44

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.

Rinzwind
  • 299,756
  • I agree that Ubuntu should not remove old kernels automatically. I once had a situation where newer kernels caused a serious problem with some software; until that problem was fixed, I had to retain the oldest kernel that worked. Naturally, I avoided apt-get autoremove. – Paddy Landau Mar 03 '15 at 13:40
  • 3
    it should at least be an option to be able to enable autoremove
    just my .02
    – mBardos Nov 11 '15 at 07:40
  • @mBardos there is (sort of): Ubuntu Tweak has a method for this. – Rinzwind Nov 11 '15 at 07:43
  • 3
    Doesn't "old" mean "not one of the last 2 (or three) kernels to be booted"? Unless you've got two different lines of kernels installed? Hmm I suppose you might be using an older version of a kernel for a different install on another partition. But there should be a default state that gets cancelled out if you are some kind of funk wizard. Or it should at least by default allocate enough space on /boot that you don't need to worry about this for years. Otherwise grannies are never gonna run linux. – intuited Nov 26 '15 at 17:35
  • @PaddyLandau: See @andrewdevans' answer. I just did autoremove and dpkg --get-selections | grep -v deinstall | grep linux shows three kernels installed. – idbrii Dec 02 '15 at 15:31
  • @idbrii, that's interesting, because the last time that I tried it, it did remove kernels other than the most recent two. I wonder what could differ between our systems? – Paddy Landau Dec 03 '15 at 11:36
  • @PaddyLandau I'm using the last LTS release. Maybe that implies more caution and by default configures different behavior? I had three kernels left installed, but they were not consecutive versions (something like 35, 51, 52). I assume those are the last three I booted, but I'm not sure. I don't use this machine daily, so I'm not updating that regularly. Maybe there's some quality measure that your kernels failed and mine did not due to my low usage? – idbrii Dec 03 '15 at 23:54
  • "no clean way to currently decide what the definition of old kernels is" Huh? What do you think apt autoremove does? Guess? "OLD" simply means the revision/version on kernel is lower than other ones. Autoremove removes older revisions of SAME MAJOR VERSION based on /etc/apt/apt.conf.d/01autoremove-kernels. There's never been a system script/bin to deal with kernel cleanup.Just APT. Consequently, you see 10,000 answers via google like this one - https://help.ubuntu.com/community/RemoveOldKernels#Safely_Removing_Old_Kernels Not the best way to manage it IMO. – B. Shea Feb 11 '19 at 16:19
  • @bshea a user manually executing it not automatic regardless of the word auto in autoremove. As stated after that part you quote: old and never booted kernel does not mean it is an unwanted kernel – Rinzwind Feb 11 '19 at 18:00
  • I never said anything about "automatic".<< Old means old. What would you call it then? - I said nothing about it meaning anything else but old. apt autoremove removes OLD KERNELS. You seem to want to split hairs on wording.

    – B. Shea Feb 12 '19 at 00:30
  • @bshea no. It HAS to be split hairs. You do not want to remove old kernels. You want to remove unwanted kernels. And that is a case by case decision. Not an automated as the question was about: why does Ubuntu not remove old kernels automatically. – Rinzwind Feb 12 '19 at 07:19
22

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.

user535733
  • 62,253
20

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.

psnizek
  • 201
  • 3
    This script is potentially dangerous: when I do a system update which installs a new kernel, then run this script before rebooting, the new kernel will be removed instead of the old one. I don't want to say it's a bad script, just that one has to be careful when to call it. – daniel kullmann Jul 30 '15 at 07:05
  • Thanks! "0 upgraded, 0 newly installed, 48 to remove and 5 not upgraded. After this operation, 3,322 MB disk space will be freed." – thenickdude Oct 22 '15 at 11:43
  • 7
    It is wise to run this script without | xargs ... part to check what packages are about to be removed, before actually removing them: sudo dpkg -l 'linux-*' | sed '/^ii/!d;/'"$(uname -r | sed "s/\(.*\)-\([^0-9]\+\)/\1/")"'/d;s/^[^ ]* [^ ]* \([^ ]*\).*/\1/;/[0-9]/!d' – PeterM Nov 26 '15 at 13:43
  • 2
    @PeterM, and sudo is not needed in front of dpkg. – jarno Dec 23 '15 at 16:36
  • There is a separate question about the script here. – jarno Dec 23 '15 at 16:40
  • This script intends to remove my latest updated kernel. Can someone please verify that. – We are Borg Feb 23 '16 at 15:55
  • @WeareBorg Sure, it is designed to remove every kernel except the currently booted one. – jarno Jun 05 '17 at 14:56
3

The simple-minded way to remove old kernels is to use ubuntu-tweak. You select the Janitor tag and tick the Old Kernel box. That gives you a list of kernels it thinks you don't need any more. You can select them all and click Clean and, after a while, the job's done. Being of a nervous disposition I checked which kernel I was running using uname -a but it didn't figure on the list. This worked on my wife's laptop (which has a rather small boot partition and was showing warnings) and mine (which was working fine). Does everyone agree that this is safe?

  • Oh removing kernels automatically can be done safely; it is just that the person in charge of making this happen does not feel confident enough to claim he can decide for everyone a kernel of a specific system should be deleted. Old does not mean unwanted ;) – Rinzwind Feb 28 '15 at 13:15
  • As @Rinzwind points out, there are really two issues here. How and when/if to do it. I used to use ubuntu-tweak and it worked, but didn't like KDE (which I use) very much. Now, whenever I get a kernel update, I go into the muon gui package manager, search for kernel, find the one to delete, add it's last number like 36 to kernel - kernel 36, sort on installed, and select the 2 or 3 packages to be removed. It's easy. It's also manual, so if I have compiled any driver modules that still need an older kernel, that kernel won't get deleted until I'm ready for that to happen. – Joe Mar 06 '15 at 11:58
2

Ubuntu only auto-removes items that are no longer needed or are a security risk...So I assume that the reason for this is security purposes. Lets say for some odd reason a new kernel becomes insecure...Then you would be rerouted back to the old kernel temporarily while the new one is being fixed. Also new kernels can come with changes so therefore some kernels are opt-in for example updating 14.04.1 to 14.04.2 you can opt-in for a new kernel, but this April when the new LTS version 15.04 comes out you will automatically have you kernels updates. So I reckon that its for security purposes.

but if you want to remove it try running this command line

sudo apt-get autoremove

This should remove any unneeded software such as your old kernel if not try

sudo apt-get remove (kernels-name)
Tactux
  • 166
  • 1
    "Lets say for some odd reason a new kernel becomes insecure...Then you would be rerouted back to the old kernel temporarily while the new one is being fixed." Do you have a source for this claim? – John Kugelman Feb 27 '15 at 22:51
  • 3
    @JohnKugelman it wouldn't happen. A new package would be released with the fix and people would upgrade to it. In case of extreme unfixable problem, an older kernel would be packaged with higher version number (epoch change perhaps) to effect the upgrade. – hobbs Mar 01 '15 at 05:29
  • "...when the new LTS version 15.04 comes out..." - 15.04 will not be LTS, unless there has been a major support shift by Canonical which I haven't heard of. – iGadget Apr 14 '15 at 14:24
  • actually 15.04 is going to be the new LTS but the release as is the same for all releases will not become the new standard LTS until a specific date after the new system has been released...i believe Canonical does this because they want to be absolutely sure the system is completely secure...as i am typing this 15.04 has already been released so give it a few months and it'll be an LTS...i don't plan on upgrading till then...not much changed anyway – Tactux Apr 28 '15 at 21:01
  • @TAC_Tux That's not how Ubuntu does LTS releases. You're possibly thinking of the way the Debian project does things. Here's the current Ubuntu release schedule: https://wiki.ubuntu.com/LTS – Martijn Heemels Jul 30 '15 at 13:32
  • @Martijn Heemels lol yeah i found that out this tread is very old and at the time i was poorly informed upon the release system Canonical and Ubuntu run next LTS will be 16.04 next year – Tactux Sep 10 '15 at 19:36
2

I recommand use Ubuntu Tweak to remove old kernels. look here:

How do I remove old kernel versions to clean up the boot menu?

skonsoft
  • 159
  • 11
2

I think the latest release of Ubuntu 15.10 could remove old kernels automatically, if you edit /etc/apt/apt.conf.d/50unattended-upgrades file: Change line

//Unattended-Upgrade::Remove-Unused-Dependencies "false";

to

Unattended-Upgrade::Remove-Unused-Dependencies "true";

I haven't tested this, though.

jarno
  • 5,600
2

I had a problem with being unable to install software. Apt-get was unable to install linux-headers, reporting the disk was full.$ df -i reported that IUse% was at 100%: The disk had free space but had run out of inodes. $ sudo dpkg --configure -a failed, & so did $ sudo apt-get -f install and $ sudo apt-get autoremove

The problem solved when I manually removed several of the older kernel folders from /usr/src - just $ rm or shift-delete the folders. This freed up enough inode space to allow $ apt-get -f install to complete.

After that I had only 10 linux kernels. I ran $ sudo apt-get autoremove which deleted 1.4 GB of files. Which begs the question: Really?? Really?? But honestly, is the this the right way to run a user friendly system?