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



11 comment
As a Ubuntu user you might want to look at the bikeshed package and the included purge-old-kernels.sh script.
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`)Show off!
Or, intall & use Bleachbit, and clean up a bunch of stuff at once.
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)}')Ubuntu tweak is also very easy and fast.
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 purgeOr 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?
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]+//')")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.
The script should be updated to work with the newest Debian distros, where the currently used kernel lies in the packages lnux-image-ARCH and linux-headers-ARCH (where ARCH can be “amd64”, etc).