Remove Old Kernels in Ubuntu

December 5, 2009 by
Filed under: HowTo, linux, Ubuntu 

Edit: Oct. 2, 2010 - I've written a new post that shows a bash one-liner command that will remove the old kernels from your system in one step. You can check it out here.

If you've been using Ubuntu Linux for a while, then you probably have a number of Linux kernel updates that have been installed on your system. (I've got a total of 3 kernels in my Karmic system already.) In all likelihood, these updates get installed and you boot into the latest kernel, never to use the older kernels again. But these old kernels are still hanging around on your system, cluttering up your grub boot screen and taking up space on your hard drive.  

There have been attempts made to create an automatic tool to clean these up for you along with much discussion on the Ubuntu Forums, but so far there's nothing officially in the repositories.

In the past, I've gone into Synaptic and searched for the older kernels and related packages and manually marked them for removal. This method works fine, but the search process is a little slow in Synaptic and you have to run a few searches to catch all of the packages to remove. I decided to come up with a way to do this from the terminal and hopefully save some time. The commands I'm going to use can be a bit daunting, so just copy and paste.

Affiliate Link

Warning

Don't follow this process unless you're sure you don't need to boot into the older kernels. If you're not sure, just leave things alone. Also, it is possible to remove all of the kernels from your system and make it unbootable. I suggest leaving the latest kernel and one version previous to that. You can find out the kernel version that you're currently running with

uname -r

Find and remove old kernels

The first step is to figure out what kernels are installed. The following command will do the job.

ls /boot | grep vmlinuz | cut -d'-' -f2,3

Your result should look something like this.

2.6.28-15
2.6.28-16
2.6.28-17

This is the list of kernels installed on your system. Now you want to find out which packages are installed relative to the kernel you want to remove. For my example I'm going to remove the oldest one, 2.6.28-15.

dpkg -l | grep ^ii | grep 2.6.28-15 | awk -F' ' '{ print $2 }'

On my system, (Jaunty) the resulting list is:

linux-headers-2.6.28-15
linux-headers-2.6.28-15-generic
linux-image-2.6.28-15-generic
linux-restricted-modules-2.6.28-15-generic

Now that we know what packages to remove we can remove them with dpkg, apt-get or aptitude.

sudo aptitude remove linux-headers-2.6.28-15 linux-headers-2.6.28-15-generic linux-image-2.6.28-15-generic linux-restricted-modules-2.6.28-15-generic

That's it. You've removed an old kernel and related packages. Now you can keep your system as clean as it is right after you install Ubuntu. The commands are a bit complex, so maybe I'll write up a bash script when I have some time. Proceed with caution!

Comments

28 Responses to “Remove Old Kernels in Ubuntu”

  1. Tim Miller Dyck says:

    Hi d4, thanks for that idea.

    When I tried it, aptitude was prompting for a confirmation and then quitting when it did not get the confirmation, so I needed to add "--assume-yes".

    dpkg -l | grep ^ii | grep "linux-image-[0-9]" | awk -F' ' '{ print $2 }' | grep -v `uname -r` | sed '$d' | sed '$d' | sed 's/linux-image//' | xargs -i echo "linux-image{} linux-headers{}" | xargs sudo aptitude --assume-yes purge

    Regards,
    Tim Miller Dyck

  2. d4 says:

    And here is on small script to only keep the currently running kernel and the two most recent ones:

    dpkg -l | grep ^ii | grep "linux-image-[0-9]" | awk -F' ' '{ print $2 }' | grep -v `uname -r` | sed '$d' | sed '$d' | sed 's/linux-image//' | xargs -i echo "linux-image{} linux-headers{}" | xargs sudo aptitude purge

  3. [...] i want to remove the oldest. For the past few rounds of cleanups, I have used the method found in Tux Tweaks . Just need to copy and paste the commands from that [...]

  4. techjacker says:

    brilliant guide thanks very much!

    if anyone gets an error about grub when they remove an image eg:
    The link /vmlinuz.old is a damaged link
    Removing symbolic link vmlinuz.old
    you may need to re-run your boot loader[grub]

    then make sure you run this command before you restart!
    sudo update-grub

  5. Hans Lambermont says:

    This one-liner also removes the headers of the current running kernel:
    linux-headers-2.6.32-24 for uname -r = 2.6.32-24-generic-pae

    Adding 's/-[^0-9].*//' to the line fixes this :

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

  6. Dawie Joubert says:

    I found this script on the net some time ago, works wonders:

    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
    • Linerd says:

      Yep, that's the same one that Zach posted above. It removes all but the most recent kernel (I think. I'm not sure if avoids removing the currently running kernel, or if it avoids removing the latest).

  7. Thahir says:

    great....

  8. Jonathan says:

    Awesome, Zach! Thanks! Keep showin' us how it's done! :D

  9. Rohit says:

    wowwww.its really awesome and seems like magic....!

    Thanks for sharing Dude

    Thanks for great post, keep it up

  10. Barb says:

    Thanks Zach! Worked like magic and no pain and suffering on the user end!

    Cheers,
    Barb

  11. Abhishek says:

    Wow !! it works like charm !!

  12. Mez says:

    Or far more user-friendly, you can just search for "linux" into synaptic and uninstall the older kernels and related packages...

  13. Zach says:

    @linerd I believe so, unless of course, you use aptitude and it just does that automatically.

  14. [...] been installed on your system. (I’ve got a total of 3 kernels in my Karmic system already.) More here In all likelihood, these updates get installed and you boot into the latest kernel, never to use [...]

  15. ATOzTOA says:

    I use ubuntu-tweak for doing it for me... and lot more :)

    _ATOzTOA
    http://www.atoztoa.com

  16. Zach says:

    I had previously found this in a bug report on launchpad. Does about the same thing.

    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
    • Linerd says:

      Zach - Wow, that's one monster of a command! That one is going to take a while to digest. It looks like the uname -r | sed part of the command filters out the current kernel and then only the older kernels are fed into the apt-get purge command.

      I might suggest removing the -y from the final portion of the command to run apt-get interactively.

  17. Zach says:

    Oh and also, you can just comment out the relevant lines in /etc/apt/apt.conf.d/01autoremove to have your package manager do this for you. I'd consider this this the preferred method.

    • Linerd says:

      Zach - Another nice tip. If I comment the lines
      "^linux-image.*";
      "^linux-restricted-modules.*";
      "^linux-ubuntu-modules-.*";
      would the old kernels be removed automatically, or would I have to run
      sudo apt-get autoremove

  18. Story added...

    This story has been submitted to fsdaily.com! If you think this story should be read by the free software community, come vote it up and discuss it here:

    http://www.fsdaily.com/HighEnd/Remove_Old_Kernels_in_Ubuntu

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>