Autoremove does not remove old headers

Solution 1:

autoremove will remove any package that:

  • is auto installed
  • is not included in APT::NeverAutoRemove APT configuration
  • don't have any dependents

Here is the debuging steps we followed

  1. Checked auto/manual installation

    aptitude search '^linux* ?installed'
    
  2. Checked currently declared versions under APT::NeverAutoRemove

    cat /etc/apt/apt.conf.d/01autoremove-kernels
    
  3. Checked dependency

    apt-cache rdepends linux-headers-4.4.0-38-generic
    

    brings nothing but

    sudo apt-get -o Debug::pkgAutoRemove=1 -s autoremove 2> debug-autoremove.txt
    
    ...
    Following dep: virtualbox-5.0:amd64 5.0.20-106931~Ubuntu~wily Recommends linux-headers:amd64 , provided by linux-headers-4.4.0-43-lowlatency:amd64 4.4.0-43.63
    Following dep: virtualbox-5.0:amd64 5.0.20-106931~Ubuntu~wily Recommends linux-headers:amd64 , provided by linux-headers-4.4.0-43-generic:amd64 4.4.0-43.63
    Following dep: virtualbox-5.0:amd64 5.0.20-106931~Ubuntu~wily Recommends linux-headers:amd64 , provided by linux-headers-4.4.0-42-lowlatency:amd64 4.4.0-42.62
    Following dep: virtualbox-5.0:amd64 5.0.20-106931~Ubuntu~wily Recommends linux-headers:amd64 , provided by linux-headers-4.4.0-42-generic:amd64 4.4.0-42.62
    Following dep: virtualbox-5.0:amd64 5.0.20-106931~Ubuntu~wily Recommends linux-headers:amd64 , provided by linux-headers-4.4.0-38-lowlatency:amd64 4.4.0-38.57
    Following dep: virtualbox-5.0:amd64 5.0.20-106931~Ubuntu~wily Recommends linux-headers:amd64 , provided by linux-headers-4.4.0-38-generic:amd64 4.4.0-38.57
    Following dep: virtualbox-5.0:amd64 5.0.20-106931~Ubuntu~wily Recommends linux-headers:amd64 , provided by linux-headers-4.4.0-36-lowlatency:amd64 4.4.0-36.55
    Following dep: virtualbox-5.0:amd64 5.0.20-106931~Ubuntu~wily Recommends linux-headers:amd64 , provided by linux-headers-4.4.0-36-generic:amd64 4.4.0-36.55
    Following dep: virtualbox-5.0:amd64 5.0.20-106931~Ubuntu~wily Recommends linux-headers:amd64 , provided by linux-headers-4.4.0-34-lowlatency:amd64 4.4.0-34.53
    Following dep: virtualbox-5.0:amd64 5.0.20-106931~Ubuntu~wily Recommends linux-headers:amd64 , provided by linux-headers-4.4.0-34-generic:amd64 4.4.0-34.53
    Following dep: virtualbox-5.0:amd64 5.0.20-106931~Ubuntu~wily Recommends linux-headers:amd64 , provided by linux-headers-4.4.0-31-lowlatency:amd64 4.4.0-31.50
    Following dep: virtualbox-5.0:amd64 5.0.20-106931~Ubuntu~wily Recommends linux-headers:amd64 , provided by linux-headers-4.4.0-31-generic:amd64 4.4.0-31.50
    Following dep: virtualbox-5.0:amd64 5.0.20-106931~Ubuntu~wily Recommends linux-headers:amd64 , provided by linux-headers-4.4.0-28-lowlatency:amd64 4.4.0-28.47
    Following dep: virtualbox-5.0:amd64 5.0.20-106931~Ubuntu~wily Recommends linux-headers:amd64 , provided by linux-headers-4.4.0-28-generic:amd64 4.4.0-28.47
    Following dep: virtualbox-5.0:amd64 5.0.20-106931~Ubuntu~wily Recommends linux-headers:amd64 , provided by linux-headers-4.4.0-24-lowlatency:amd64 4.4.0-24.43
    Following dep: virtualbox-5.0:amd64 5.0.20-106931~Ubuntu~wily Recommends linux-headers:amd64 , provided by linux-headers-4.4.0-24-generic:amd64 4.4.0-24.43
    Following dep: virtualbox-5.0:amd64 5.0.20-106931~Ubuntu~wily Recommends linux-headers:amd64 , provided by linux-headers-4.4.0-22-lowlatency:amd64 4.4.0-22.40
    Following dep: virtualbox-5.0:amd64 5.0.20-106931~Ubuntu~wily Recommends linux-headers:amd64 , provided by linux-headers-4.4.0-22-generic:amd64 4.4.0-22.40
    Following dep: virtualbox-5.0:amd64 5.0.20-106931~Ubuntu~wily Recommends linux-headers:amd64 , provided by linux-headers-4.4.0-21-lowlatency:amd64 4.4.0-21.37
    Following dep: virtualbox-5.0:amd64 5.0.20-106931~Ubuntu~wily Recommends linux-headers:amd64 , provided by linux-headers-4.4.0-21-generic:amd64 4.4.0-21.37
    ...
    

    So it's a dependency issue through the virtual package linux-headers

I'm in doubt here, It seems to me as bug.

  • Either in autoremove function, it can't deal with virtual package dependency.
  • Or in virtualbox package, that may need another way to declare dependency to Linux headers.

Solution 2:

I generally do not trust much in automatically running system maintenance actions when it comes to important topics like kernel management. As I can see from your comments, there seems to be an interference with VirtualBox 5. So I recommend the most reliable way to clean up old kernels.

Check which kernel versions are installed : dpkg --get-selections | grep linux

To check especially for headers : dpkg --get-selections | grep linux-headers

Remove the ones you want to get rid of (replace * with the kernel version number) :

sudo apt purge linux-headers-* linux-headers-*-generic linux-image-*-generic linux-image-extra-*-generic linux-signed-image-*-generic

This is of course not an explanation what went wrong and why the automatic headers removal does not work when running the apt autoremove command, but in the end it is a solution to achieve what you want and it additionally makes sure that everything is cleaned up properly.