≡ Menu

Debian / Ubuntu Linux Delete Old Kernel Images Command

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, enter:
# dpkg --list | egrep -i --color 'linux-image|linux-headers'
Sample outputs:

Fig.01: Check what kernel image(s) are installed on your system

Fig.01: Check what kernel image(s) are installed on your system (click to enlarge)

Step #2: Delete unwanted and unused kernel images

You can remove kernel images one by one using the following 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

A note about newer Ubuntu and Debian 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-get 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 ...

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
Share this tutorial on:

{ 10 comments… add one }
  • Jan Vonde November 23, 2015, 7:01 am

    As a Ubuntu user you might want to look at the bikeshed package and the included purge-old-kernels.sh script.

  • valic November 23, 2015, 8:45 am

    Or you can use a single command to do this:

    apt-get remove $(dpkg -l|egrep '^ii linux-(im|he)'|awk '{print $2}'|grep -v `uname -r`)

    • Jason November 27, 2015, 3:10 am

      Show off!

  • Col. Panek November 24, 2015, 2:36 am

    Or, intall & use Bleachbit, and clean up a bunch of stuff at once.

  • Michal M November 24, 2015, 2:50 pm

    sudo apt-get remove $(dpkg -l|awk '/^ii linux-image-/{print $2}'|sed 's/linux-image-//'|awk -v v=`uname -r` 'v>$0'|sed 's/-generic//'|awk '{printf("linux-headers-%s\nlinux-headers-%s-generic\nlinux-image-%s-generic\n",$0,$0,$0)}')

  • gerrit November 24, 2015, 2:58 pm

    Ubuntu tweak is also very easy and fast.

  • Rabin November 26, 2015, 10:18 pm

    Just like to add me one-liner to the mix,

    export KEEP_KERNEL=2; dpkg -l 'linux-image*' | awk '/^ii/ { print $2 }' | grep "[0-9]" | awk 'BEGIN{i=1}{print i++, $0}' | grep `uname -r` -B99 | sort -r | tail -n+$(($KEEP_KERNEL+2)) | awk '{print $2}'| xargs apt-get -y purge

  • Tuxworx December 1, 2015, 7:14 pm

    Or you can use the Synaptic package manager. Search for “linux-image” and It will give you a graphical list of installed kernels. As shown using the grep command above, you will be able to see that one of them is the newest (in that example, 3.13.0-68). Maybe search again for “3.13.0” to be more certain you aren’t missing any. Right-click to mark all of them that have a lower version number than 3.13.0-68 for deletion and click on “apply.” Make sure you are keeping one file type that ends with “68” for every file type for which you are deleting files that have a lower version number.

    Why not? You are running a graphical user interface, aren’t you? Why does everyone giving Ubuntu advice immediately go to complicated command-line methods when there is a graphical tool that can do the same thing?

  • Amos December 3, 2015, 11:58 am

    Similar to above…one command
    sudo apt-get purge $(dpkg -l linux-{image,headers}-"[0-9]*" | awk '/ii/{print $2}' | grep -ve "$(uname -r | sed -r 's/-[a-z]+//')")

  • Doug December 26, 2015, 8:04 pm

    Or… You can leave the last couple kernels in place in case something decides to bork and you need to roll back. The kernel is incredibly small.

Leave a Comment


   Tagged with: , , ,