One of the things that seriously annoys me but that pops up from time to time, is the fact that my Linux /boot partition fills up with old, unused kernels. I know, I should have created a bigger partition but this installation is old, updated several times over the course of a few years and back then, kernels were smaller. So how do we safely clean up boot by deleting unused kernels?
I don’t take credit for inventing this method, it’s just that I got tired of searching it on duckduckgo each time I need it so I decided to put it somewhere I can easily find it. So where better than my own blog?
Before we can start deleting kernel images, we need to know which kernel we are running at the moment. We don’t want to remove that because obviously, we’ll need at least one kernel image to run the computer. The command to find out which kernel we are currently running is as follows.
$ uname -r
Running this command in a terminal will result in the running kernel image being reported to you, for instance like so.
We are going to keep that kernel version (we obviously need one kernel) and delete all the others, which are no longer necessary. We know which kernel we are running, so now we’re going to look at all the kernel images that are actually installed on the system. Usually, there are one or two older images preserved by the update routine.
The following command will list all kernel images installed, as shown below.
dpkg --list 'linux-image*' | grep ^ii ii linux-image-4.2.0-18-generic 4.2.0-18.22 amd64 Linux kernel image for version 4.2.0 on 64 bit x86 SMP ii linux-image-4.2.0-19-generic 4.2.0-19.23 amd64 Linux kernel image for version 4.2.0 on 64 bit x86 SMP ii linux-image-extra-4.2.0-18-generic 4.2.0-18.22 amd64 Linux kernel extra modules for version 4.2.0 on 64 bit x86 SMP ii linux-image-extra-4.2.0-19-generic 4.2.0-19.23 amd64 Linux kernel extra modules for version 4.2.0 on 64 bit x86 SMP ii linux-image-generic 220.127.116.11.21 amd64 Generic Linux kernel image
That is the list of all kernel images present on the system. As you can see, there are two different kernels present on the system, versions 4.2.0-18 and 4.2.0-19. The latter is our current kernel so we’re going to leave that alone, version 4.2.0-18 and its associated files.
$ sudo apt-get remove linux-image-4.2.0-18-generic
The command above will remove the kernel from the system. It will generate some output after you’ve confirmed you want to run it, generating startup files and such. Once all that is complete, we can delete the rest of left over files by using the autoremove option of apt-get.
$ sudo apt-get autoremove
This will generate a lot more output as any residual files are also removed. Repeat the above for each kernel version you wish to remove but be sure not to remove your current kernel as this will leave your system unable to boot!
When you are done deleting old, unused kernels, you can update the grub kernel list as follows. This will remove the old kernels from the boot configuration.
$ sudo update-grub
Congratulations, you’ve managed to safely clean up /boot on your Ubuntu system (or other Debian bases system).