How To Remove Old CentOS Kernel images

H

When you update CentOS and there is a new kernel CentOS will install the new kernel but not remove the old kernel, this can become problematic when you have a very small /boot partition so it is a good idea to remove the old kernels that are no longer needed. Please note that if you have a large /boot partition it is not necessary to delete the old kernels.

Before deleting any of your old kernels make sure that you are booted onto the latest installed kernel.

We can see the available space in the /boot mount by executing df -h /boot/

[root@centos]# df -h /boot/
Filesystem      Size  Used Avail Use% Mounted on
/dev/sda2       1014M  122M  893M  12% /boot

As you can see my system has already been removed of the old kernels, your results will be different.

To check which kernels you have installed on your box you need to execute the command:

[root@centos]# rpm -q kernel
kernel-3.10.0-229.el7.x86_64
kernel-3.10.0-229.20.1.el7.x86_64
kernel-3.10.0-229.11.1.el7.x86_64

In this case, the latest kernel is kernel-3.10.0-229.11.1.el7.x86_64 so we need to make sure that the system is booted using this kernel. To confirm which kernel our system is running, run uname to confirm.

[root@centos]# uname -r
3.10.0-229.11.1.el7.x86_64

After we verify that we have the latest kernel installed and we are currently booted on it we can begin to remove the old kernels.

Before we start that though, we need to install the yum-utils which has the required package (package-cleanup) to remove the unused kernels.

Let’s install it with:

[root@centos]# yum install yum-utils -y

Once this has been installed, to remove the old kernels we have to execute the package-cleanup command.

The package cleanup command has options, the two main ones that we are going to use are --oldkernels and --count=(number). The --count=(number) option can be added to state which kernels need to be removed, and for this case we are going to say we want to cleanup all old kernels other than the one installed. For my sake I want to leave one old kernel on the system so I will use the option --count=1. If you would like to remove ALL old kernels you can do that with the option --count=0.

I personally believe that it is a good idea to leave one old kernel as a backup so I will execute this command:

[root@centos]# package-cleanup --oldkernels --count=1

Once this has finished executing, we can verify that all of the old kernels have been removed by running the rpm command.

[root@centos]# rpm -q kernel
kernel-3.10.0-229.20.1.el7.x86_64
kernel-3.10.0-229.11.1.el7.x86_64

If you have any comments of questions, please use the comment box below and I will do my best to help out.

About the author

3 comments

By Nick

Recent Posts