Ask Ubuntu is a question and answer site for Ubuntu users and developers. It's 100% free.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I've got 200 MB assigned for the /boot partition. Everytime I try to update the kernel I get an error message that basically says the size of /boot is full.

What can I do to cleanup /boot and remote/backup the older kernels?

share|improve this question
up vote 103 down vote accepted

Graphical method:

You can use Ubuntu Tweak, it's an easy-to-use graphical tool that let you tweak ubuntu, and also, into the "Janitor" section you can clean the system, as well as remove older kernels. You can take it from here:

Command line method:

First check your kernel version, so you won't delete the in-use kernel image, running:

uname -r

Now run this command for a list of installed kernels:

sudo dpkg --list 'linux-image*'

and delete the kernels you don't want/need anymore by running this:

sudo apt-get remove linux-image-VERSION

Replace VERSION with the version of the kernel you want to remove.

When you're done removing the older kernels, you can run this to remove ever packages you won't need anymore:

sudo apt-get autoremove

And finally you can run this to update grub kernel list:

sudo update-grub
share|improve this answer
the command line worked like a charm, thanks! – koba101 Sep 15 '13 at 15:04
sudo dpkg --list 'linux-image*' | grep ^ii makes it a little easier to see just the installed kernels. Also I think the update-grub is harmless but not strictly necessary, that is run automatically when you uninstall a kernel. – Nelson Mar 5 '14 at 16:15
Use sudo dpkg --list 'linux-image*' | grep ^ii | awk '{print $2}' | sort | egrep "[0-9]-generic" | head -n -3 | tr '\n' ' '; echo "" to get the list of package names to be used with sudo apt-get remove. head -n -3 is used to keep 3 most recent kernels left in the system. – Sithsu Nov 16 '14 at 6:31
my simple one liner: apt-get remove `dpkg --list 'linux-image*' |grep ^ii | awk '{print $2}'\ | grep -v \`uname -r\`` – gcb Mar 1 '15 at 20:49
You can use awk to do one of the greps, but I can't find a way around the backticks in awk for the other one: sudo dpkg --list 'linux-image*'|awk '{ if ($1=="ii") print $2}'|grep -v `uname -r` – flickerfly Jul 15 '15 at 18:03

NOTE: this is only if you can't use apt to clean up due to a 100% full /boot

If apt-get isn't functioning because your /boot is at 100%, you'll need to clean out /boot first. This likely has caught a kernel upgrade in a partial install which means apt has pretty much froze up entirely and will keep telling you to run apt-get -f install even though that command keeps failing.

Get the list of kernel images and determine what you can do without. This command will show installed kernels except the currently running one sudo dpkg --list 'linux-image*'|awk '{ if ($1=="ii") print $2}'|grep -v `uname -r`. Note the two newest versions in the list. You don't need to worry about the running one as it isn't listed here. You can check that with uname -r.

Craft a command to delete all files in /boot for kernels that don't matter to you using brace expansion to keep you sane. Remember to exclude the current and two newest kernel images. Example: sudo rm -rf /boot/*-3.2.0-{23,45,49,51,52,53,54,55}-*.

sudo apt-get -f install to clean up what's making apt grumpy about a partial install.

If you run into an error that includes a line like "Internal Error: Could not find image (/boot/vmlinuz-3.2.0-56-generic)", then run the command sudo apt-get purge linux-image-3.2.0-56-generic (with your appropriate version).

Finally, sudo apt-get autoremove to clear out the old kernel image packages that have been orphaned by the manual boot clean.

Suggestion, run sudo apt-get update and sudo apt-get upgrade to take care of any upgrades that may have backed up while waiting for you to discover the full /boot partition.

Suggestion2, Review and consider setting Unattended-Upgrade::Remove-Unused-Dependencies to true in /etc/apt/apt.conf.d/50unattended-upgrades. This will be the equivalent of running autoremove after each security updates to be sure you clean out unused kernels but will also remove other things it thinks are unused saving you from this problem in the future.

share|improve this answer
May I ask why to exclude the last two newest kernel images? – nightire Apr 22 '14 at 2:37
This way I have the latest for the next reboot and then the one before just in case something breaks in that one. Usually I have plenty of room so it doesn't hurt to have a few and it satisfies my paranoia for not having enough backup options in any given scenario. – flickerfly Apr 22 '14 at 6:32
Great answer! Do you think it's completely safe to enable Unattended-Upgrade::Remove-Unused-Dependencies? – mkataja Apr 15 '15 at 9:50
I have never had a problem caused by unattended upgrades. I can imagine scenarios where this could be an issue mostly around dependencies being lost on non-deb packaged installs Say you install php, decide to uninstall it and install a newer version from source. This version has dependencies provided by the previous install, but apt is unaware that it is still required. Next time you run auto-remove those dependencies will be gone. If automated, this could be a bit confusing. If you don't install outside the repositories I believe it to be totally safe. – flickerfly Apr 15 '15 at 13:08
After seeing this in several vsphere virtual servers (where kernels were being automatically upgraded but not removed afterward), I wrote a python script to automate it. I'd love to have more eyes on it – EvanK Sep 29 '15 at 23:39

This one-liner from did the trick for me

dpkg --get-selections|grep 'linux-image*'|awk '{print $1}'|egrep -v "linux-image-$(uname -r)|linux-image-generic" |while read n;do apt-get -y remove $n;done

However, this only worked once! The second time I ran it, my computer no longer had a kernel (see comment below). Be careful with this command as I spent about 10 hours figuring out what happened and then replacing the kernel.

share|improve this answer
This will remove the current linux-image-extra and the version-less linux-image-server. I would suggest using dpkg --get-selections | grep 'linux-image.*-[0-9].*' | awk '{print $1}' | egrep -v "$(uname -r)" look at the list carefully and only then append | while read n; do apt-get -y remove $n; done – canaaerus Aug 3 '14 at 10:29
Thanks for your input, I wish I knew why this only worked once – Paul Gregoire Aug 3 '14 at 16:08

A very simple answer that worked for me:

sudo apt-get autoremove

I guess it will depend how you added the kernels in the first place. After this command, I was left with my 2 newest kernels, plus the initial one from when I installed my server.

Or course this command might remove other packages so do check the list it offers. Hopefully they are also packages you no longer need.

share|improve this answer
-1: autoremove won't work directly since the partition is full and this interrupted package installation. apt-get won't apply autoremove until that has been resolved. – muru Sep 9 '15 at 12:57
Well I still think this answer has it's uses for some people. The "interrupted package installation" isn't necessarily the case for everyone - the question headline is "What is the safest way to clean up /boot partition?". That's what I wanted to do, and this answer was perfect for me. The accepted answer still uses apt-get. The answer from flickerfly forces remove first. – AdamS Sep 9 '15 at 17:17
Several of the answers with grep and awk are potentially hazardous, especially the one form Paul Gregoire which seems to remove your working kernel and that one didn't even get downvoted. – AdamS Sep 9 '15 at 17:18
there is more to the question than the headline. That's why we can add a body to it, you know. The case for everyone is in another question, not this one. – muru Sep 9 '15 at 17:23
At the very least, I would suggest remove maximum one kernel using the more complex methods, without any complicated awk etc. This would unlock the interrupted state safely without risk of removing package in use. Then can use the autoremove to handle the rest. – AdamS Sep 9 '15 at 17:23

protected by Community Jan 21 at 14:21

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.

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.