How to remove old unused kernels on CentOS Linux 8.x Linux when /boot has no free disk space

If your /boot partition is full (100% use of space), the easiest way to fix the problem is to delete old and unused kernels that are often taking the space. The following article describes the process of removing unused kernels on CentOS Linux 8.x Linux when /boot partition has no free disk space.

The boot partition is a primary partition that contains the boot loader (responsible for booting the operating system), but also boot files (kernel, initrd, and boot loader) are mounted at /boot.

In my case, I see that I am using pretty much all available space and I’ve confirmed that I have 3 kernels installed in the /boot partition:

df -h /boot

rpm -q kernel

Let’s assume that I want to keep the latest two kernels (the current and the one before as a backup), but remove any kernel older than that.

You can’t just go to /root and start deleting the kernel files related to (in my case ‘kernel-4.18.0-147.8.1.el8_1.x86_64’  highlighted below):

There is an easier way. We can use DNF to keep only the two latest kernels and remove any older kernels from the /boot partition:

Note: you can change the latest-limit property from 2 to any number of kernels you want to keep.

dnf remove $(dnf repoquery --installonly --latest-limit=-2 -q)

As you can see, in my case, I wanted to keep only two latest kernels and the DNF command properly identified the 3rd kernel backup and uninstalled it:

Now the boot partition is clean again:

If you don’t want to keep manually removing old kernels, there is a simple fix.

Adjust the DNF configuration to keep only the -n number of desired kernels:

Edit the /etc/dnf/dnf.conf file with nano or another editor of your choice:

nano /etc/dnf/dnf.conf

And change the installonly_limit to the maximum number of kernels you want to keep on your system:

And that’s it, enjoy your clean /boot partition!