Ask Ubuntu is a question and answer site for Ubuntu users and developers. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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
    
As well as removing the old kernels, uncomment this line in /etc/default/grub if you want to get rid of the 'recovery' items in the menu: #GRUB_DISABLE_LINUX_RECOVERY="true" – poolie Nov 29 '10 at 6:04
    
In recent releases Ubuntu hides kernels in Grub's sub menu, so it is not that obtrusive. However, it may be good idea to remove extra kernels to save space, especially, if you have a separate small /boot partition. – jarno Apr 17 '15 at 21:44
    
There is an ubuntu command called purge-old-kernels to do the job. See my answer for more information. – jarno May 7 '15 at 11:43
2  
sudo apt-get autoremove should do the trick on Ubuntu 14.04+ – hobs Dec 1 '15 at 17:17
    
The accumulation of old kernels is a bug, with fix in progress: bugs.launchpad.net/bugs/1357093 . When the fix is released, older kernels will autoremove by default. – user535733 Dec 31 '15 at 17:52

34 Answers 34

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
    
It would purge linux-libc-dev:amd64 on my machine. – jarno Jan 2 '15 at 13:23

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
2  
-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

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. – user108430 Apr 1 '13 at 15:24

protected by Community Aug 2 '15 at 6:05

Thank you for your interest in this question. Because it has attracted low-quality or spam answers that had to be removed, posting an answer now requires 10 reputation on this site (the association bonus does not count).

Would you like to answer one of these unanswered questions instead?

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