Skip to content

Instantly share code, notes, and snippets.

@Iman
Last active Sep 22, 2021
Embed
What would you like to do?
Free up disk space on Ubuntu - clean log, cache, archive packages/apt archives, orphaned packages, old kernel and remove the trash
#!/bin/sh
#Check the Drive Space Used by Cached Files
du -sh /var/cache/apt/archives
#Clean all the log file
#for logs in `find /var/log -type f`; do > $logs; done
logs=`find /var/log -type f`
for i in $logs
do
> $i
done
#Getting rid of partial packages
apt-get clean && apt-get autoclean
apt-get remove --purge -y software-properties-common
#Getting rid of no longer required packages
apt-get autoremove -y
#Getting rid of orphaned packages
deborphan | xargs sudo apt-get -y remove --purge
#Free up space by clean out the cached packages
apt-get clean
# Remove the Trash
rm -rf /home/*/.local/share/Trash/*/**
rm -rf /root/.local/share/Trash/*/**
# Remove Man
rm -rf /usr/share/man/??
rm -rf /usr/share/man/??_*
#Delete all .gz and rotated file
find /var/log -type f -regex ".*\.gz$" | xargs rm -Rf
find /var/log -type f -regex ".*\.[0-9]$" | xargs rm -Rf
#Cleaning the old kernels
dpkg-query -l|grep linux-im*
#dpkg-query -l |grep linux-im*|awk '{print $2}'
apt-get purge $(dpkg -l 'linux-*' | sed '/^ii/!d;/'"$(uname -r | sed "s/\(.*\)-\([^0-9]\+\)/\1/")"'/d;s/^[^ ]* [^ ]* \([^ ]*\).*/\1/;/[0-9]/!d' | head -n -1) --assume-yes
apt-get install linux-headers-`uname -r|cut -d'-' -f3`-`uname -r|cut -d'-' -f4`
#Cleaning is completed
echo "Cleaning is completed"
@mohsen-walton

This comment has been minimized.

Copy link

@mohsen-walton mohsen-walton commented Jun 2, 2020

thank you it worked like a charm on ubuntu 20.04

@gelahcem

This comment has been minimized.

Copy link

@gelahcem gelahcem commented Jun 19, 2020

Great work!! 🏆
Executed on Ubuntu 20.04 👍

@yarkomar

This comment has been minimized.

Copy link

@yarkomar yarkomar commented Jul 2, 2020

emm, it has removed my "Ubuntu Software Center"... :) (18.04)
I've kinda restored it with apt install gnome-software though

@Ch3mjor

This comment has been minimized.

Copy link

@Ch3mjor Ch3mjor commented Jul 6, 2020

This broke so many of my dependencies on Ubuntu 18.04. Be warned it may break yours too.

@tyeth

This comment has been minimized.

Copy link

@tyeth tyeth commented Jul 8, 2020

Comment out (prefix with #) the line
apt remove --purge software-properties-common
To avoid harming your system by removing key packages

@shuvoenr

This comment has been minimized.

Copy link

@shuvoenr shuvoenr commented Jul 16, 2020

Works well. Thanks

@luiz-rocha

This comment has been minimized.

Copy link

@luiz-rocha luiz-rocha commented Aug 3, 2020

BE CAREFUL DO NOT USE THIS SCRIPT, YOU WILL HARM YOUR SYSTEM

The following packages will be REMOVED:
apturl* gnome-software* gnome-software-plugin-snap* nautilus-share* software-properties-common* software-properties-gtk* ubuntu-desktop* ubuntu-desktop-minimal*
0 upgraded, 0 newly installed, 8 to remove and 0 not upgraded.
After this operation, 8.035 kB disk space will be freed.
(Reading database ... 242150 files and directories currently installed.)
Removing nautilus-share (0.7.3-2ubuntu3) ...
Removing apturl (0.5.2ubuntu19) ...
Removing gnome-software-plugin-snap (3.36.1-0ubuntu0.20.04.0) ...
Removing gnome-software (3.36.1-0ubuntu0.20.04.0) ...
Removing ubuntu-desktop (1.450.1) ...
Removing ubuntu-desktop-minimal (1.450.1) ...
Removing software-properties-gtk (0.98.9.1) ...
Removing software-properties-common (0.98.9.1) ...

@Ferdous123

This comment has been minimized.

Copy link

@Ferdous123 Ferdous123 commented Aug 23, 2020

awesome bro, cleaned around 3.7 gb on Ubuntu 20.04 LTS. I was being hopeless after trying a lot & then yours worked flawlessly....

@tardonly

This comment has been minimized.

Copy link

@tardonly tardonly commented Sep 12, 2020

it cleaned 67.5G , don't know why. but Great cleaning
System ubuntu 20.10 development branch

@Jonathana1106

This comment has been minimized.

Copy link

@Jonathana1106 Jonathana1106 commented Sep 19, 2020

Thanks man! Ubuntu 20.04

@ooobii

This comment has been minimized.

Copy link

@ooobii ooobii commented Oct 19, 2020

Worked very well for the multiple minimal install VMs I have for both 20.04 and 18.04! Thank you! 😄

Do be careful with every script you copy from the internet, and inspect the commands within until you understand what they're doing.

@matiasdiez

This comment has been minimized.

Copy link

@matiasdiez matiasdiez commented Oct 23, 2020

Thank you! I am new and I was lost space and didn't find how. This help me to do all the task I was reading about in one shot (sorry my english). Ubuntu 20.04.

@Iman

This comment has been minimized.

Copy link
Owner Author

@Iman Iman commented Oct 23, 2020

BE CAREFUL DO NOT USE THIS SCRIPT, YOU WILL HARM YOUR SYSTEM

The following packages will be REMOVED:
apturl* gnome-software* gnome-software-plugin-snap* nautilus-share* software-properties-common* software-properties-gtk* ubuntu-desktop* ubuntu-desktop-minimal*
0 upgraded, 0 newly installed, 8 to remove and 0 not upgraded.
After this operation, 8.035 kB disk space will be freed.
(Reading database ... 242150 files and directories currently installed.)
Removing nautilus-share (0.7.3-2ubuntu3) ...
Removing apturl (0.5.2ubuntu19) ...
Removing gnome-software-plugin-snap (3.36.1-0ubuntu0.20.04.0) ...
Removing gnome-software (3.36.1-0ubuntu0.20.04.0) ...
Removing ubuntu-desktop (1.450.1) ...
Removing ubuntu-desktop-minimal (1.450.1) ...
Removing software-properties-gtk (0.98.9.1) ...
Removing software-properties-common (0.98.9.1) ...

Perhaps those packages are outdated and there's no need to maintain them on your machine, hence you've been prompted to allow deletion and make more space.

@tyeth

This comment has been minimized.

Copy link

@tyeth tyeth commented Oct 26, 2020

@elileal

This comment has been minimized.

Copy link

@elileal elileal commented Nov 17, 2020

Excelente! TKS

@pgsridhar

This comment has been minimized.

Copy link

@pgsridhar pgsridhar commented Jan 21, 2021

thanks :)

@JinnaBalu

This comment has been minimized.

Copy link

@JinnaBalu JinnaBalu commented Feb 11, 2021

Nice work

@Wedz0ff

This comment has been minimized.

Copy link

@Wedz0ff Wedz0ff commented Mar 13, 2021

It worked on Ubuntu 18.04 on DigitalOcean, ty.

@lahirusamith

This comment has been minimized.

Copy link

@lahirusamith lahirusamith commented Mar 29, 2021

nice!

@1ur11

This comment has been minimized.

Copy link

@1ur11 1ur11 commented Apr 12, 2021

Ubuntu 18.04.4 works fine

@PthDE

This comment has been minimized.

Copy link

@PthDE PthDE commented May 2, 2021

Brilliant!!
Running with 18.04 LTS. I have apps like KiCAD and PyCharm on a 34GB root file-system. Down to 6GB left clean.sh found another 5GB to clean out to give me 11GB space left.
Thanks Iman!

@chakradharchokkaku

This comment has been minimized.

Copy link

@chakradharchokkaku chakradharchokkaku commented May 8, 2021

It worked on Ubuntu 20.04. Nearly 20GB cleaned.

@neilyoung

This comment has been minimized.

Copy link

@neilyoung neilyoung commented Jun 8, 2021

thumbs up

@avinashxw

This comment has been minimized.

Copy link

@avinashxw avinashxw commented Jun 20, 2021

Great work @Iman!
Never thought the cleanup is super easy with this script.
All happened in a jiffy!

@SkellaMental

This comment has been minimized.

Copy link

@SkellaMental SkellaMental commented Sep 20, 2021

Skip the step
apt-get remove --purge -y software-properties-common
Otherwise some handy steps and commands for tidying out excessive logs thanks !

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment