[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.
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
Thank you very much for that - worked in Debian Wheezy too
ReplyDeleteYou're welcome. I'm glad this worked for you too.
DeleteI always used this command
ReplyDeletedpkg -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?
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)
DeleteYour command is lacking the update-grub2, though, which updates the boot menu.
I'm using apt-get --purge remove linux-kernel-xxx linux-headers-xxx
ReplyDeleteYou have to manually replace xxx with some version. Mine does that automatically.
DeleteWorked great, thanks! Over 1 gig of hard disk space freed up.
ReplyDeleteYou're welcome. That's the main reason why I wrote this; so many disk space being wasted.
DeleteThanks! Worked like a charm also for a Xubuntu 12.10 system. 2,7 GB freed!!
ReplyDeleteMuch appreciated.
Thank you very much. Works flawlessly.
ReplyDeleteYou're welcome.
DeleteThank you, just freed up 1.6 GB!
ReplyDeleteThanks 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.
ReplyDeleteExcellent! Thank you very much!
ReplyDeleteWorked 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.
ReplyDeleteI'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.
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.
ReplyDeleteThat's odd. I've used this dozens of times and never had any problem :(
DeleteNo 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