Take the 2-minute tour ×
Ask Ubuntu is a question and answer site for Ubuntu users and developers. It's 100% free.

Every time I install a new Linux kernel, it gets left in the grub_config, making the boot menu longer each time.

I know I can manually search through the installed packages and remove them.

Does Ubuntu provide any easier way to clean them up or keep them from showing in the boot list?

share|improve this question

37 Answers 37

Based on a previous answer by David Kemp, the following script will purge all headers and images except for the last 2 versions.

#!/bin/sh
# This script assumes that the installed linux-image and linux-headers packages
# share the same versions (i.e. if a linux-image version number is installed,
# the corresponding linux-headers package will also be installed, and vice
# versa.)

SECONDTOLASTVER=$(dpkg --list | grep linux-image | awk '{ print $2 }' | sort -r -n | sed '/^[^0-9]\+$/d' | sed 's/^.*-\([0-9\.]\+-[0-9]\+\).*/\1/' | uniq | sed -n 2p)

# get a list of package names matching the argument passed to the function, and
# return only those package names which should be removed
get_pkgs_to_remove_matching () {
    if [ -n "$SECONDTOLASTVER" ]; then
        echo $(dpkg --list | grep $1 | awk '{ print $2 }' | sort | sed -n '/'"$SECONDTOLASTVER"'/q;p')
    fi
}

echo $(get_pkgs_to_remove_matching linux-image) $(get_pkgs_to_remove_matching linux-headers) | xargs sudo apt-get -y purge
share|improve this answer

I don't want to purge newer kernel than current so I use this :

dpkg -l linux-* | awk -v cur=$(uname -r) '
  /^ii/ && /linux-image(.*)-[0-9\.\-]+/
  {
    match(cur, /([0-9]+)\.([0-9]+)\.([0-9]+)-([0-9]+)/, v);
    match($2, /([0-9]+)\.([0-9]+)\.([0-9]+)-([0-9]+)/, c);
    if ((c[1] < v[1]) || (c[1]<=v[1] && c[2]<v[2]) ||
      (c[1]<=v[1] && c[2]<=v[2] && c[3]<v[3]) ||
      (c[1]<=v[1] && c[2]<=v[2] && c[3]<=v[3] && c[4]<v[4])
    ) {
      print $2;
    }
  }
  ' | xargs sudo apt-get -y purge
share|improve this answer

This is by far the best answer in my opinion:

http://tuxtweaks.com/2010/10/remove-old-kernels-in-ubuntu-with-one-command/

Follows the last command on the site above somewhat tweaked:

sudo apt-get purge $(dpkg -l 'linux-*' | awk '/^ii/{print $2}' | grep -v -e `uname -r | cut -f1,2 -d"-"` | grep -E -e -[0-9]+(\.[0-9]+){2}-[^-]+)
share|improve this answer

Here's the script I use to keep 2 kernel packages installed (i.e. the current one and the previous one). It also removes the initrd-VERSION-generic.old-dkms files left around by DKMS, which can fill-up a small boot partition.

The script:

echo "**Removing -generic.old-dkms files from /boot**" && rm -f /boot/*-generic.old-dkms # if using DKMS it creates initrd-VERSION-generic.old-dkms in /boot and doesn't clean them up, meaning after a few kernel upgrades /boot can become full
OLDKERNEL=$(ls -tr /boot/vmlinuz-* | head -n -2 | cut -d- -f2- | awk '{print "linux-image-" $0}')
OLDHEADERS=$(ls -tr /boot/vmlinuz-* | head -n -2 | cut -d- -f2- | sed 's/-generic//g' | awk '{print "linux-headers-" $0}')
OLDHEADERS=${OLDHEADERS//-pae/} # remove -pae string as linux-header packages don't have it in their names
if [ -n "$OLDKERNEL" -o -n "$OLDHEADERS" ]; then
apt-get -q remove --purge $OLDKERNEL $OLDHEADERS
echo "**Finished removing old kernels**"
else
echo "**No old kernels found**"
fi
share|improve this answer
  1. Open the terminal.
  2. Type the command sudo su.
  3. Open file directory using xdg-open /boot/ in the bash shell.
  4. Then remove all the kernel files that you don't require.
  5. Give the command update-grub.
share|improve this answer
1  
-1 this does not add anything to askubuntu.com/a/155356/158442 (which was downvoted to -3, if you note), and is just as dangerous. –  muru Aug 31 '14 at 9:19

Just write the below in terminal

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
share|improve this answer

in addition to the above answers you could try to remove the kernel image + system.map + config files manually. grub looks for those files in the /boot directory.

remove any files not related to the kernel version(s) you use. if you are using only the latest version, let's say 3.2.0-25, it should be safe to remove all 2.6.* and 3.0.* versions. so you type ls -alF /boot/2.6. to see what's there and remove those. same with all the other versions except the one(s) you are using.

NB: when updating the kernel it may be good policy to keep the version that previously worked best for you

share|improve this answer
2  
This seems likely to break things, particularly when 3rd party modules have been installed. –  leonardo Apr 1 '13 at 15:24

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.