Delete old kernels in Linux and save space

In this post we will look at the procedure to remove old kernels in Linux

As seen below my boot partition is running out of space and is been used by old kernels

$ df -h | grep boot
/dev/nvme0n1p1 295M 289M 6.2M 98% /boot
$ rpm -q kernel
kernel-4.18.0-240.el8.x86_64
kernel-4.18.0-240.15.1.el8_3.x86_64
kernel-4.18.0-240.22.1.el8_3.x86_64

How to find the kernels to be deleted

Use dnf and comm to find the old versions that can be removed.

$ sudo dnf repoquery --installonly --latest-limit 99 -q > /tmp/allversions
$ sudo dnf repoquery --installonly --latest-limit 1 -q > /tmp/latest
$ comm -3  /tmp/allversions  /tmp/latest 
 kernel-0:4.18.0-240.15.1.el8_3.x86_64
 kernel-0:4.18.0-240.el8.x86_64
 kernel-core-0:4.18.0-240.15.1.el8_3.x86_64
 kernel-core-0:4.18.0-240.el8.x86_64
 kernel-devel-0:4.18.0-240.15.1.el8_3.x86_64
 kernel-devel-0:4.18.0-240.el8.x86_64
 kernel-modules-0:4.18.0-240.15.1.el8_3.x86_64
 kernel-modules-0:4.18.0-240.el8.x86_64
$ comm -3 /tmp/allversions /tmp/latest > /tmp/old-kernels-for-removal

Remove Kernel

Execute following command to delete old kernels

$ sudo dnf --noautoremove remove $(cat /tmp/old-kernels-for-removal )
Updating Subscription Management repositories.
Dependencies resolved.
Package Architecture Version Repository Size
Removing:
kernel x86_64 4.18.0-240.el8 @anaconda 0
kernel x86_64 4.18.0-240.15.1.el8_3 @rhel-8-for-x86_64-baseos-rpms 0
kernel-core x86_64 4.18.0-240.el8 @anaconda 62 M
kernel-core x86_64 4.18.0-240.15.1.el8_3 @rhel-8-for-x86_64-baseos-rpms 62 M
kernel-devel x86_64 4.18.0-240.el8 @anaconda 47 M
kernel-devel x86_64 4.18.0-240.15.1.el8_3 @rhel-8-for-x86_64-baseos-rpms 47 M
kernel-modules x86_64 4.18.0-240.el8 @anaconda 21 M
kernel-modules x86_64 4.18.0-240.15.1.el8_3 @rhel-8-for-x86_64-baseos-rpms 21 M

Transaction Summary

Remove 8 Packages

Freed space: 260 M
Is this ok [y/N]: y
Running transaction check
Transaction check succeeded.
Running transaction test
Transaction test succeeded.
Running transaction
Preparing : 1/1
Erasing : kernel-4.18.0-240.15.1.el8_3.x86_64 1/8
Running scriptlet: kernel-4.18.0-240.15.1.el8_3.x86_64 1/8
Erasing : kernel-4.18.0-240.el8.x86_64 2/8
Running scriptlet: kernel-4.18.0-240.el8.x86_64 2/8
Erasing : kernel-devel-4.18.0-240.15.1.el8_3.x86_64 3/8
Erasing : kernel-devel-4.18.0-240.el8.x86_64 4/8
Erasing : kernel-modules-4.18.0-240.el8.x86_64 5/8
Running scriptlet: kernel-modules-4.18.0-240.el8.x86_64 5/8
Erasing : kernel-modules-4.18.0-240.15.1.el8_3.x86_64 6/8
Running scriptlet: kernel-modules-4.18.0-240.15.1.el8_3.x86_64 6/8
Running scriptlet: kernel-core-4.18.0-240.15.1.el8_3.x86_64 7/8
Erasing : kernel-core-4.18.0-240.15.1.el8_3.x86_64 7/8
Running scriptlet: kernel-core-4.18.0-240.15.1.el8_3.x86_64 7/8
Running scriptlet: kernel-core-4.18.0-240.el8.x86_64 8/8
Erasing : kernel-core-4.18.0-240.el8.x86_64 8/8
Running scriptlet: kernel-core-4.18.0-240.el8.x86_64 8/8
Verifying : kernel-4.18.0-240.el8.x86_64 1/8
Verifying : kernel-4.18.0-240.15.1.el8_3.x86_64 2/8
Verifying : kernel-core-4.18.0-240.el8.x86_64 3/8
Verifying : kernel-core-4.18.0-240.15.1.el8_3.x86_64 4/8
Verifying : kernel-devel-4.18.0-240.el8.x86_64 5/8
Verifying : kernel-devel-4.18.0-240.15.1.el8_3.x86_64 6/8
Verifying : kernel-modules-4.18.0-240.el8.x86_64 7/8
Verifying : kernel-modules-4.18.0-240.15.1.el8_3.x86_64 8/8
Installed products updated.

Removed:
kernel-4.18.0-240.el8.x86_64 kernel-4.18.0-240.15.1.el8_3.x86_64 kernel-core-4.18.0-240.el8.x86_64 kernel-core-4.18.0-240.15.1.el8_3.x86_64
kernel-devel-4.18.0-240.el8.x86_64 kernel-devel-4.18.0-240.15.1.el8_3.x86_64 kernel-modules-4.18.0-240.el8.x86_64 kernel-modules-4.18.0-240.15.1.el8_3.x86_64

Complete!

Check space after deletion, as we see 18% is saved.

$ df -h | grep boot
/dev/nvme0n1p1 295M 206M 90M 70% /boot

0 Comments on Delete old kernels in Linux and save space

Leave a Comment

Your email address will not be published. Required fields are marked *

You Might Be Interested In