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

Linux sysadmin blog - Linux/Unix Howtos and Tutorials - Linux bash shell scripting wiki