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.
☻☻☻☻☻☻☻☻☻☻☻☻☻☻☻☻☻☻☻☻☻☻☻☻☻☻☻☻☻☻☻☻☻☻☻☻☻☻☻☻☻☻☻
Did this information make your day? Did it rescue you from hours of headache? Then please consider making a donation via PayPal, to buy me a donut, beer, or some fish’n’chips for my time and effort! Many thanks!
[…] 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).
Or you may be looking for some way to automate kernel clean-up on a server.
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 https://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: https://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. […]
[…] https://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 https://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.
This did not work on ubuntu 13.04, any idea why?
Not sure without more info, like whether you got an error message or not. All I can say is if it is a fresh install, then you won’t have any previous kernels etc to remove; if you upgraded, they may have been removed already (I upgraded yesterday and noticed most if not all previous kernels were marked for removal).
Thanks for the reply. I upgraded from ubuntu 12.04. I get no error messages when I run your code, however I get the following error message when I run software updater:
The upgrade needs a total of 40,4 M free space on disk ‘/boot’. Please free at least an additional 6.284 k of disk space on ‘/boot’. Empty your trash and remove temporary packages of former installations using ‘sudo apt-get clean’.
Not sure what I can offer here, but seems your drive/partition is pretty much totally full, and I suspect you have no previous kernels to delete to free up some space. All I can suggest is follow the suggestion (sudo apt-get clean) and see if that helps. If there is anything else you can delete, do that too, and perhaps resize the partition with GParted Partition Editor, as it obviously needs more room to breathe. Obviously, if you’re being told you need to free up at least another 6Mb, things are pretty dire, and your /boot partition is way too small.
Fantastic goods from you, man. I have understand your stuff previous to and you’re just too magnificent. I actually like what you have acquired here, really like what you’re saying and
the way in which you say it. You make it entertaining and
you still care for to keep it sensible. I can’t wait to read far more from you. This is really a tremendous web site.
[…] Setelah Anda melakukan update kernel di Ubuntu, seringkali masih tersimpan kernel yang lama. Saat booting, akan banyak pilihan kernel, di ‘previous kernel’ bahkan menghabiskan kuota harddisk Anda, lumayan sih. Nah, sebenarnya mudah sekali membersihkan kernel tersebut melalui artikel ini. […]
Hi there,
I have been using your great code for a while without problems but after I uograded from 12.04 to 13.04 it is leaving behind some old kernels. I get…
The following packages will be REMOVED
linux-headers-3.8.0-22* linux-headers-3.8.0-22-generic* linux-image-3.8.0-22-generic* linux-image-extra-3.8.0-22-generic*
0 upgraded, 0 newly installed, 4 to remove and 0 not upgraded.
After this operation, 235 MB disk space will be freed.
Which is great but then I get…
Generating grub.cfg …
Found linux image: /boot/vmlinuz-3.8.0-23-generic
Found initrd image: /boot/initrd.img-3.8.0-23-generic
Found linux image: /boot/vmlinuz-3.5.0-28-generic
Found initrd image: /boot/initrd.img-3.5.0-28-generic
Found linux image: /boot/vmlinuz-3.5.0-27-generic
Found initrd image: /boot/initrd.img-3.5.0-27-generic
Found linux image: /boot/vmlinuz-3.5.0-23-generic
Found initrd image: /boot/initrd.img-3.5.0-23-generic
Found memtest86+ image: /boot/memtest86+.bin
done
Is there a way I can get rid of the 3.5.xxx family?
Jon
In Synaptic Package Manager, you can paste 3.5.0-2 into the search box and all related packages will be listed, which you can then mark for removal. Strange you’ve still got those 3.5x ones sitting there though! Probably easier than typing in the command, which you can find info on here: https://help.ubuntu.com/community/Lubuntu/Documentation/RemoveOldKernels (I see they’ve now added the command for removing all previous kernels to the bottom of the guide too).
Thanks for the response but I’m still a little stumped. The package manager shows nothing with that search and the command doesn’t find them either. I guess that references to these have been removed but the files remain. Would it be safe to just delete them?
Are you sure the files are still on your system? Or is it that GRUB still lists them? Strange either way, as if they are there, you should be able to uninstall them, and if they’re not there, they shouldn’t be in the GRUB menu, as it does a pretty good job of updating itself when forced to rebuild itself after updates. I wouldn’t go manually deleting the files. Probably best to sudo apt-get autoremove, as that should get rid of unneeded packages, and if that doesn’t do it, then I’d try sudo apt-get -f install to “fix broken packages”.
Note, the current regex supplied above will attempt to remove libc (linux-libc-dev:amd64). Not a good idea. Run it without the apt-get predicate first to be sure about what you’re removing.
See http://askubuntu.com/q/2793/79981 for a list of many ways to do this…
dear god this is hideous. here’s a better replacement:
aptitude purge “~i linux-(headers|image) ?not(`uname -r`)”
I decided I wanted to see the list of kernels about to be deleted, and choose which ones I wanted to go (because I prefer leaving at least two working kernels on my system).
Using zenity:
dpkg -l ‘linux-image*-generic*’ | sed ‘/^ii/!d;/'”$(uname -r | sed “s/\(.*\)-\([^0-9]\+\)/\1/”)”‘/d;s/^[^ ]* [^ ]* \([^ ]*\).*/\1/;/[0-9]/!d;s/-generic.*//;s/linux-image-//’ | zenity –title ‘choose kernels to remove’ –list –column=file –multiple –hide-header –separator ‘\n’ | sed ‘s/^/linux-\*/;s/$/\*/;’ | xargs dpkg -l | sed ‘s/^[^ ]* [^ ]* \([^ ]*\).*/\1/;/[0-9]/!d’ | xargs sudo apt-get purge
My system is running Ubuntu 12 LTS. Current kernel is 3.2.0.40. The /boot partition is full, filled with kernels and related files going back to 3.2.0.30. When I run the command you list at the top of this article to clean out the old kernels, I get this:
You might want to run ‘apt-get -f install’ to correct these:
The following packages have unmet dependencies:
linux-generic-pae : Depends: linux-headers-generic-pae (= 3.2.0.41.49) but 3.2.0.48.58 is to be installed
linux-headers-3.2.0-40-generic : Depends: linux-headers-3.2.0-40 but it is not going to be installed
linux-headers-3.2.0-40-generic-pae : Depends: linux-headers-3.2.0-40 but it is not going to be installed
linux-headers-generic : Depends: linux-headers-3.2.0-41-generic but it is not going to be installed
linux-headers-generic-pae : Depends: linux-headers-3.2.0-48-generic-pae but it is not going to be installed
linux-image-generic-pae : Depends: linux-image-3.2.0-41-generic-pae but it is not going to be installed
E: Unmet dependencies. Try ‘apt-get -f install’ with no packages (or specify a solution).
All attempts to use “apt-get -f install” to fix the listed dependency problems fail consistently with a “disk full” error (the very problem I’m trying to solve).
I’m stuck in a loop here.
Any suggestions on how to free space in /boot given the above would be greatly appreciated. For example, is there any way to simply “rm” the files in /boot that I don’t need?
Thanks
BDwernychuk
While there are various suggestions for things like this in the other comments (as in command-line options), have you tried seeing if you can remove them one at a time via Synaptic or Ubuntu Software Center (I prefer to use Synaptic)? I found that the easiest solution for when I would occasionally go in to remove the oldest one or two kernels (these days I just periodically run the command to remove all but the latest, since if I haven’t had any trouble with the latest kernel in a week or so, it’s doubtful I would suddenly start doing so).
…how is that done for ssh prompt with no gui (aka for a remote server)? I think both Synaptic and Ubuntu software Center are local console gui apps?
Sorry, but you’re the ssh expert out of the 2 of us! But you don’t need either of the 2 apps mentioned, as the command is run through the terminal of course, so you should be able to figure out how to do it remotely. I unfortunately have no skills in that area.
This could be due to your filesystem, which is already out of inodes (have a look at ‘df -i’). In that case, delete some of the obsolete directories from /usr/src by hand first before running ‘apt-get -f install’ (which would fail previously) and then, afterwards, run those commands from Ubuntu Genius.
ths my frien 480 mb free nou :)
Heya i’m for the first time here. I came across this board and I find It really useful & it helped me out a lot. I’m hoping to present one thing again and help others such as you aided me.
Thanks dude! Simple, easy, and fast! By the way, I was thinking if it’s safe to remove old kernels. Thank you for the clarification about what’s the point of keeping old kernels.
[…] 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[1] […]
[…] caut prin linkurile salvate hăt prin toate browserele folosite în cursul timpului și am dat de un link (de prin 2011, probabil ultima dată când m-am confruntat cu o problemă asemănătoare) care […]
[…] via Ubuntu Cleanup: How to Remove All Unused Linux Kernel Headers, Images and Modules | Ubuntu Genius…. […]
[…] https://ubuntugenius.wordpress.com/2011/01/08/ubuntu-cleanup-how-to-remove-all-unused-linux-kernel-he… […]
Hi,
Just an extra little check, that caused a little problem for me.
Make sure there is nothing waiting to be ‘deinstalled’, before running this.
I had the package ‘linux-backports-modules-hv-3.2.0-48-virtual’ in deinstalled state for some reason.
So after removing the old kernels, I noticed it was still around (as the command above doesnt find deinstalled packages), I did a purge of it, and it then listed up lots of errors, and tried to create a “/boot/initrd.img-3.2.0-48-virtual” file.
So, make sure you have no deinstalled packages around, and purge them BEFORE removing any kernels, or it could give you problems purging them afterwards.
Use this to find them: dpkg –get-selections | grep deinstall
And then to remove them : apt-get purge
HTHs.
Many thanks for this additional info!
Thanks, i got about 6gb back on my hard drive, i had no idea old linux kernel headers remain on the system
Wow, that’s a bit more wasted space than the usual 600Mb-1Gb! Glad you found this useful!
[…] From this post: […]
thank you! now i have 2.3gb free. befere was 570mb
[…] regulären Ausdruck, der alle relevanten Pakete aus der Paketliste heraus filtert habe ich bei Ubuntugenius […]
This is just great! It free > 5G of my diskspace, but more important was that I was able to save ~70% of my inodes… what was really pain in the ass. Thank you
[…] Credit goes to:https://ubuntugenius.wordpress.com/2011/01/08/ubuntu-cleanup-how-to-remove-all-unused-linux-kernel-he… […]
Thank you!
The solution really helped me a lot and made me think about all the things this universe contains and we don’t know.
Right now, I am struggling with another problem related with my MongoDB installation in Ubuntu. I am surfing the web and looking for more smart solutions like you give in this blog.
Thanks again. Go ahead, Go Ubuntu. :)
Greetings from Medellin, Colombia.
[…] Источники: Can’t upgrade due to low disk space on /boot Ubuntu Cleanup: How to Remove All Unused Linux Kernel Headers, Images and Modules […]
Worked great (on my old HP laptop Ubuntu 14.04)!
Thank you Ubuntu Genius! :-)
First of all thanks for the useful code. I get the following error for each previous kernel versions ( about 10 of them) after remove process starts:
Removing linux-headers-3.2.0-51-generic …
dpkg: warning: while removing linux-headers-3.2.0-51-generic, directory ‘/lib/modules/3.2.0-51-generic’ not empty so not removed
Any idea why this happens?
Thanks
Hi. There can be a number of reasons for this – a common one seems to be being really low on disk space. If that’s the case, try again after clearing some room. Also see if the following commands help:
sudo apt-get clean
sudo apt-get autoremove
[…] I think the dpkg command came from here originally : https://ubuntugenius.wordpress.com/2011/01/08/ubuntu-cleanup-how-to-remove-all-unused-linux-kernel-he… […]
Awesome! Its truly remarkable piece of writing, I have got much clear idea concerning from this paragraph.
[…] File Name : Ubuntu cleanup: how to remove all unused linux kernel Source : ubuntugenius.wordpress.com Download : Ubuntu cleanup: how to remove all unused linux kernel […]
Perfect, Thanks!
Got this Post bookmarked since i need to clean the space for updates ~quarterly.
[…] Ubuntu Cleanup: How to Remove All Unused Linux Kernel … – Jan 08, 2011 · 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 …… […]
[…] en profondeur votre système en supprimant tous les anciens kernels installés (anciennes versions du système de base), ce que je ne vous recommande cependant […]
Thanks for the solution, it helped to a certain point, but I still get almost the same error message. The only difference is that it is asking me to open up less space. What could have gone wrong?
What’s the error message? Or are you saying it worked fine, but your system is still asking you to free up some space?
Thanks for the quick response. Yes, the system is asking me to free up some more space.
The error message is;
“Not enough free disk space
The upgrade has aborted. The upgrade needs a total of 2,346 M free space on disk ‘/’. Please free at least an additional 583 M of disk space on ‘/’. Empty your trash and remove temporary packages of former installations using ‘sudo apt-get clean’.”
I have already emptied the trash and ran the command line that you provided.
When you bring up the Properties for the drive, what amount is listed as being free? If it’s pretty much close to what the error says, then you really just need to delete or move some files, like large video files. Also see if sudo apt-get clean can clear some more space for you.
When there isn’t enough space the sudo apt-get autoremove (or clean) doesn’t work. I’ve found once you free enough space then it’ll work. reboot. Use uname -a to determine current kernel. cd /boot . Use rm to delete some of the older vmlinux-xxxxx files (2-4 and not the currently loading one). Rerun sudo apt-get autoremove (or clean). once apt-get is working again then go thru and update / reboot / clean again.
Thanks for the solutions. I had to increase the size of the boot partition and it worked. For whatever reason, I couldn’t remove enough files to make some space.
[…] File Name : Ubuntu cleanup: how to remove all unused linux kernel Source : ubuntugenius.wordpress.com Download : Ubuntu cleanup: how to remove all unused linux kernel […]
[…] File Name : Ubuntu cleanup: how to remove all unused linux kernel Source : ubuntugenius.wordpress.com Download : Ubuntu cleanup: how to remove all unused linux kernel […]
[…] File Name : Ubuntu cleanup: how to remove all unused linux kernel Source : ubuntugenius.wordpress.com Download : Ubuntu cleanup: how to remove all unused linux kernel […]
[…] File Name : Ubuntu cleanup: how to remove all unused linux kernel Source : ubuntugenius.wordpress.com Download : Ubuntu cleanup: how to remove all unused linux kernel […]
[…] File Name : Ubuntu cleanup: how to remove all unused linux kernel Source : ubuntugenius.wordpress.com Download : Ubuntu cleanup: how to remove all unused linux kernel […]
[…] File Name : Ubuntu cleanup: how to remove all unused linux kernel Source : ubuntugenius.wordpress.com Download : Ubuntu cleanup: how to remove all unused linux kernel […]
[…] Thank you for your detailed post of your problem, this got me going in the right direction. Although it is useful to keep previous kernel files you can remove all of them in one go, check this post: How to Remove All Unused Linux Kernel Headers, Images and Modules […]
[…] How to Remove All Unused Linux Kernel Headers, Images and Modules. January 8, 2011 by Ubuntu Genius Download Ubuntu Cleanup: How to Remove All Unused Linux Kernel … | […]
[…] How to Remove All Unused Linux Kernel Headers, Images and Modules. January 8, 2011 by Ubuntu Genius Download Ubuntu Cleanup: How to Remove All Unused Linux Kernel … | […]
I am trying to install ubantu os in my computer.. i have 3 hard disk partation in c drive already install windows 7 and in e drive i have personal data.. and my e drive is completely empty which are 317 gb but when i install ubantu 10.0 its show me there are no disk space 2.6 gb at least requaire.. and i have already 317 gb empty partation … in bios my hdd is also seen…
Sorry, but I couldn’t tell you without a LOT more info, but make sure you are trying to install to the correct partition for a start. Ubuntu doesn’t have any issues with any kind of drive or partition, so as long as you point in the right place, all should be fine (especially since it will totally wipe a drive/partition and format it to EXT4 – it should care what’s on a drive or not). Your best bet is to go to the Ubuntu Forums, which is a HUGE community, join up, then start a new thread, as you’re bound to get answers there. Just make sure you give every bit of info you can, which saves people asking you stuff instead of giving answers, and of course try to make the title of the thread as descriptive as possible to draw people in. In other words, rather than something lame like “Can’t Install Ubuntu”, make it more like “Ubuntu Installer Says No Disk Space on Empty 317Gb Partition”. Oh, and if by “10.0” you meant 10.04, or even 10.10, then I really suggest you throw that old version away and get either the latest distribution release 14.10, or to save upgrading till 2019 get the LTS release 14.04. Cheers
[…] How to Remove All Unused Linux Kernel Headers, Images and Modules. January 8, 2011 by Ubuntu Genius Download Ubuntu Cleanup: How to Remove All Unused Linux Kernel … | […]
[…] How to Remove All Unused Linux Kernel Headers, Images and Modules. January 8, 2011 by Ubuntu Genius Download Ubuntu Cleanup: How to Remove All Unused Linux Kernel … | […]
[…] How to Remove All Unused Linux Kernel Headers, Images and Modules. January 8, 2011 by Ubuntu Genius Download Ubuntu Cleanup: How to Remove All Unused Linux Kernel … | […]
[…] How to Remove All Unused Linux Kernel Headers, Images and Modules. January 8, 2011 by Ubuntu Genius Download Ubuntu Cleanup: How to Remove All Unused Linux Kernel … | […]
[…] How to Remove All Unused Linux Kernel Headers, Images and Modules. January 8, 2011 by Ubuntu Genius Download Ubuntu Cleanup: How to Remove All Unused Linux Kernel … | […]
[…] How to Remove All Unused Linux Kernel Headers, Images and Modules. January 8, 2011 by Ubuntu Genius Download Ubuntu Cleanup: How to Remove All Unused Linux Kernel … | […]
WARNING:
In KVM Servers with ubuntu virtual kernel this will remove ALL kernels from the system and make it unbootable.
This is because the virtual kernel packages are named like “linux-image-[0-9]\.[0-9]\.[0-9]{2}-[0-9]+-virtual” (e.g. linux-image-2.6.32-72-virtual) but the kernel name in uname ends in “-generic-pae” and not in “-virtual”.
You should make sure your regex is leaving at least one line for good.
I have tested the following on some dozens of servers, ubuntu and debian. It seems to reliably skip the current and the latest kernel installed.
for p in headers image image-extra; do dpkg -l “linux-$p-*” 2>/dev/null | sed ‘/^ii/!d;/'”$(uname -r | sed “s/\([-\.0-9]\+\)-.\+/\1/”)”‘/d;s/^[^ ]* [^ ]* \([^ ]*\).*/\1/;/[0-9]/!d’ | head -n -1;done
Actually, the “headers” should also be left out from my solution above because under certain circumstances they may uninstall kernel versions through dependencies that would otherwise remain untouched. Headers can usually be purged via “apt-get autoremove” anyway.
[…] https://ubuntugenius.wordpress.com/2011/01/08/ubuntu-cleanup-how-to-remove-all-unused-linux-kernel-h… […]
Thanks a lot, you made my day
[…] it safe to run the recommended command described in this blog as pointed out in that answer? If not, how should it be modified? I’m […]
I adapted the command into the below script for use on my Ubuntu machine, and it works perfectly for me. It cleans up one version (ABI) at a time, so if you need to clean up many versions, you may prefer the original version in the article. The good thing is that is shows you exactly what it is going to purge before it actually does it. I have not tested it on other distros.
[…] Taken from here: https://ubuntugenius.wordpress.com/2011/01/08/ubuntu-cleanup-how-to-remove-all-unused-linux-kernel-h… […]
[…] la podeis encontrar en http://markmcb.com/2013/02/04/cleanup-unused-linux-kernels-in-ubuntu/ y en https://ubuntugenius.wordpress.com/2011/01/08/ubuntu-cleanup-how-to-remove-all-unused-linux-kernel-h… está el blog del autor original del […]
[…] Thank you for your detailed post of your problem, this got me going in the right direction. Although it is useful to keep previous kernel files you can remove all of them in one go, check this post: How to Remove All Unused Linux Kernel Headers, Images and Modules […]
Thanx Alooot :)
[…] APT didn’t have such a useful feature (even for servers). So I started searching and found this article which explain a way that does exactly what I was looking for. The solution is synthesized as a […]
[…] Ubuntu Cleanup: How to Remove All Unused Linux Kernel Headers, Images and Modules Can’t upgrade due to low disk space on /boot […]
There has been a substantially safer mechanism in Ubuntu for years, which integrates with “apt-get autoremove”, and with a couple of config file edits, can also tie in with unattended upgrades so it’ll be automatic.
https://help.ubuntu.com/community/Lubuntu/Documentation/RemoveOldKernels
Danny, I think the biggest issue is without knowing you have to manually run the autoremove, Ubuntu will upgrade till it’s dead without that knowledge. Fundamentally a poorly designed end-user system.
silversword411, please, see the section “Automatic Maintenance” in the link. In Ubuntu 16.04 automatic installation of security updates is on by default and a new option Unattended-Upgrade::Remove-New-Unused-Dependencies, which defaults to true, should take care uninstalling the packages that become obsolete during upgrade.
[…] https://ubuntugenius.wordpress.com/2011/01/08/ubuntu-cleanup-how-to-remove-all-unused-linux-kernel-h… […]
Mentioned above – this caused me to lose 1 hour to get back all incorrectly uninstalled packages based on libc (which should not be removed, but the grep above selected it).
Dangerous command, at least change the text to suggest a dry-run first (and check for any packages apart from the expected ones -generic/headers etc)
Always workjed for me:-
sudo 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
regex is complicated !!! so true!
[…] Original Post: https://ubuntugenius.wordpress.com/2011/01/08/ubuntu-cleanup-how-to-remove-all-unused-linux-kernel-h… […]
I’m using a custom kernel. I used the first part of this command to list which will be passed to the purge command. It also included my kernel being used. Am I missing something here?
This worked for to clear 4GB off my root partition that was 100% full. There were tons of errors because the drive was full. I had to aptitude safe-upgrade to clear the unmet dependencies error but after I ran that long command again, the space was cleared. Not super comfy having only one kernel version but, we’ll see if it comes back to bite me.
This saved the day.
I had a problem using this command. It did delete the previous kernels and freed some space, but this command also deleted my gfortran and openmpi installation, which I had to install again in order to compile my code.
[…] From “Ubuntu Cleanup: How to Remove All Unused Linux Kernel Headers, Images and Modules“ […]
This is still a good method. The original cut-and-paste works very well?
[…] 참고 […]