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. Kiko says:

    Thank to both authors.... I like a clean system... and a clean desk, must be something wrong with me... LOL

    8^)

  2. the.helped says:

    Thank you for this article. I wrote the following script and maybe some others find it useful.

    
    #!/bin/bash
    echo 'You are currently running: '`uname -r`
    echo 'The following kernel images exist in /boot:'
    ls /boot|grep vmlinuz|cut -d'-' -f2,3
    nKerns=`ls /boot|grep vmlinuz|wc -l`
    if [ $nKerns -gt 1 ]
    then
      # Ok to remove the oldest ONE
      oKern=`ls /boot|grep vmlinuz|cut -d'-' -f2,3|head -1`
      echo "The oldest kernel: $oKern will be removed."
      rempkgs=`dpkg -l|grep ^ii|grep $oKern|awk -F' ' '{print $2}'`
      echo 'You will now require the SUDO (root) password to complete:'
      sudo aptitude remove $rempkgs
      echo 'Kernel cleanup completed.'
    else
      # Prevent the user from self-nuking!
      echo '*** WARNING ***'
      echo 'This is the ONLY kernel and you must keep it!'
      echo 'Without a kernel you have no Linux system!'
    fi
    
    • EdMeade says:

      the.helped,

      Thanks. I did find it helpful. I understood what was going on and liked the fact that I could remove one kernel at a time -- just to give me a warm fuzzy.

    • zen says:

      I've slightly modified your script to get a prompt before running the cleanup.

      Here is the code:

      #!/bin/bash
      echo 'You are currently running: '`uname -r`
      echo 'The following kernel images exist in /boot:'
      ls /boot|grep vmlinuz|cut -d'-' -f2,3
      nKerns=`ls /boot|grep vmlinuz|wc -l`
      if [ $nKerns -gt 1 ]
      then
      # Ok to remove the oldest ONE
      oKern=`ls /boot|grep vmlinuz|cut -d'-' -f2,3|head -1`
      echo "The oldest kernel: $oKern will be removed."
      echo "Do you want to continue?"
      select yn in "Yes" "No"; do
        case $yn in
          Yes ) echo "Proceed";break;;
          No ) exit;;
        esac
      done
      rempkgs=`dpkg -l|grep ^ii|grep $oKern|awk -F' ' '{print $2}'`
      echo 'You will now require the SUDO (root) password to complete:'
      sudo aptitude remove $rempkgs
      echo 'Kernel cleanup completed.'
      else
      # Prevent the user from self-nuking!
      echo '*** WARNING ***'
      echo 'This is the ONLY kernel and you must keep it!'
      echo 'Without a kernel you have no Linux system!'
      fi
      

      Hope it will helps!

      Bye

    • OG says:

      Thanks for this script, this does recover much wasted disk space.

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>