Several ways to remove the old kernel from Linux

For LVM-enabled (Logical Volume Manager), encrypted, or disk storage space, we can periodically or manually remove unused Linux kernels to prevent the /boot partition from running out of space. It is very easy to remove the old Linux Kernel from Linux, you can do it manually or use automated. Below are several ways to remove the old kernel from Linux.

Automatically remove Linux legacy kernel

Use the following command to automatically remove legacy kernels and packages that are no longer needed for Linux systems:

sudo apt autoremove purge

Execute the following command in the terminal to enable unattended upgrade:

sudo dpkgreconfigure unattendedupgrades

Semi-automatic remove Linux legacy kernel

If you have installed the latest kernel from Kernel PPA or installed your own manually compiled kernel, then the purge-old-kernels script is the best way to clean up these old kernels.

First, install the byobu package with the following command:

sudo apt install byobu

Then execute the following commands

sudo purgeoldkernels

Manually remove Linux legacy kernel

If your /boot partition is full and you can no longer use apt to upgrade, install, and remove packages and related dependencies, you can use the dpkg command to do it all manually.

View the current kernel version:

uname r

List all other kernel versions that do not include the current kernel version:

dpkg l | tail n +6 | grep E ‘linux-image-[0-9]+’ | grep Fv $(uname r)

The output may include the following three states of the kernel image:

rc: indicates that it has been removed
ii: indicates that the removal condition is met (removable)
iU: has entered the apt installation queue, but has not been installed (not removable).

For example, to remove the old “linux-image-5.0.0-31-generic” kernel with status ii, you can use the following command:

sudo dpkg purge linux-image-5.0.0-31-generic