36 Answers
36

11.10 and newer versions of Ubuntu

GRUB2 and its display of all kernels

The latest versions of Grub2 installed in Ubuntu automatically display the latest kernel and hides the older kernels that you may have installed.

If you do not see your grub - then remember to press Shift whilst booting.

As you can see, only the latest kernel is displayed.

If you select the option shown (press Enter) then all the old kernels become visible and available to boot from.

How to permanently delete older kernels

First boot with the latest available kernel.

There are a number of ways to delete old kernels. Personally, I wouldn't touch Computer Janitor since this is acknowledged to break your computer with its suggestions.

synaptic

An alternative is Synaptic (sudo apt-get install synaptic)

search for linux-image, right-click a kernel and choose complete removal and finally click the Apply button to delete the kernel.

Repeat the search but this time for linux-header - you can delete the associated headers for the kernel image chosen previously.

Synaptic though will not attempt to verify what you are trying to remove... you could inadvertently delete your newest kernel - or even delete all of your kernels via this tool leaving you with an unbootable Ubuntu!.

Remember to check which kernel you are using type:

uname -r

The result would be similar to:

Remember the result and the number - make sure you don't delete the corresponding image or header.

Ubuntu-tweak

IMHO, the best GUI tool is Ubuntu-Tweak

It is not available from the standard repositories. To install in 13.10 and older versions you need to use the author's PPA:

In Ubuntu 14.04+ you have to download .deb file from official website and install it using Software Center or manually.

Choose the options shown by the arrows.

Select both the headers and image with the same version number.

It will not allow you to delete the current kernel you are booted with since the current kernel is not displayed.

Recommendation

My recommendation is to keep at least two or preferably three kernels including the latest. The reason for the recommendation is that you will have at least one/two other kernels to boot with, if for what-ever reason the latest kernel you are unable to boot with or introducing a regressed capability such as broken wireless.

Removing old "linux-image*" packages using synaptic, worked well with 10.04 too. (I mention it because the title suggests it may only be for 11.10 and up)
–
mivkMay 17 '12 at 16:35

4

the -y switch at the end of the apt-get line is necessary, otherwise apt-get just asks for confirmation and since the input is a pipe, it just aborts. So it should end in: | xargs sudo apt-get purge -y
–
JoshAug 26 '13 at 16:30

1

if you recommend to keep at least two kernels, you could have written your script to do exactly that ;) (I was just looking for a script that does that, but it seems that I’ll have to write it myself)
–
törzsmókusNov 8 '13 at 12:56

13

Removing about 20 versions of old linux-image and linux-headers freed 4.5GB of space on my machine.
–
Andrew MaoJan 7 '14 at 19:30

6

This method works well, but deletion of every kernel excessively regenerates grub.cfg, which takes time. Is there any way of disabling this for batch deletion, then regenerating it once?
–
spacediverMar 25 '14 at 23:47

No need to reboot the system afterward. In fact, you should reboot the system before performing these steps, to ensure you are using the latest kernel version that you probably just downloaded & installed (you did an apt-get update && apt-get upgrade before this, right??)
–
RicketFeb 28 '14 at 2:27

1

You may also want to uninstall the linux-signed-image packages that may be installed in UEFI systems: sudo apt-get purge linux-signed-image-3.11.0-{12,14,24}-generic which would remove 12, 14 and 24
–
deviusJul 1 '14 at 10:53

1

@jarno I don't know if I'm using a signed kernel or not, but uname -r doesn't have any signed string on my system. I do seem to have a signed image installed and the output of that ls command is: /boot/vmlinuz-3.19.0-16-generic.efi.signed.
–
deviusMay 13 at 14:38

`uname -r` outputs the current installed kernel release - we include it in backticks so that the output is includes as part of the command (you might also see this as $(uname -r)

/something/q says stop when you match 'something' (in this case, something is output of uname -r) - the / surround a regular expression

p is print

the ; is the command separtor, so /something/q;p says quit when you match something, else print

altogether, sed -n '/'`uname -r`'/q;p' is print the lines until it matches with the current kernel name.

If you're paranoid (like me), you can make the last part xargs echo sudo apt-get -y purge so that the command to purge the old kernels is printed, then you can check that nothing unexpected is included before you run it.

@DmitryPaskal As always, don't just copy-paste these without understanding them. On my machine this one-liner also matches linux-libc-dev:amd64 which shouldn't be removed.
–
jamesadneyMar 12 '13 at 20:19

1

@Kees Cook, shouldn't the awk argument only be surrounded by single-quotes? If your intent is to prepend "linux-image-" to the piped lines, this only seems to work if you use awk '{print "linux-image-" $0}'.
–
MarkJun 12 '13 at 17:33

Note:
After kernel updates a new entry is added to the GRUB menu.You can remove the older one if you want.However, most experienced users will advise you to keep at least one spare entry in case something goes wrong with an upgrade and you need to boot an older kernel version for troubleshooting purposes.

You can find it under System>>Administration>>
You see in the second screenshot you can select how many kernels to show? I generally just keep it on 1, but when I get a kernel upgrade I always change it to 2 before restarting so I can select the older kernel if the new kernel has problems with my hardware. Once I know the new kernel is working well I change it back to 1.

Actually, startupmanager dint give me a window like this on Ubuntu 10.04, instead it just gave a window with two tabs -> Boot options and Advanced.. and in advanced it dint have the option to limit the number of kernels. So please update the answer for Ubuntu 10.04.(And thats why i down-voted this..)
–
SenDec 13 '10 at 5:12

Computer Janitor should not be used - it is buggy and has now been dropped as a default application in natty/oneiric/precise.
–
fossfreedom♦Feb 13 '12 at 16:04

2

Rather, install "Ubuntu Tweak" as described in another answer (its own ppa), which has its own "computer janitor" (not to be confused with the "computer janitor" that temporarily was available in older ubuntu versions)
–
michael_nFeb 4 '13 at 2:10

I did this but it was still collecting 3+ old kernels consuming 100's of MBs. So I added apt-get autoremove to my daily cron job that actually does the upgrades, since it doesn't always go through built-in update-manager.
–
MarcosAug 24 '12 at 12:01

From Ubuntu 11.04 the grub menu only shows the current kernel by default, older kernels are hidden in the 'Previous kernels' menu. This method will still work for cleaning out the older ones.
–
AndyMay 16 '11 at 16:03

You can uninstall the old kernels (linux-image-... packages) using Synaptic, and that will remove them from the boot menu. Take care not to remove the running kernel (you can check its version with uname -r).

Bear in mind that having a one or two older versions can help you troubleshoot, should something go wrong.

Alternatively, you can edit/remove the entries manually (gksu gedit /boot/grub/grub.cfg), but they will be re-generated when you update to a newer kernel.
If you are thinking about removing recovery mode options - don't. They can come in handy if you break something which prevents you from booting.

The fastest/simpler way (Applicable at least since 12.04) possible that already comes with Ubuntu is apt-get. Do the following if you wish to remove all older kernel versions that are not in use (Except the previous one that you are no using. This is to make sure that if the current kernel version fails in some way, you have a way to go back to a previous state). Do the following:

sudo apt-get autoclean

This will eliminate any old files (Including kernel versions) you may have. Note that if you have many old versions, it will take a while since it has to make sure that removing the kernel version has no issues. For me, removing the last 12 kernel versions took about 2 minutes. You can also do the following:

sudo apt-get clean

Which will eliminate everything downloaded and stored in the cache folder of apt. Lastly you have:

sudo apt-get autoremove

which would check for any unused packages and remove them if necessary. This is great for those libraries and dependency packages that are no longer needed byt any app installed.

There is a command called purge-old-kernels that is in bikeshed Ubuntu package (available in universe repository) that can be used to purge extra kernels. It will keep 2 latest kernels and the current one by default. The package has a few dependencies, though.

The following Bash script aims to let user purge some kernels that are older than the currently used one. You can set by an option, how many older kernels it keeps. This has the advantage that it will not purge any newer kernels that may exist e.g when system has not been booted after installing some new kernel(s).

Save the script as purge-older-kernels, make it executable by running chmod a+x ./purge-older-kernels in the directory. Then you can see its usage by running ./purge-older-kernels -?. If you want to use the script like a command, copy the script in "/usr/local/bin". Actual removing requires superuser privileges, i.e. use of sudo.

If you have any version that is newer than the current one this will give you a warning to restart you computer first. Also note that the older kernels are preserved due to a good reason which is if you somehow mess up your current kernel making your system unstable then you should be able to boot into any older kernel.

Next time, when removing old kernels open a Terminal and use this command:
sudo apt-get autoremove linux-headers-2.6.38-10-generic

You can use Synaptic to get the exact name of the kernel that you intend to delete. Just open Synaptic and search for "linux-headers" and then select which kernel entry you want to remove. The relevant entry will be tagged with "-generic" at the end.

To clear out any unused (left over) dependencies throughout the system use this command by itself:
sudo apt-get autoremove

makes it executable, then finally we update grub which will change the grub.cfg file:

sudo update-grub

Now, BEWARE, if you update your kernel or OS, your boot menu probably will not update... you'll have to do that manually. But doing this procedure will let you customize the boot menu a bit more, such as remove the kernel version and just put the ubuntu name... i.e. Ubuntu Lucid 10.04, etc...

Hope someone finds this helpful, as it took me a while to figure out... didn't see this solution anywhere...

This answer seems like overkill. It's better to just remove old kernels.
–
Scott SeveranceJan 8 '12 at 23:32

1

I keep the immediate previous kernel around until I've verified the the most current kernel works properly. If it works, it isn't going to stop working. So, after I've verified the most recent kernel (after a week or so), I have no reason to keep the old kernel around. If it doesn't work, then I have the latest known-good kernel to fall back to. Actually, I'm currently running an old kernel due to a showstopper bug in the current kernels. But that doesn't change my overall policy. You only need one known good kernel.
–
Scott SeveranceJan 10 '12 at 5:14

Install the synaptic package manager and go down to the filters tab (I think filters, if not try all 5) and select "local". This will show you orphaned packages on your system, such as the kernels. After you uninstall them, run update-grub. That command updates the list of boot options for grub.

If this fails, you can always try apt-get remove linux-image-version-generic.

I use this to keep desktop's boot volumes relatively clean, but in a server situation you'd probably want to expand the logic and write some additional scripting to maintain a list of the last X kernels the server has booted.

I'm using a KDE desktop, and the easiest option I found was using the kde-config-grub2 application as suggested here: https://www.kubuntuforums.net/showthread.php?58075-remove-old-linux-versions (which I already had installed for setting background image, default boot option, and the like). Next to the drop-down box where you can choose the default entry, there is a "Remove Old Entries" button. Clicking this button presents you with a list of all installed kernels and you can select which ones to remove. When you apply the changes it will use dpkg to actually remove them from the system as well as the GRUB menu.

I disabled the 10_linux boot file and put custom entries in 40_custom (copied/pasted from boot.cfg). That way you can edit the text of the entry carefully. Although updating your kernel may not update your 40_custom file, so you may have to beware of that. Although it's easy to fix.

Thanks for the edit. Ubuntu has used GRUB2 since 9.10, so it's unlikely any solution that would work for any version since then would fail for any other version. (Thus, if this works for 10.04, it probably works for later versions too.) Unfortunately, because it's GRUB2, this will probably not work at all, or at least not for very long. The file you're editing, /etc/grub/grub.cfg is automatically recreated when update-grub runs--and it is run every time there is a kernel update (and for some other updates).
–
Eliah KaganJan 30 '13 at 13:10