Ubuntu 18.04 remove all unused old kernels

Originally published at: https://www.cyberciti.biz/faq/ubuntu-18-04-remove-all-unused-old-kernels/

How do I remove all unused old kernels in Ubuntu Linux 18.04 LTS using the command line?

Just update with:

sudo apt update && sudo apt upgrade -fy && sudo apt autoremove -fy

And you will not have to clean anything unused

1 Like

Doesn’t work for me…

This works:
dpkg -l 'linux-*' | sed '/^ii/!d;/hwe/d;/'"$(uname -r | sed "s/\(.*\)-\([^0-9]\+\)/\1/")"'/d;s/^[^ ]* [^ ]* \([^ ]*\).*/\1/;/[0-9]/!d' | xargs sudo apt -y purge; update-grub

1 Like

2 easiest ways I’ve found:

sudo apt-get purge $(dpkg -l linux-{image,headers}-"[0-9]*" | awk '/ii/{print $2}' | grep -ve "$(uname -r | sed -r 's/-[a-]+//')")

better yet,

purge-old-kernels 

If it’s not already installed (it was on my 16.04 → 18 upgrade) you can get it by running

apt-get -y install byobu

One trick I’ve found if you have a separate boot partition and it’s 100% full is to move some of the old kernels out and then sym-link (ln -s) them back into /boot that way the file is still there for apt to be happy but it isn’t taking up the space in the partition

-cheers

1 Like

“0 upgraded, 0 newly installed, 0 to remove and 0 not upgraded.”

I can see the other linux kernels though.

What do you mean by other Linux kernels? Can you post output using the dpkg?

dpkg --list | grep '^ii' | grep linux-image

Remember custom compiled kernel can not be removed using this method.