696

Every time I install a new Linux kernel, it gets left in the grub_config, making the boot menu longer each time.

I know I can manually search through the installed packages and remove them.

Does Ubuntu provide any easier way to clean them up or keep them from showing in the boot list?

  • 10
    There is an ubuntu command called purge-old-kernels to do the job. See my answer for more information. – jarno May 7 '15 at 11:43
  • 29
    sudo apt-get autoremove should do the trick on Ubuntu 14.04+ – hobs Dec 1 '15 at 17:17
  • 1
    The accumulation of old kernels is a bug, with fix in progress: bugs.launchpad.net/bugs/1357093 . When the fix is released, older kernels will autoremove by default. – user535733 Dec 31 '15 at 17:52
  • 1
    @hobs The command does not do the trick for me in Ubuntu Studio 14.04, if the kernels have not been installed automatically, but by Software Updater. – jarno May 27 '16 at 9:38
  • 1
    I keep returning to this page... the comment by @hobs only works if your kernels were auto-installed - however you make sudo apt-get autoremove --purge the answer by following the apt-mark advice on https://help.ubuntu.com/community/RemoveOldKernels – earcam Jul 2 at 19:32

38 Answers 38

0

Rather use apt, the cli frontend for humans, than apt-get to purge old kernel images:

$ sudo apt autoremove --purge
...
The following packages will be REMOVED:
  linux-headers-4.4.0-51* linux-headers-4.4.0-51-generic* linux-image-4.4.0-51-generic* linux-image-extra-4.4.0-51-generic*
  • What's the difference here between apt and apt-get? (Note, apt is not available on a plain 12.04 installation.) – mwfearnley Feb 24 '17 at 9:24
  • @mwfearnley For basic difference, see this answer When I was researching this, apt-get did not purge the old kernels for me. apt did, yet I also may have used them differently. – k0pernikus Mar 1 '17 at 11:56
0

I use unattended-upgrades so I really liked Qwerty's answer that configures it to uninstall old kernel packages automatically.

However there was one issue: after a new kernel is installed, this process would uninstall the package for the currently running kernel!

That doesn't break the OS entirely. In fact I rarely noticed it had happened, until I needed to hot-load a module (e.g. by plugging in a USB device) and then the module could not be found.

As a solution to this, I created a script to generate a virtual package currently-installed-kernel which effectively pins the current kernel package until the next boot.

The script is called pin-current-kernel-with-package.sh. Here is the latest version (probably) and the first version (definitely).

Just save that script somewhere, and add it to your bootup process. For example, save it in /root/bit/pin-current-kernel-with-package.sh and then edit /etc/rc.local and add the following line:

bash /root/bin/pin-current-kernel-with-package.sh

Now your running kernel package will never be uninstalled!

0

I give two safer variations of the methods in other answers: one using synaptic manager and the other using terminal.

With these variations you only remove packages that the system identifies as removable. There is no risk you remove linux kernels that are currently used—as it happened to me some weeks ago ;-(

Using Synaptic Manager

Instead of searching linux-image over all installed packages, select the autoremovable packages (See red ellipse in the picture below). Now from this list remove the linux-image you want.

enter image description here

Using terminal

Similarly, if you are removing kernels on terminal, show only the autoremovable kernels by running

 sudo apt-get -s autoremove | grep linux-image

And then remove whichever kernel linux-image-x.x.x-x-generic you want to remove from the given list by running

 sudo apt-get purge linux-image-x.x.x-x-generic 
0

To remove the kernels that were automatically installed via regular system updates, open terminal and run:

sudo apt autoremove --purge

To know your current kernel release run uname -r and also u can run uname --help to read more about all uname commands

You can also run df -h to show you file system drivers, similarly you can run df --help for more.

To enable automatic removing of old kernels: Edit the config file using gksu

gksudo gedit /etc/apt/apt.conf.d/50unattended-upgrades

When the file opens, uncomment the following line and change the value to true:

//Unattended-Upgrade::Remove-Unused-Dependencies "false";

If you need to install gksu, you can run sudo apt install gksu

To clear old packages we can use sudo apt-get clean

For More, visit http://ubuntuhandbook.org/index.php/2016/05/remove-old-kernels-ubuntu-16-04/

0

Next time, when removing old kernels open a Terminal and use this command:

sudo apt-get autoremove linux-headers-2.6.38-10-generic

You can use Synaptic to get the exact name of the kernel that you intend to delete. Just open Synaptic and search for "linux-headers" and then select which kernel entry you want to remove. The relevant entry will be tagged with "-generic" at the end.

To clear out any unused (left over) dependencies throughout the system use this command by itself:

sudo apt-get autoremove
  • dependencies are removed but I still have these files in /boot folder. – Patryk Oct 10 '11 at 13:43
  • I think you should use --purge option and purge the respective linux-image packages as well as linux-headers packages. – jarno May 16 '15 at 12:10
0

As @jarno and @earcam mentioned in their comments on the question, you first want to ensure all your linux kernel packages are marked as having been autoinstalled:

sudo apt-mark auto '^linux-.*-4\.12\.0-12(-generic)?$'

Then the normal apt-get autoremove command should work.

sudo apt-get autoremove --purge

Read the docs for more details.

-1

The following string of commands will purge any installed linux kernels except the currently running one (grep -v uname -r) and the lastest available kernel (dpkg -l | .... | tail -1):

dpkg -l | grep -E linux-image-.*-generic | cut -d ' ' -f3 | grep -v `dpkg -l | grep -E linux-image-.*-generic | cut -d ' ' -f3 | tail -1` | grep -v `uname -r` | xargs apt-get -y purge

I use this to keep desktop's boot volumes relatively clean, but in a server situation you'd probably want to expand the logic and write some additional scripting to maintain a list of the last X kernels the server has booted.

Something like a startup script that does:

uname -r >> /root/bootedkernels
cat /root/bootedkernels | sort -u | tail -3 > /root/bootedkernels # Keep the last 3 booted kernels

and then use:

dpkg -l | grep -E linux-image-.*-generic | cut -d ' ' -f3 | grep -vf /root/bootedkernels | grep -v `dpkg -l | grep -E linux-image-.*-generic | cut -d ' ' -f3 | tail -1` | grep -v `uname -r` | xargs apt-get -y purge
  • Simpler form would be sudo apt-get purge $(dpkg-query -W -f'${Package}\n' 'linux-image-[0-9]*' | head -n -1 | grep -v $(uname -r)), but even that raises questions: Can you rely on that dpkg lists newest kernel last? Can you rely on matching the current version as such regular expression? A dot in version string matches any character. What if the package name has one more character in version number than the current version? To be strict, you had better use uname -r | sed -r -e 's/\./\\./g' -e 's/.*/-&($|-)/' as the regular expression to match in the last grep command. – jarno May 16 '15 at 14:15
  • Besides, the script does not purge any linux-header packages or other related packages. – jarno May 16 '15 at 14:25
  • DO NOT USE THIS! It will remove old kernels without question. I tried without -y but then the command breaks before executing – rubo77 Jul 25 at 9:05
-3

Here's the script I use to keep 2 kernel packages installed (i.e. the current one and the previous one). It also removes the initrd-VERSION-generic.old-dkms files left around by DKMS, which can fill-up a small boot partition.

The script:

echo "**Removing -generic.old-dkms files from /boot**" && rm -f /boot/*-generic.old-dkms # if using DKMS it creates initrd-VERSION-generic.old-dkms in /boot and doesn't clean them up, meaning after a few kernel upgrades /boot can become full
OLDKERNEL=$(ls -tr /boot/vmlinuz-* | head -n -2 | cut -d- -f2- | awk '{print "linux-image-" $0}')
OLDHEADERS=$(ls -tr /boot/vmlinuz-* | head -n -2 | cut -d- -f2- | sed 's/-generic//g' | awk '{print "linux-headers-" $0}')
OLDHEADERS=${OLDHEADERS//-pae/} # remove -pae string as linux-header packages don't have it in their names
if [ -n "$OLDKERNEL" -o -n "$OLDHEADERS" ]; then
apt-get -q remove --purge $OLDKERNEL $OLDHEADERS
echo "**Finished removing old kernels**"
else
echo "**No old kernels found**"
fi

protected by Community Aug 2 '15 at 6:05

Thank you for your interest in this question. Because it has attracted low-quality or spam answers that had to be removed, posting an answer now requires 10 reputation on this site (the association bonus does not count).

Would you like to answer one of these unanswered questions instead?

Not the answer you're looking for? Browse other questions tagged or ask your own question.