[HOW TO] Remove old kernels in Ubuntu

After running the system for a while the Linux kernel gets updated and the old versions are kept installed, even though that aren't needed any more.

Remove old kernels in Ubuntu

In this article I provide a way to remove automatically any kernel version that is older than the current being used.

By the way, I was able to recover more than 1GB of free disk space by running this.

Remove

Open a terminal window and run:

current=`uname -r` && uninstall="" && for version in `dpkg -l linux-image* | grep ii | awk '{ print $2}'`; do if [[ "$version" < "linux-image-$current" ]]; then uninstall=$uninstall" $version"; fi; done && sudo apt-get purge $uninstall -y && sudo update-grub2

References

Remove Old Kernels in Ubuntu 11.10 (Oneiric ocelot)

Comments

  1. Thank you very much for that - worked in Debian Wheezy too

    ReplyDelete
    Replies
    1. You're welcome. I'm glad this worked for you too.

      Delete
  2. I always used this command
    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
    Which one do you think is better or is the same?

    ReplyDelete
    Replies
    1. As long as it gets the job done, it doesn't really matter. My version is more "programming like" and it's easier for me to understand (of course I wrote it :p)

      Your command is lacking the update-grub2, though, which updates the boot menu.

      Delete
  3. I'm using apt-get --purge remove linux-kernel-xxx linux-headers-xxx

    ReplyDelete
    Replies
    1. You have to manually replace xxx with some version. Mine does that automatically.

      Delete
  4. Worked great, thanks! Over 1 gig of hard disk space freed up.

    ReplyDelete
    Replies
    1. You're welcome. That's the main reason why I wrote this; so many disk space being wasted.

      Delete
  5. Thanks! Worked like a charm also for a Xubuntu 12.10 system. 2,7 GB freed!!
    Much appreciated.

    ReplyDelete
  6. Thank you very much. Works flawlessly.

    ReplyDelete
  7. Thank you, just freed up 1.6 GB!

    ReplyDelete
  8. Thanks so much. I had put xubuntu on an ancient laptop and couldn't update anymore because there was too much junk in the boot. 456MB is a lot of freed up room for this old clunker.

    ReplyDelete
  9. Excellent! Thank you very much!

    ReplyDelete
  10. Worked great. I appreciate the command snippet. Makes sense what it's doing. I saved almost a gig. One nit. Warning: Setting GRUB_TIMEOUT to a non-zero value when GRUB_HIDDEN_TIMEOUT is set is no longer supported.

    I'm in a hurry and don't have time to look into that warning right now. I'll write again if I get enlightened. Meanwhile maybe it's obvious to you what changed since you wrote your snippet.

    ReplyDelete
  11. Ignore my previous comment. I spoke too soon. This wiped all my kernels. Fortunately, it was only a VirtualBox guest. When I rebooted, only memtest86 would run.

    ReplyDelete
    Replies
    1. That's odd. I've used this dozens of times and never had any problem :(

      Delete
    2. No worries. I should have tested it myself before running. I recovered my guest and reproduced. I got far enough to see that COLUMNs was too short and strings were being truncated in the dpkg part of your solution. I was in a hurry so didn't get the automated pipeline working. When I do I'll write again. But this is just a warning to all that to be safe, set COLUMNS=255.

      Delete

Post a Comment

Popular posts from this blog

[HOW TO] Create QR Codes in Xubuntu 12.04

[FIX] VLC is unable to open the MRL (smb://)

Add items to Xfce Applications Menu