2

I am on Ubuntu 14.04 and I have a lot of old kernels that I want to remove.

This question can help to find and remove them, but there is a problem:

Each time dpkg removes a single kernel, it runs /etc/kernel/postinst.d/zz-update-grub and update-initramfs over all the kernels currently remaining.

As a result, the uninstall takes forever!

I want to remove all the packages in one go, and then run update-grub once at the end. How can I disable the unnecessary steps?

(I use unattended-upgrades. Perhaps that's how I got into this situation.)

joeytwiddle
  • 1,957
  • A closely related question: https://askubuntu.com/questions/681953/removing-older-kernels-takes-forever/728561#728561 – joeytwiddle May 30 '18 at 08:58

1 Answers1

4

Here is what I did:

  1. Edit the file /usr/sbin/grub-mkconfig (as root) and add the following line at the top: (*)

    exit 0
    
  2. Then run apt-get purge or dpkg -P as suggested in the linked question, to remove the unwanted packages. For example, I ran:

    sudo apt-get purge $(dpkg --list | grep -E 'linux-(image|signed-image|headers)' | awk '{ print $2 }' | sort -V | grep -vF "$(uname -r | grep -o '[0-9.-]*' | sed 's/-$//')")
    

    (Feeling nervous? Put echo at the front to see what it will purge, before running it for real.)

  3. Then don't forget to edit grub-mkconfig again and remove the line:

    exit 0
    
  4. Then finally run:

    sudo update-grub
    

    to get your boot loader up-to-date.

This process recovered 11GB of space in under 30 minutes.

I also followed this excellent suggestion to prevent unattended-upgrades from building up my kernel packages again in the future.

(*) I also considered disabling /usr/sbin/dkms and /usr/sbin/extlinux-update and /usr/sbin/update-initramfs using the exit 0 trick, but these didn't save that much time, and they left files around in /lib/modules/[kernel_version]/ and /boot/initrd.img-[kernel_version] so not really a good option.

joeytwiddle
  • 1,957