In the previous exercise we have removed all the extra/unwanted/unneeded services from our distro, this time we are going to reclaim back some disk space.
First of all, why you might want to do this kind of cleanup?
We all know and laugh on Windows 10/11 disk requirements, right? But out-of-the-box Linux distros are becoming nothing better than it. And the reason for that is, distro teams are trying to squeeze as much as they can into the distribution, so most use cases will be covered. It might not sound like a bad idea, but what is the point for you, yes, specifically you, to have installed on your disk (and running as a daemon) such a thing as CUPS, if you don't even have a printer at home, right? Or having some graphical themes for your GRUB, when you don't care about bootloader beauty? Or having SNAP deamon installed and running, if you don't like vaping long-bearded hipsters and all the novelties and prefer your software to be installed from distro repo? Or why on earth you need to spare 300+ Mb on your hard drive to have "wireless-regdb" package (wireless regulatory database) for a system without WiFi?
As I mentioned, I do own an SBC which is running either off EMMC or off SD card. As you can imagine, the storage there is not endless. I wanted my OS to be as compact as it could be, without hurting much to its operation capabilities and not by paying a price of having a limited amount of applications. Quite the opposite - I want my free space to be used by the applications I use and by my own content (photos, music, videos).
Secondly, the less free space you have on your solid state drive, no matter what generation it is, the less it will span. This might sound like a joke, unless you
investigate that on your own, how SSD is operating and how writes are distributed across free blocks. Logic here is very simple, whenever you update something on your disk, the SSD controller will likely mark the storage cell occupied with your "old" data as clean, without actually cleaning it, and copy modified things around to a new storage cell. This approach is called "copy-on-write", and this is what all solid-state-drive controllers are doing, underneath the hood. So the less free space you have on your drive, the more pressure those free cells will get.
Before we begin
Your next best friend should be a tool like KDE Filelight or GNOME Baobab (recently was renamed to generic "Disk usage analyzer").
You open it up, and look very carefully what takes the most space on your drive.
Delete apt cache
Unfortunately apt (a Debian-based distros package manager) has a bad habit of leaving the trash behind. It's just coded that way, so it first downloads the package to a local "cache"- your hard drive - and only then it installs it. And guess what? It doesn't give a shit to wipe out whatever remained after it. It's like if you never emptied your "Downloads" folder in Windows :)
So we can do that manually:
apt-get autoclean
apt autoclean
apt clean
Compact the jorunal
Quite time ago, Linux distributions have switched from keeping a good old plain text log files under /var/log to something new and shiny. That something new was called journalctl. Basically that's a service which keeps up a binary log from whatever other service or software who wants to put something into system log. Binary here is a good thing in terms of space occupied, because it's using some kind of compression. But the bad thing about it, is that 90% of desktop Linux users don't even know how to look into those journalctl logs and they never do.
If you're not a big fan of archeological digging into your old journalctl records from a month ago, I strongly suggest you to limit, how much logs can journalctl write to your hdd.
sudo journalctl --vacuum-size=100M
Delete extra locales
du -hs /usr/share/locale/*
find /usr/share/locale/ -type f -exec dpkg -S {} \; | sort -u
TODO https://serverfault.com/questions/394610/remove-a-locale-in-ubuntu/1037183#1037183
Delete extra kernels you don't need
apt list --installed "*5.15.0-50*"
Delete snaps / flatpacks you don't use or alltogether
rm -rf /var/lib/flatpak
Remove the swapfile / swap partition
Find and delete the fattest software
Unfortunately, I found no easy tool to use, how can you measure what is the
storage impact of the packages you installed manually, considering all the
dependencies it brought, when the dependencies are only needed to run that
your package. All the GUI tools that are coming with various Desktop Environments are doing the same mistake - whenever they're calculating the size occupied by a package, they don't consider its dependencies.
So I had to make my own simple scrip:
Debian-based distros
cat << EOF > /usr/local/bin/apt-space-used-by #!/bin/sh out=\$(apt-get --assume-no autoremove \$1 2>&1) ec=\$? if [ \$ec -eq 1 ] ; then size=\$(echo -n "\$out" | grep "After this operation" | cut -d' ' -f4-5) size=\$(echo \$size | sed -e "s/[^0-9kMG,.]//g" | tr 'k' 'K') echo -n "\$size\\t" else echo -n "0 (cannot delete)" fi echo -n "\$1\\t"
dpkg-query -W -f='\${binary:Summary}\\n' \$1
EOF
chmod a+x /usr/local/bin/apt-space-used-by
# before we go any further we need to cleanout all the orphan pacakges, as they will be bothering our little script
apt autoremovedpkg-query -W -f='${binary:Package}\n' | xargs -I % sh -c "apt-space-used-by %" | tee -a
# now if you want to see what packages you installed manually will free what space
apt-mark showmanual | xargs -I % sh -c "apt-space-used-by %" | sort -h
# ... or whatever other packages which came with your distro
echo "" > /tmp/final_report.txt/tmp/final_report.txt
cat/tmp/final_report.txt |
sort -h
RHEL-based distros
cat << EOF > /usr/local/bin/yum-space-used-by
#!/bin/sh
out=$(yum --assumeno erase $1 2>&1)
echo -n "$out" | grep -qE "^Freed space:"
ec=$?
if [ $ec -eq 0 ] ; then
size=$(echo -n "$out" | grep -E "^Freed space:" | cut -d' ' -f3-4)
size=$(echo $size | sed -e "s/[^0-9kMG,.]//g" | tr 'k' 'K')
echo -ne "$size\t"
else
echo -ne "0 (cannot delete)"
fi
echo -ne "$1\t"
rpm -q --queryformat="%{SUMMARY}" $1
echo ""
EOF
chmod a+x /usr/local/bin/yum-space-used-by
# before we go any further we need to cleanout all the orphan pacakges, as they will be bothering our little script
yum autoremoverpm -qa | xargs -I % sh -c "yum-space-used-by %" | tee -a
# now if you want to see what packages you installed manually will free what space
yum history userinstalled | grep -v "Packages installed by user" | xargs -I % sh -c "yum-space-used-by %" | sort -h
# ... or whatever other packages which came with your distro
echo "" > /tmp/final_report.txt/tmp/final_report.txt
cat/tmp/final_report.txt |
sort -h
A word of caution regarding the last command. Imagine you have git installed. The "git" package brings with it a set of mandatory dependencies, it couldn't live without, like "git-man". So if you delete "git-man", it will also delete "git". This is why you will see some that both "git" and "git-man" packages will free up the same amount of disk space.
Once you figured out what you're ready to remove run:
apt autoremove <pacakgename>
Upsize your partitions
It might be the case, that you do have some unallocated space on your drive. That's quite easy to fix. Imagine you have a disk (/dev/sda) with a single partition (/dev/sda1) and some free space after that partition. You first run lsblk to confirm what kind of layout you have, then you run parted and resize that 1st partition (/dev/sda1) to occupy 100% of remaining free space. And the cherry on a cake - you upsize the filesystem. That's it. Everything can be done online, without the need for reboot.
lsblk
parted /dev/sda
print all
resizepart 1 100%
resize2fs /dev/sda1
TODO
# remove dev packages you installed manually apt-mark showmanual | grep -E "\-dev$" | awk '{system("sudo apt-get --dry-run purge "$1)}' # remove It's safe to remove the content of your trashbin: ~/.local/share/Trash See also what kind of programs you might have already deleted, but they left behind their traces: ~/.local/share Like in my case I had some trails left by Konqueror (browser) I was experimenting with, and then used "apt remove" instead of "apt purge" It's just me being not very carefull, but there's a good thing about it, we can pick up all such traces in one go: dpkg --get-selections | awk '$2=="deinstall" {system("sudo apt-get --dry-run purge "$1)}' dpkg --get-selections | awk '$2=="deinstall" {system("sudo apt-get -y purge "$1)}'