I‘m a new Ubuntu Linux user and noticed that old kernel still exists in my system. Why doesn’t Ubuntu remove old kernels automatically? How do I delete old unused kernel images to free disk space. How to remove unused old kernel images on Ubuntu Linux safely?
You need to delete and/or remove old kernels from system manually. Ubuntu and Debian based system keeps old kernel images so that system can be booted if newer kernel failed. The safest way to purge and remove old kernel is as follows. In this tutorial you will learn how to delete unused old kernel images on Ubuntu or Debian Linux to free disk space as well as the various state of linux-image package.
Step #1: Boot into new kernel
First, boot into newly installed kernel. Verify this with the following command:
$ uname -mrs
$ uname -a
Sample outputs:
Linux server1 3.13.0-68-generic #111-Ubuntu SMP Fri Nov 6 18:17:06 UTC 2015 x86_64 x86_64 x86_64 GNU/Linux
To list all installed Linux kernel images, type the following dpkg command:
# dpkg --list | egrep -i --color 'linux-image|linux-headers'
Sample outputs:

Step #2: Delete unwanted and unused kernel images
You can remove kernel images one by one using the following apt-get command or apt command syntax:
# apt-get --purge remove linux-image-3.13.0-67-generic
OR
$ sudo apt-get --purge remove linux-image-3.13.0-67-generic
The purge removes packages and any configuration files are deleted too.
A note about newer Ubuntu and Debian based system
On newer system all obsolete kernels and headers should automatically be flagged as no more needed, and thus can be purged with the following single command:
$ sudo apt --purge autoremove
OR
$ sudo apt-get --purge autoremove
Sample outputs:
Reading package lists... Done
Building dependency tree
Reading state information... Done
The following packages will be REMOVED:
linux-headers-3.2.0-93* linux-headers-3.2.0-93-virtual*
0 upgraded, 0 newly installed, 2 to remove and 0 not upgraded.
After this operation, 67.8 MB disk space will be freed.
Do you want to continue [Y/n]? y
(Reading database ... 227543 files and directories currently installed.)
Removing linux-headers-3.2.0-93-virtual ...
Removing linux-headers-3.2.0-93 ...
Please note that the autoremove is used to remove packages that were automatically installed to satisfy dependencies for other packages and are now no longer needed. This may remove rest of unwanted packages too.
Understanding package states in Ubuntu and Debian Linux
Consider the following example:
# dpkg --list | grep linux-image
Sample outputs:
rc linux-image-3.13.0-62-generic 3.13.0-62.102 amd64 Linux kernel image for version 3.13.0 on 64 bit x86 SMP rc linux-image-3.13.0-63-generic 3.13.0-63.103 amd64 Linux kernel image for version 3.13.0 on 64 bit x86 SMP rc linux-image-3.13.0-65-generic 3.13.0-65.106 amd64 Linux kernel image for version 3.13.0 on 64 bit x86 SMP rc linux-image-3.13.0-66-generic 3.13.0-66.108 amd64 Linux kernel image for version 3.13.0 on 64 bit x86 SMP rc linux-image-3.13.0-67-generic 3.13.0-67.110 amd64 Linux kernel image for version 3.13.0 on 64 bit x86 SMP ii linux-image-3.13.0-68-generic 3.13.0-68.111 amd64 Linux kernel image for version 3.13.0 on 64 bit x86 SMP rc linux-image-extra-3.13.0-62-generic 3.13.0-62.102 amd64 Linux kernel extra modules for version 3.13.0 on 64 bit x86 SMP rc linux-image-extra-3.13.0-63-generic 3.13.0-63.103 amd64 Linux kernel extra modules for version 3.13.0 on 64 bit x86 SMP rc linux-image-extra-3.13.0-65-generic 3.13.0-65.106 amd64 Linux kernel extra modules for version 3.13.0 on 64 bit x86 SMP rc linux-image-extra-3.13.0-66-generic 3.13.0-66.108 amd64 Linux kernel extra modules for version 3.13.0 on 64 bit x86 SMP rc linux-image-extra-3.13.0-67-generic 3.13.0-67.110 amd64 Linux kernel extra modules for version 3.13.0 on 64 bit x86 SMP ii linux-image-extra-3.13.0-68-generic 3.13.0-68.111 amd64 Linux kernel extra modules for version 3.13.0 on 64 bit x86 SMP ii linux-image-generic 3.13.0.68.74 amd64 Generic Linux kernel image
The first column indicates package flags like rc, ii. So, what do the various dpkg flags like ‘ii’ ‘rc’ mean?
- rc: It means package is in remove/deinstall state and only config file exists.
- ii: It means package is in install state and it is 100% installed on the system.
You can remove all linux-image packages in rc state using the following command:
# x=$(dpkg --list | grep -i linux-image | grep ^rc| awk '{ print $2}')
# echo "$x"
# apt-get --purge remove $x
Sample outputs:
Reading package lists... Done Building dependency tree Reading state information... Done The following packages will be REMOVED: linux-image-3.13.0-62-generic* linux-image-3.13.0-63-generic* linux-image-3.13.0-65-generic* linux-image-3.13.0-66-generic* linux-image-3.13.0-67-generic* linux-image-extra-3.13.0-62-generic* linux-image-extra-3.13.0-63-generic* linux-image-extra-3.13.0-65-generic* linux-image-extra-3.13.0-66-generic* linux-image-extra-3.13.0-67-generic* 0 upgraded, 0 newly installed, 10 to remove and 0 not upgraded. After this operation, 0 B of additional disk space will be used. Do you want to continue? [Y/n] y (Reading database ... 65623 files and directories currently installed.) Removing linux-image-3.13.0-62-generic (3.13.0-62.102) ... Purging configuration files for linux-image-3.13.0-62-generic (3.13.0-62.102) ... Examining /etc/kernel/postrm.d . run-parts: executing /etc/kernel/postrm.d/initramfs-tools 3.13.0-62-generic /boot/vmlinuz-3.13.0-62-generic run-parts: executing /etc/kernel/postrm.d/zz-update-grub 3.13.0-62-generic /boot/vmlinuz-3.13.0-62-generic Removing linux-image-3.13.0-63-generic (3.13.0-63.103) ... Purging configuration files for linux-image-3.13.0-63-generic (3.13.0-63.103) ... Examining /etc/kernel/postrm.d . run-parts: executing /etc/kernel/postrm.d/initramfs-tools 3.13.0-63-generic /boot/vmlinuz-3.13.0-63-generic run-parts: executing /etc/kernel/postrm.d/zz-update-grub 3.13.0-63-generic /boot/vmlinuz-3.13.0-63-generic Removing linux-image-3.13.0-65-generic (3.13.0-65.106) ... Purging configuration files for linux-image-3.13.0-65-generic (3.13.0-65.106) ... Examining /etc/kernel/postrm.d . run-parts: executing /etc/kernel/postrm.d/initramfs-tools 3.13.0-65-generic /boot/vmlinuz-3.13.0-65-generic run-parts: executing /etc/kernel/postrm.d/zz-update-grub 3.13.0-65-generic /boot/vmlinuz-3.13.0-65-generic Removing linux-image-3.13.0-66-generic (3.13.0-66.108) ... Purging configuration files for linux-image-3.13.0-66-generic (3.13.0-66.108) ... Examining /etc/kernel/postrm.d . run-parts: executing /etc/kernel/postrm.d/initramfs-tools 3.13.0-66-generic /boot/vmlinuz-3.13.0-66-generic run-parts: executing /etc/kernel/postrm.d/zz-update-grub 3.13.0-66-generic /boot/vmlinuz-3.13.0-66-generic Removing linux-image-3.13.0-67-generic (3.13.0-67.110) ... Purging configuration files for linux-image-3.13.0-67-generic (3.13.0-67.110) ... Examining /etc/kernel/postrm.d . run-parts: executing /etc/kernel/postrm.d/initramfs-tools 3.13.0-67-generic /boot/vmlinuz-3.13.0-67-generic run-parts: executing /etc/kernel/postrm.d/zz-update-grub 3.13.0-67-generic /boot/vmlinuz-3.13.0-67-generic Removing linux-image-extra-3.13.0-62-generic (3.13.0-62.102) ... Purging configuration files for linux-image-extra-3.13.0-62-generic (3.13.0-62.102) ... Removing linux-image-extra-3.13.0-63-generic (3.13.0-63.103) ... Purging configuration files for linux-image-extra-3.13.0-63-generic (3.13.0-63.103) ... Removing linux-image-extra-3.13.0-65-generic (3.13.0-65.106) ... Purging configuration files for linux-image-extra-3.13.0-65-generic (3.13.0-65.106) ... Removing linux-image-extra-3.13.0-66-generic (3.13.0-66.108) ... Purging configuration files for linux-image-extra-3.13.0-66-generic (3.13.0-66.108) ... Removing linux-image-extra-3.13.0-67-generic (3.13.0-67.110) ... Purging configuration files for linux-image-extra-3.13.0-67-generic (3.13.0-67.110) ...
Type the following command again to see the results:
# dpkg --list | egrep -i --color 'linux-image|linux-headers'
Sample outputs:
ii linux-headers-3.13.0-68 3.13.0-68.111 all Header files related to Linux kernel version 3.13.0 ii linux-headers-3.13.0-68-generic 3.13.0-68.111 amd64 Linux kernel headers for version 3.13.0 on 64 bit x86 SMP ii linux-headers-generic 3.13.0.68.74 amd64 Generic Linux kernel headers ii linux-image-3.13.0-68-generic 3.13.0-68.111 amd64 Linux kernel image for version 3.13.0 on 64 bit x86 SMP ii linux-image-extra-3.13.0-68-generic 3.13.0-68.111 amd64 Linux kernel extra modules for version 3.13.0 on 64 bit x86 SMP ii linux-image-generic 3.13.0.68.74 amd64 Generic Linux kernel image
Deleting all old kernels easily
Consider the following bash kung-fu:
# Get kernel version # v="$(uname -r | awk -F '-virtual' '{ print $1}')" # Create ignore list to avoid deleting the running kernel # i="linux-headers-virtual|linux-image-virtual|linux-headers-${v}|linux-image-$(uname -r)" # Display the list # echo dpkg --list | egrep -i 'linux-image|linux-headers' | awk '/ii/{ print $2}' | egrep -v "$i" |
Sample outputs:
linux-headers-3.2.0-31 linux-headers-3.2.0-31-virtual linux-headers-3.2.0-52 linux-headers-3.2.0-52-virtual linux-headers-3.2.0-60 linux-headers-3.2.0-60-virtual linux-headers-3.2.0-61 linux-headers-3.2.0-61-virtual linux-headers-3.2.0-92 linux-headers-3.2.0-92-virtual linux-headers-3.2.0-94 linux-headers-3.2.0-94-virtual linux-image-3.2.0-89-virtual linux-image-3.2.0-91-virtual linux-image-3.2.0-92-virtual linux-image-3.2.0-93-virtual linux-image-3.2.0-94-virtual
To delete it simply run it as follows:
v="$(uname -r | awk -F '-virtual' '{ print $1}')" i="linux-headers-virtual|linux-image-virtual|linux-headers-${v}|linux-image-$(uname -r)" apt-get --purge remove $(dpkg --list | egrep -i 'linux-image|linux-headers' | awk '/ii/{ print $2}' | egrep -v "$i") |
Sample outputs:
Reading package lists... Done Building dependency tree Reading state information... Done The following packages will be REMOVED: linux-headers-3.2.0-31* linux-headers-3.2.0-31-virtual* linux-headers-3.2.0-52* linux-headers-3.2.0-52-virtual* linux-headers-3.2.0-60* linux-headers-3.2.0-60-virtual* linux-headers-3.2.0-61* linux-headers-3.2.0-61-virtual* linux-headers-3.2.0-92* linux-headers-3.2.0-92-virtual* linux-headers-3.2.0-94* linux-headers-3.2.0-94-virtual* linux-image-3.2.0-89-virtual* linux-image-3.2.0-91-virtual* linux-image-3.2.0-92-virtual* linux-image-3.2.0-93-virtual* linux-image-3.2.0-94-virtual* 0 upgraded, 0 newly installed, 17 to remove and 0 not upgraded. After this operation, 588 MB disk space will be freed. Do you want to continue [Y/n]? y (Reading database ... 206225 files and directories currently installed.) Removing linux-headers-3.2.0-31-virtual ... Removing linux-headers-3.2.0-31 ... Removing linux-headers-3.2.0-52-virtual ... Removing linux-headers-3.2.0-52 ... Removing linux-headers-3.2.0-60-virtual ... Removing linux-headers-3.2.0-60 ... .... .. ... Purging configuration files for linux-image-3.2.0-94-virtual ... Examining /etc/kernel/postrm.d . run-parts: executing /etc/kernel/postrm.d/initramfs-tools 3.2.0-94-virtual /boot/vmlinuz-3.2.0-94-virtual run-parts: executing /etc/kernel/postrm.d/zz-update-grub 3.2.0-94-virtual /boot/vmlinuz-3.2.0-94-virtual