Unless you have a totally fresh install of Ubuntu, you have probably noticed that each time you boot up, the GRUB boot menu lists a bunch of previous Linux kernels which you can boot into. While this can occasionally come in handy – like if you can’t boot into the new kernel after an upgrade – those previous kernels, images and modules are usually just wasting space.
While you can go into Synaptic, search for all the bits and pieces of previous kernels, and mark them for removal, here is a much easier method. In a terminal, simply paste the following command, and it will remove all but the current kernel (if you’ve upgraded your system, or had an update with a new kernel, please reboot your machine before running this):
dpkg -l 'linux-*' | sed '/^ii/!d;/'"$(uname -r | sed "s/\(.*\)-\([^0-9]\+\)/\1/")"'/d;s/^[^ ]* [^ ]* \([^ ]*\).*/\1/;/[0-9]/!d' | xargs sudo apt-get -y purge
You will see some info about what is going on:
The following packages will be REMOVED:
linux-headers-2.6.35-22* linux-headers-2.6.35-22-generic*
linux-headers-2.6.35-23* linux-headers-2.6.35-23-generic*
linux-image-2.6.32-25-generic* linux-image-2.6.35-22-generic*
linux-image-2.6.35-23-generic*
0 upgraded, 0 newly installed, 7 to remove and 13 not upgraded.
After this operation, 586MB disk space will be freed.
(Reading database … 261863 files and directories currently installed.)
Removing linux-headers-2.6.35-22-generic …
Removing linux-headers-2.6.35-22 …
Removing linux-headers-2.6.35-23-generic …
Removing linux-headers-2.6.35-23 …
Removing linux-image-2.6.32-25-generic …
It will then go on to generate a new GRUB menu, and when you reboot, you’ll see only the current kernel is listed.
[...] pm TTC Shelbyville IT Department Leave a comment Go to comments There is an excellent article on Ubuntu Genius on how to clean Ubuntu’s previous kernals. While I have been using Linux, Unix and every [...]
Use ubuntu tweak much easier way
A “much easier way” than pasting a command in a terminal and watching it do its stuff? Methinks you jest! The process can be running before Ubuntu Tweak even finishes loading, so (call me crazy but) it would seem the terminal is not only WAY easier, but much, MUCH faster. But, hey, each to their own…
PS: Besides, everyone has a terminal – not everyone has Ubuntu Tweak (I installed it, but never actually use it).
the only reason i told was for new users and ubuntu tweak does many thing than removing old kernels.
This is entirely user choice which one they feel easy
Hey, no worries, it’s just that you DID use the words “much easier way”, rather than something like “For those scared of the terminal (or just long, strange commands), Ubuntu Tweak can do it too, and is useful in many others ways besides”. I’ll actually be doing an article on it later. I’ve been installing it since the beginning, and it has come a long way (only used to have a handful of features). You actually made me contemplate adding the U.T. method to the article, but will do a separate one and link them together. Thanks once again for your valued input!
so what do I do If I keep getting an unmet dependency error after pasting that command to the terminal? The error I get is
“The following packages have unmet dependencies:
linux-image-server : Depends: linux-image-3.0.0-24-server but it is not going to be installed
E: Unmet dependencies. Try ‘apt-get -f install’ with no packages (or specify a solution).
“
OK, obviously something went amiss with that kernel install, and I’m hoping that was a previous one. I would try the following command to make sure you have the latest kernel, and it should get rid of some earlier ones, like the broken one: sudo aptitude safe-upgrade (or sudo apt-get dist-upgrade).
I had the same thing and then ran aptitude safe-upgrade and It runs for awhile until it errors gzip: stdout: no space left on device
I need to run this command to fix 100% used /boot partition (why oh why do they make this partition so tiny!).
But running it complains about linux-headers-3.2.0-37-generic
Sometimes people do have problems with specific kernels – I know I’ve had a couple – so it may pay to Google the specific error message along with the kernel version, or better yet start a thread at the Ubuntu Forums. That way if people have encountered the same issue, and found the answer, they’ll generally take the time to fill you in on what to do. That has saved me a lot of headaches in the past.
Thanks! Just what I was looking for!
I have this saved in my bookmarks and use it all the time. Would love to know more about terminal language.
+1 to raised eyebrow comment about it being “much easier” than copying & pasting.
Wha?!
hi there !
thank you,
i just want to know if i can remove only ‘kernel headers’ and leave ‘linux-images’ ?
please help :-)
Not sure why you’d want to keep those after getting rid of the rest, but this should work:
dpkg -l ‘linux-*’ | sed ‘/^ii/!d;/’”$(uname -r | sed “s/\(.*\)-\([^0-9]\+\)/\1/”)”‘/d;s/^[^ ]* [^ ]* \([^ ]*\).*/\1/;/[0-9]/!d’ | grep “linux-headers” | xargs sudo apt-get -y purge
WordPress will probably autoformat some of that, so replace any single and double “smart” quotes (that are angled instead of vertical).
Yeah, overtype the curved and/or angled quotes or the command is useless. Sorry I haven’t got time to replace them all with code that will force them to stay as they should be, but as I said just select any single-quote and replace it with a fresh one, and some for double-quotes, THEN paste it in the terminal. Cheers.
Works great! Thanks!
I removed the header and linux-images but still I get the old entries in the list, although they start up nothing when selected and I fall back into grub. Can someone help me to remove these entries!
(Sorry for the late reply): OK, depends on whether you are using GRUB2, or still using legacy GRUB. You should find plenty of user guides (including the official ones) for both versions, and that should be one of the topics covered. In the original GRUB, it was as easy as editing a text file, but GRUB2 now stores all the info in various files, and most can’t just be simply edited like before.
Care to explain the regex for us students?
Explain all of THAT, hehe? I’d rather not (that is one long expression). Needless to say it works, which is the important part, hehe. Have a great New Year. Cheers.
This guy found this site and explained how it works…
http://markmcb.com/2013/02/04/cleanup-unused-linux-kernels-in-ubuntu/
Excellent site, have shared some information about your Newbies for my students.
Steve at TTC Shelbyville
Thanks!
bravo!
Worked perfectly, liberating a total of almost 700MB!!
Which I really needed, by the way, like the deserts miss the rain.
Thanks genius!
Never run this if you haven’t reboot the machine first. The script will delete the newer kernels installed since the last reboot (because of the uname -r).
Thanks, but I think most people would reboot after an upgrade or update, which is why everyone is happily reporting success with this. But thanks for pointing that out anyway.
[...] Para hacer esto podéis utilizar ubuntu tweak por ejemplo, pero me ha entrado la curiosidad sobre cómo hacerlo por consola para tener un pequeño script que lo haga, y buscando he encontrado esta web http://ubuntugenius.wordpress.com/2011/01/08/ubuntu-cleanup-how-to-remove-all-unused-linux-kernel-he… [...]
tis broken:
dpkg-query: warning: parsing file ‘/var/lib/dpkg/available’ near line 81448 package ‘virtualbox-3.1′:
error in Version string ’3.1.2-56127_Ubuntu_hardy’: invalid character in revision number
Sorry, not sure what is going on there, but hopefully someone seeing this can help. It seems to be an issue with Virtualbox.
Is it possible to change it to keep not only the one in use but also the last one. I always keep two kernel on the system just for safe :)
Thanks!
May I simply say what a relief to discover a person that really understands what they are talking about on the internet. You certainly know how to bring a problem to light and make it important. A lot more people ought to look at this and understand this side of your story. I can’t believe you aren’t more popular since you most certainly have the gift.
Thanks mate! I do try to make everything concise and easy to follow. Cheers
Thanks duuuuuuuuuudee…. it freed me 700 mbs :)
Now I appreciate how simple this solution is… And I’m quite happy that it will work… But it scares the hell out of me!
Your “simple” solution uses a very complicated sed command to remove EVERY kernel you have apart from the current one.
I would like to see what it’s going to delete first and be able to save one or two old kernels for safety. I’m going to have to decode what it’s doing… but when I’ve got it, I’ll post my newer version ;-)
Hi. Yes, keeping a previous kernel as a safety net can be a good thing, but this article was working on the basis that one’s current kernel is working without issue – whenever I’ve needed to switch back to a previous kernel, it’s always been either straight after an Ubuntu upgrade, or a kernel upgrade. So for most of us, getting rid of all previous kernels is fine, and frees up even more space. (And I had to pick whether this article was going to be “… Remove All…” or “… Remove Nearly All…”, hehe…). Thanks for your input, and have a great day!
Ok I’ve had a proper think about this, and I think my favourite improvement would simply be to split the command and output the first part to a file, have a look at it, deleting lines that worry you then feed the result into apt-get.
dpkg -l ‘linux-*’ | sed ‘/^ii/!d;/’”$(uname -r | sed “s/\(.*\)-\([^0-9]\+\)/\1/”)”‘/d;s/^[^ ]* [^ ]* \([^ ]*\).*/\1/;/[0-9]/!d’ >/tmp/file
vi /tmp/file
cat /tmp/file | xargs sudo apt-get -y purge
I’d have started with awk instead of sed, but it’s not really so complicated after all :-P
U.G. thanks for this. Tim Bradshaw, thanks for sharing your version. I had the same concerns. All in all, great work you both.
Awesome. Thanks.
Very helpful. For the more cautious who have experienced kernel issues and want to ensure at least one or two prior versions are available for fallback.
Overall, great blog post and extremely helpful information!
Oh this is sweet and the scary thing is l understand it ;op thanks for a great article :-D
Great Work…….
Reblogged this on Places of Interest to Ray and commented:
This should come in handy for netbook users that have smallish (4G) primary drives. We’re always running out of space during version upgrades.
Thanks a lot, since Ubuntu 12.04 does not have synaptic app, your method is very handy and easy.
It is important to know that the above command removes VMware tools as well, if you are running your Linux box as a virtual machine. I highly recommend creating a snapshot of your VM before running this command so you can rollback if required.
Thanks a bunch! it worked for me too…
Thanks, my bootscreen is full, but 3 are needed, I have 3 OS’s , a 10.04 and XP on internal drive, and a 10.10 on usb drive. Am I right in thinking this method will mess me up?
If you mean you’re multi-booting those OSes on the same machine, then it still should be fine. Removing Linux kernels (and their headers) does just that – this guide is not about removing entries from your GRUB boot menu, but about removing old Linux kernels (which then automatically disappear from GRUB, since they’re no longer there). So the only part of the boot process it affects (or choices at boot, rather) is the inability to boot into Ubuntu using earlier kernels, which most of us never do anyway.
THANK YOU! big in capitals….Ari Torres (kuvanito)
great job!!!
[...] Source Like this:LikeBe the first to like this. [...]
Absolutely elegant! (And I thought I deleted all those old kernels)
Another way to do this same thing is to run the following command.
sudo apt-get remove $(dpkg -l|egrep ‘^ii linux-(im|he)’|awk ‘{print $2}’|grep -v `uname -r | cut -d- -f1,2`)
Doing it this way will allow you to review the files to be deleted before deleting them. If you decide you want to delete the files then just hit the ‘Enter’ key and presto, no more old kernels. Instead of trying to remember the command just throw it in a bash script, run ‘chmod +x myscriptsname.sh’ and from now on just run that script.
Ubuntu Genius, I just ran that command and it did it’s thing. But my /boot is now only showing “grub lost+found memtest86+.bin” … everything else that was in there is gone. Is this normal? I still want to apply this to a few more of my servers but I’m afraid to reboot this one first. Thanks in advance.
Hmmm… I just ran this again myself, as I had 6 previous kernels to remove and reclaim 1.2Gb, and my /boot folder has the one sub-folder grub (I don’t have a lost+found in there) and the following files:
/boot/abi-3.2.0-31-generic
/boot/config-3.2.0-31-generic
/boot/initrd.img-3.2.0-31-generic
/boot/memtest86+.bin
/boot/memtest86+_multiboot.bin
/boot/System.map-3.2.0-31-generic
/boot/vmlinuz-3.2.0-31-generic
I’d make sure you have a kernel and related packages installed before rebooting (https://help.ubuntu.com/community/Kernel/Upgrade?action=show&redirect=UpgradeKernel). Not sure what went wrong there – only thing I can think of is what I’ve cautioned against, being that a new kernel and images was installed, and the system hadn’t been rebooted yet. One of the last things you should have seen in the terminal was something like:
Generating grub.cfg …
using custom appearance settings
Found background image: /home/ozzman/Themes/Skulls-Usplash-ss.jpg
Found linux image: /boot/vmlinuz-3.2.0-31-generic
Found initrd image: /boot/initrd.img-3.2.0-31-generic
Found linux image: /boot/vmlinuz-3.2.0-31-generic
Found initrd image: /boot/initrd.img-3.2.0-31-generic
Found memtest86+ image: /boot/memtest86+.bin
Found Windows 7 (loader) on /dev/sda1
Found Windows 7 (loader) on /dev/sda1
done
… but I’m guessing you didn’t, which is why you looked in the /boot folder in the first place. You should be able to install the latest kernel and dependencies without issue though.
I had rebooted… but I think there was problems with the kernel update because of space on my boot partition… hence why I was trying to free space and found this. I used synaptic to install the latest kernel and now there are 6 more items… generally the same items you listed except for 2.6. So I feel much safer rebooting now. Very glad I asked, I knew that something wasn’t right with that… So I should be cool to reboot now?
Thanks for the quick response, you rock.
Ah, yeah I wouldn’t be surprised if it was a failed kernel update, especially if you had free space issues. All should be fine, especially if looking in Synaptic shows you the kernels/images/dependencies are listed as being properly installed. To be certain, in the terminal run uname -r, and it will list the current kernel version (I’m gathering only properly installed ones will be listed). Also, since you say you already had rebooted, and I assume you returned to Ubuntu without issues, all should be safe then to reboot.
The one listed with uname -r isn’t the one shown in synaptic… is that because I haven’t rebooted? It’s showing 2.6.32-314-ec2 but synaptic shows 2.6.31-11.154 … it only shows me two available images and both are 2.6.31 not 2.6.32…
OK, I’m not exactly sure – except that (I assume) Synaptic would list the new kernel even if it’s not currently being used as you haven’t rebooted. Actually, from memory when trying to get rid of a latest kernel that gave me problems, Synaptic listed all those installed. Run dpkg –list | grep linux-image and see what that says (actually, I just did that on my system and it showed me the previous 12 kernels, though I’m certain they’re not installed). However, uname -a should shed more light on which is correctly installed (on my system it shows the time on Sept 7 when it was installed).
You can reinstall it all with these 4 commands (replacing *** with the correct numbers of the specific kernel you are trying to install; note that since you’re using an earlier 2.6 version of the kernel, you might not need the 4th command, as that is a recent addition for the newer 3.x kernels):
sudo apt-get install linux-headers-***_all
sudo apt-get install linux-headers-***_i386 (or amd64 instead of last 4 digits if 64-bit)
sudo apt-get install linux-image-***_i386 (or amd64 instead of last 4 digits if 64-bit)
sudo apt-get install linux-image-extra-***_i386 (or amd64 instead of last 4 digits if 64-bit)
Once they’re installed, then run sudo update-grub to update your GRUB bootloader menu.
I’m going to have to head off for the day, so check out http://ubuntuforums.org/showthread.php?t=1872537 for some more info.
Oh, and also don’t forget to run sudo update-grub after the manual install of the kernel, if doing that! I’ll edit my last reply to include that, and then delete this one later.
Thanks for the help.. I’ll update this thread after I’ve rebooted. :)
For safety reasons, I’d like to keep the current kernel and the one before that. Is there a way to modify the command to do that?
I found I can keep the previous version quite easily. Using the original command line at the top of this thread, if you don’t have the latests update then it’ll download that but also keep your current version.
Never mind, I figured it out by using Tim Bradshaw’s version. Thanks for this post.
[...] can’t actually claim ownership of this one – this sed monstrosity was actually made by the folks at Ubuntu Genius blog. In fact, they went one step beyond showing you how to automatically purge these things in a single [...]
This is great! I think I’m going to run this automatically when ever my /boot gets close to full! Thanks
Just remember that if you’ve updated the kernel, reboot first! But yeah, it’s a good way to clear up a few megabytes, even over 1Gb at times.
[...] . As most of these were of the linux-header or linux-image type, I used a wonderful command I found here which finds all of the available header files and eliminates them all, other than the one currently [...]
thanks for saving the day :)
Wow am I the only person this did not work for. I’m a newbie to Ubuntu and after running the command my /boot ran out of space while trying to free up space now I’m at 0% left vs the little 6.9M I had prior to trying to get fix my initial problem lol. Any assistance would be greatly appreciated.
Thank you, Ubuntu Genius.
Worked like a charm. Thanks!
[...] 1.2GB of linux headers of all previous kernels that were installed. Then I came across this post by where he gave this nice one line [...]
[...] Quelle: http://ubuntugenius.wordpress.com/2011/01/08/ubuntu-cleanup-how-to-remove-all-unused-linux-kernel-he… [...]
You can also try this: http://www.kubuntuforums.net/showthread.php?58010-Linux-kernels-in-Precise&p=292536&viewfull=1#post292536
Make a mykernels.sh executable script with this in it:
#!/bin/sh
clear && dpkg –list | grep linux-image | awk ‘{print $2}’ && cat ~/removeKernel &
a mykernel2rm.sh executable script with this in it:
#!/bin/sh
echo ‘sudo apt-get purge linux-image-’$1
sudo apt-get purge linux-image-$1
sudo update-grub2
and a simple text file removeKernel in your Home directory with this:
=================================================
Removing older kernels from the console:
dpkg –list | grep linux-image | awk ‘{print $2}’
sudo apt-get purge linux-image-x.x.x-xx-generic
sudo update-grub2
Use the first script to list the installed kernels.
Use the second script with the Kernel you want to remove:
mykernel2rm 3.2.0-19-generic
That’s all!
Can i run this command using ssh ?
Hmm, not sure – my network skills are pretty much zilch. I’m guessing if you can run other commands over the network, then you should be able to, but then you’ll know once you give it a shot. Sorry I couldn’t be of more help.
Many thanks for assisting in removing dead-files from my 100% /boot :)
What a fab piece of cmd-line :)
Happy 2013 UG!!
[...] This was stole directly from here. [...]
[...] http://ubuntugenius.wordpress.com/2011/01/08/ubuntu-cleanup-how-to-remove-all-unused-linux-kernel-he… [...]
[...] Ubuntu Cleanup: How to Remove All Unused Linux Kernel Headers, Images and Modules [...]
After struggling for two days to free up some space in my OS partition (still have to make it bigger, but am an utter noobie), following several system boot fails/stalls (worked around it by getting into terminal by ctrl+alt+F1, then sudo service gdm start *whew), failing to get Ubuntu Tweak to not crash (5x) while it scanned for old kernals, and losing a tonne of hair… I found HERE & UG!, and thanks to this brilliant bit of code and decent instructions, I did as bade and freed up over 3gigs of space on my meagre 6gig partition!
It was a bit nerve wracking watching the removals scroll by and get close to my latest version (3.2.0-37/xubuntu 12.04.2), but low and behold, it worked like a charm! I just did this a minute ago so haven’t tried to re-boot yet (crossing fingures), all I can say it that you frikken ROCK! (and I really am a total linux terminal noob, well not so much now) :))))))) < me being very, very happy!
Thank You!
It worked! System is up and running again. It has been quite a learning curve and I’ve just lost my first (long) reply which detailed my floundering/crashing and bashing around, oh well, another time.
Note: After I was able to get into my desktop I used Synaptic to free up enough room to update/upgrade using my terminal and ‘sudo’ commands.
Two ways I found to shutdown/reboot (other than using my applications menu),
In termial> ‘sudo bash’ -root command ‘shutdown -h now’ for shutdown, and ‘shutdown -r now’ for reboot.
In places I was stuck in terminal> Alt-SysRq and slowly type R-E-I-S-U-B for reboot, and change the B for an O for shutdown, came in handy.
My DL version of Ubuntu Tweak is getting junked, it kept crashing when listing Old Kernals, UG’s code is faster and easier once your OS is updated and rebooted, does what it should.
Thanks again UbuntuGenius
[...] quick Google search found Ubuntu Cleanup: How to Remove All Unused Linux Kernel Headers, Images and Modules. The solution on the page had exactly what I’m looking for, however, I couldn’t take it [...]
If you’re looking for a step by step explanation of this, I just wrote one up here: http://markmcb.com/2013/02/04/cleanup-unused-linux-kernels-in-ubuntu/
Indeed your blog deserves the genius part. I’ve been messing with apt-get commands that scrape a couple 100MB which is lost days later. But this saved me 2.2GB. Now we’re frying big fish. Thank you.
Dude! Thank you so much. Like the previous commenter, I’ve been shuffling stuff, rearranging partitions and mounts but too scared to touch the OS partition in case I screwed it up. With this command I just freed up 1.3GB. – enough to avoid problems for the foreseeable future.
Fantastic! This saved me a lot of time and headaches freeing up the boot device. Thanks :)
This seems to be processing but I’m CONSTANTLY getting prompts like this:
A new version of /boot/grub/menu.lst is available, but the version installed currently has been locally modified.
I’ve been selecting keep the local version currently installed. Hope this doesn’t screw anything up.
Great article, it certainly fixed things for me :) I was afraid of manually expanding /boot, or, worse, deleting files without knowing if “something” (in this case, the whole packaging system) was tracking them, and would throw an error afterwards…
I wonder why Ubuntu insists in keeping around so many versions anyway. I’d say 5 or so would be more than adequate…
I would say it’s not so much that Ubuntu keeps the old kernels, it just doesn’t get rid of them. I’ve been using Ubuntu since 2006, and I’m somewhat certain that there HAS been some housecleaning over the years, as I’m pretty sure I’ve seen some versions disappear from the boot menu before I started do it manually myself… but don’t quote me, hehe.
[...] (Seen from http://ubuntugenius.wordpress.com/2011/01/08/ubuntu-cleanup-how-to-remove-all… [...]
I’m not that much of a online reader to be honest but
your blogs really nice, keep it up! I’ll go ahead and bookmark your website to come back in the future. Many thanks
Thank you!
This articel has been really helpful to me!
Keep up the good work :)
Thanks a lot! This should be installed by default in Ubuntu.
What a waste of space!
Thank you so much!!! I tries do many ways of cleaning up the boot partition and this one worked perfectly! God bless your soul!
[…] 1.2GB of linux headers of all previous kernels that were installed. Then I came across this post by Ubuntu Genius where he gave this nice one line […]
Heya i’m for the primary time here. I found this board and I in finding It really useful & it helped me out a lot. I am hoping to present something back and help others such as you helped me.