30 Comments

  1. Menno

    It is 2021 and I still visit this page for removing the 5.8 series kernels 😉

    Reply
  2. Denis

    Thankxx it just worked perfectly, save me a lot of work .......respect

    Reply
  3. pave

    Thanks 🙂 Helped me a lot.

    Reply
  4. Kiko

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

    8^)

    Reply
  5. the.helped

    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
    
    Reply
    • EdMeade

      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.

      Reply
    • zen

      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

      Reply
    • OG

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

      Reply
  6. Tim Miller Dyck

    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

    Reply
  7. d4

    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

    Reply

  8. 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

    Reply
  9. Hans Lambermont

    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

    Reply
  10. Dawie Joubert

    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
    Reply
    • Linerd

      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).

      Reply
  11. Thahir

    great....

    Reply
  12. Jonathan

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

    Reply

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

    Thanks for sharing Dude

    Thanks for great post, keep it up

    Reply
  14. Barb

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

    Cheers,
    Barb

    Reply

  15. Wow !! it works like charm !!

    Reply
  16. Mez

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

    Reply
  17. Zach

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

    Reply

  18. I use ubuntu-tweak for doing it for me... and lot more 🙂

    _ATOzTOA
    http://www.atoztoa.com

    Reply
  19. Zach

    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
    Reply
    • Linerd

      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.

      Reply
  20. Zach

    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.

    Reply
    • Linerd

      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

      Reply

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.