Remove all unused kernels with 1 command in debian based systems

As you know, everytime there is an update for the kernel, the new kernel is installed while the old one is not automaticaly uninstalled, after several kernel updates you will find a large list of old kernels in the start terminalgrub menu . So if we got no problem with the new installed kernel, we can just delete the old ones.



In my previous post i did show you how to uninstall older kernel in Ubuntu. Today I will show you another command to remove unused kernels:

sudo apt-get remove $(dpkg -l|egrep '^ii  linux-(im|he)'|awk '{print $2}'|grep -v `uname -r`)


Is done.

{module post forum}

  • Learning

    A short explanation for every command would be perfect.

  • bash

    pay attention with your ati drivers, fglrx and many other related programs like gearth will be removed!


    Or if you are an Ubuntu user, you can use Ubuntu Tweak if you want to use a simple to use GUI.

  • Foo

    You need to cut off the text after the revision in Ubuntu.

    $(dpkg -l|egrep ‘^ii  linux-(im|he)’|awk ‘{print $2}’|grep -v `uname -r | cut -d- -f1,2`)

  • Alex

    Worked like a charm without cutting of anything.

  • Mark Preston

    Could you explain to me how to run that command while in LiveUSB session? I have so many old kernels that the OS will not boot. You can see more of about that, if you wish at: – I ran Boot-Repair-Disk and it says that / is too full (even though I know it has a little over a 1 gig (in 15 gig) free.

  • Stephen Harker

    This will actually pull out the linux-headers package for the live kernel as well which you don’t really want. ie. on my system it removes “linux-headers-3.5.0-22” and “linux-headers-3.5.0-22-generic” but leaves “linux-image-3.5.0-22-generic” which is the live kernel.

    It also removes the meta package “linux-image-generic” which you want to keep if you want to get the kernel upgrades in the first place :)

  • Bernie Haverkamp

    This is still removing the meta package for the current kernel.

  • Ludovic Urbain

    This is a bad idea.
    There’s no valid reason to do blind upgrades or blind upgrade cleanup.
    If you want to help people with that, you should document or write a way to do an update with cleanup of prior header / image.

  • Pingback: Remove all unused kernels in debian based systems | Webové stránky Jana Faixe()

  • Pingback: Debian – Supprimer les anciens kernels inutilisés | JBNet 3.0()