How to remove old kernels in Grub2, including files.

Using the Disk Usage Analyzer, I found that the Linux kernels are located in 4 areas of the root directory: /boot /lib/firmware /lib/modules /usr/src

All we have to do is, go to each directory and remove the files or directories that are not needed.

Use the command [rm] for files and [rm -r] for directories. You have to use [sudo su] to have administrative account, or use [sudo (command)] to invoke administrative command.

For our example, we will use the linux kernel version 4.4.0-112 You can choose which version you will remove.

at /boot = kernels are all files : abi-4.4.0-112-generic config-4.4.0-112-generic initrd.img-4.4.0-112-generic retpoline-4.4.0-112-generic System.map-4.4.0-112-generic vmliniz-4.4.0-112-generic

(note: not all kernel versions have these files included above)

at /lib/firmware = kernels are in subdirectories : 4.4.0-112-generic

at /lib/modules = kernels are in subdirectories : 4.4.0-112-generic

at /usr/src = kernels are in subdirectories : linux-headers-4.4.0-112 linux-headers-4.4.0-112-generic

Now all we need to do is use the command [update-grub]

This will free your hard disk around 300MB worth of disk space for every kernel that you will remove.

Please note that I am using Ubuntu 16.04 Xenial.

And if anybody knows how to delete files in GUI, please let me know, it would be alot easier.

  • 1
    DO NOT use sudo rm for ANY files in /boot unless you know exactly what you are doing. Most of those files are created and removed by apt (and apt hooks). If you muck with them, the resulting apt errors will block most apt installs, removals, and upgrades. They can be quite tedious to repair, and you have better things to do with your time. – user535733 Jun 25 '18 at 17:24

sudo apt autoremove will do the trick.

It will leave the two most recent kernels, and remove the rest. It will also remove other packages that are not needed any more, that is packages that was automatically installed as dependencies to other packages that has later been uninstalled.

| improve this answer | |

Ubuntu before 18.04

List old kernel(s):

dpkg -l 'linux-*' | sed '/^ii/!d;/'"$(uname -r | sed "s/\(.*\)-\([^0-9]\+\)/\1/")"'/d;s/^[^ ]* [^ ]* \([^ ]*\).*/\1/;/[0-9]/!d'

Uninstall old kernel(s):

dpkg -l 'linux-*' | sed '/^ii/!d;/'"$(uname -r | sed "s/\(.*\)-\([^0-9]\+\)/\1/")"'/d;s/^[^ ]* [^ ]* \([^ ]*\).*/\1/;/[0-9]/!d' | xargs sudo apt-get -y purge

Ubuntu 18.04 and later

since 18.04, this two liner should be applied as the previous one liner may uninstall other packages

dpkg --list | grep 'linux-image' | awk '{ print $2 }' | sort -V | sed -n '/'"$(uname -r | sed "s/\([0-9.-]*\)-\([^0-9]\+\)/\1/")"'/q;p' | xargs apt-get -y purge
dpkg --list | grep 'linux-headers' | awk '{ print $2 }' | sort -V | sed -n '/'"$(uname -r | sed "s/\([0-9.-]*\)-\([^0-9]\+\)/\1/")"'/q;p' | xargs apt-get -y purge

Uninstalling of kernel will run the grub reconfigure process

| improve this answer | |

Uninstall the kernels you no longer need using your package manager, this will take care of the files as well as the grub entries, as long as you haven’t made manual changes like custom entries and custom files.

| improve this answer | |

Not the answer you're looking for? Browse other questions tagged or ask your own question.