Fedora: Remove specific kernel version

For several reasons we might need to remove a specific installed kernel version (either because we have installed a testing version or we want to remove an old one to free some space from the /boot directory).

Run the following command to list the installed kernerl versions:

rpm -qa kernel 

You must be able to see a similar result as above:

kernel-6.4.7-200.fc38.x86_64
kernel-6.5.0-0.0.next.20230804.232.vanilla.fc38.x86_64
kernel-6.5.0-0.0.next.20230807.235.vanilla.fc38.x86_64
kernel-6.5.0-0.0.next.20230809.237.vanilla.fc38.x86_64

Then to remove for example the kernel-6.5.0-0.0.next.20230804.232.vanilla.fc38.x86_64 run:

sudo dnf remove kernel-6.5.0-0.0.next.20230804.232.vanilla.fc38.x86_64 
Dependencies resolved.
==========================================================================================================================================================================================================================================================================================
 Package                                        Architecture                                   Version                                                                            Repository                                                                                         Size
==========================================================================================================================================================================================================================================================================================
Removing:
 kernel                                         x86_64                                         6.5.0-0.0.next.20230804.232.vanilla.fc38                                           @copr:copr.fedorainfracloud.org:group_kernel-vanilla:next                                           0  

Transaction Summary
==========================================================================================================================================================================================================================================================================================
Remove  1 Package
Note: If we have installed a custom Linux kernel we have to remove manually the records from the /boot and /lib/module directories
sudo rm -fr /boot/config-6.5.0-0.0.next.20230804.232.vanilla.fc38.x86_64 \
            /boot/initramfs-6.5.0-0.0.next.20230804.232.vanilla.fc38.x86_64.img \
            /boot/symvers-6.5.0-0.0.next.20230804.232.vanilla.fc38.x86_64.xz \
            /boot/System.map-6.5.0-0.0.next.20230804.232.vanilla.fc38.x86_64 \
            /boot/vmlinuz-6.5.0-0.0.next.20230804.232.vanilla.fc38.x86_64 \
            /boot/loader/entries/2884f60588b44bf8ba298eb34d4d6ad4-6.5.0-0.0.next.20230804.232.vanilla.fc38.x86_64.conf \
            /lib/modules/6.5.0-0.0.next.20230804.232.vanilla.fc38.x86_64/* 

Note: We can use find command to easily list the needed paths:

sudo find /boot,/lib -name "*6.5.0-0.0*"
sudo find /lib -name "*6.5.0-0.0*"

Then we need to update the Grub loader:

sudo grub2-mkconfig -o /boot/efi/EFI/fedora/grub.cfg                      


Generating grub configuration file ...
Found Windows Boot Manager on /dev/nvme0n1p1@/efi/Microsoft/Boot/bootmgfw.efi
Adding boot menu entry for UEFI Firmware Settings ...
done

It is also good to increase the number of the kernel versions that the system we want to keep, in case of a failed update/upgrade:

We need to change the installonly_limit value at the /etc/dnf/dnf.conf file. Be aware that if we increase the number too high then we run the risk of filling up the /boot partition.

# see `man dnf.conf` for defaults and possible options

[main]
gpgcheck=True
installonly_limit=6 # the default value was 3 
clean_requirements_on_remove=True
best=False
skip_if_unavailable=True
max_parallel_downloads=10
fastestmirror=True