Ultimate Guide to Freeing Up Disk Space on Linux: Clean Logs, Cache, and More
“Disk space is like pizza — you always think you have enough until you run out at the worst possible moment.
Linux servers generally perform really well and we all love it, but software running on it often conks off due to low disk space availability. It is important that you know the method and commands that will help you claim the precious disk space. Let's see how...
Find the culprit: Who's hogging up the space?
Disk Free (df)
$ df -h
Filesystem Size Used Avail Use% Mounted on
/dev/root 29G 23G 6.7G 78% /
devtmpfs 3.9G 0 3.9G 0% /dev
tmpfs 3.9G 0 3.9G 0% /dev/shm
tmpfs 789M 1.1M 788M 1% /run
tmpfs 5.0M 0 5.0M 0% /run/lock
tmpfs 3.9G 0 3.9G 0% /sys/fs/cgroup
/dev/loop1 64M 64M 0 100% /snap/core20/2379
/dev/loop2 92M 92M 0 100% /snap/lxd/24061
/dev/loop3 92M 92M 0 100% /snap/lxd/29619
/dev/loop4 39M 39M 0 100% /snap/snapd/21759
/dev/sda15 105M 6.1M 99M 6% /boot/efi
/dev/sdb1 16G 28K 15G 1% /mnt
/dev/loop6 64M 64M 0 100% /snap/core20/2434
/dev/loop0 45M 45M 0 100% /snap/snapd/23258
tmpfs 789M 0 789M 0% /run/user/1000
Understanding the output
-
/dev/root
: This is the main root filesystem where most of your system files and user data reside. It is nearly 78% full. -
/dev/shm, /run, /sys/fs/cgroup, tmpfs
: These are temporary filesystems (RAM-backed) used for various runtime operations. Their usage is minimal. -
/snap/...
: Snap packages are mounted as loopback devices. Their sizes are fixed and fully utilized (100% use is expected). -
/boot/efi
: Partition for EFI boot data. It’s only 6% used. -
/mnt
: Likely an external or secondary disk with plenty of free space (16G total, only 28K used).
Disk Usage (du)
$ sudo du -hxd1 / | sort -h
4.0K /media
4.0K /srv
16K /lost+found
16K /opt
24K /snap
36K /root
68K /tmp
5.6M /etc
476M /boot
4.5G /usr
5.9G /var
12G /home
23G /
Understanding the output
/home
folder on root is using the most amount of data12GB
. Digging in is easy, simply add the folder path you are interested in. In a few tries, I was able to figure it out, that since it is a build server where multiple agents were deployed (and recently upgraded), there was junk lying around. Trying the good oldrm -rf culprit-folders
gave me my space back.
$ sudo du -hxd1 /home | sort -h
12G /home
12G /home/azureuser
$ sudo du -hxd1 /home/azureuser | sort -h
...
...
...
189M /home/azureuser/.nvm
2.6G /home/azureuser/myagent-4
2.8G /home/azureuser/myagent-3
2.9G /home/azureuser/myagent-2
3.1G /home/azureuser/myagent-1
12G /home/azureuser
$ sudo du -hxd1 /home/azureuser/myagent-1 | sort -h
54M /home/azureuser/myagent-1/_diag
105M /home/azureuser/myagent-1/bin.3.241.0
107M /home/azureuser/myagent-1/bin.4.248.0
317M /home/azureuser/myagent-1/externals.3.241.0
317M /home/azureuser/myagent-1/externals.4.248.0
2.2G /home/azureuser/myagent-1/_work
3.1G /home/azureuser/myagent-1
The flags in the command imply:
-
-h
: Human-readable sizes. -
-x
: Restrict the scan to the /dev/root filesystem (avoid crossing into other mounts like /mnt or /snap). -
-d1
: Show only the sizes of top-level directories.
Translation: Show me all the big directories in /, but please sort them nicely so I can pick on the fattest one.
You’ll likely see the usual suspects: /var
, /home
, /usr
. Spoiler alert: /var
is often the worst offender.
Log Files: Do You Really Need ALL That History?
Your system loves keeping a diary, especially in /var/log. But, let’s face it: Do you really need logs from six months ago when you only debugged that server crash once? No.
Here’s how to trim the fat:
- To check which logs are hogging space:
$ sudo du -ah /var/log | sort -h | tail -n 10
If you’re paranoid about accidentally deleting something important, compress them instead:
$ sudo gzip /var/log/<filename>
To remove the outdated ones:
$ sudo snap remove <package-name> --revision=<old-revision>
Clean Package Caches: Because You Don’t Need a Backlog
Ever downloaded software and wondered why your disk is mysteriously shrinking? That’s because package managers like apt keep a cache of installation files.
To clean it up:
$ sudo apt clean
And while you are it, kick out the unused dependencies:
$ sudo apt autoremove
Reading package lists... Done
Building dependency tree
Reading state information... Done
The following packages will be REMOVED:
...
linux-image-5.15.0-1065-azure linux-image-5.15.0-1067-azure linux-image-5.15.0-1068-azure linux-image-5.15.0-1070-azure
linux-image-5.15.0-1071-azure linux-image-5.15.0-1072-azure linux-modules-5.15.0-1064-azure linux-modules-5.15.0-1065-azure
linux-modules-5.15.0-1067-azure linux-modules-5.15.0-1068-azure linux-modules-5.15.0-1070-azure linux-modules-5.15.0-1071-azure
linux-modules-5.15.0-1072-azure linux-tools-5.15.0-1064-azure linux-tools-5.15.0-1065-azure linux-tools-5.15.0-1067-azure
linux-tools-5.15.0-1068-azure linux-tools-5.15.0-1070-azure linux-tools-5.15.0-1071-azure linux-tools-5.15.0-1072-azure
0 upgraded, 0 newly installed, 56 to remove and 47 not upgraded.
After this operation, 2019 MB disk space will be freed.
Do you want to continue? [Y/n]
Press Y if you are good to remove these.
Look for Hidden Giants: The Largest Files
When in doubt, find the truly greedy files with this command:
$ sudo find / -xdev -type f -exec du -h {} + | sort -rh | head -n 10
175M /usr/bin/mongod
143M /usr/bin/mongosh
140M /home/azureuser/vsts-agent-linux-x64-3.241.0.tar.gz
124M /usr/bin/mongos
123M /var/log/btmp.1
112M /usr/lib/mongosh_crypt_v1.so
109M /var/lib/docker/buildkit/metadata_v2.db
103M /usr/bin/dockerd
100M /var/lib/mongodb/journal/WiredTigerPreplog.0000000002
100M /var/lib/mongodb/journal/WiredTigerPreplog.0000000001
This will point out the top 10 offenders. Approach with caution, though—don’t delete system files unless you want your Linux system to stage a rebellion.
Handle the Big Boss: /var/log/journal
The systemd-journald logs in /var/log/journal can balloon to ridiculous sizes. If you’re running low on space, it’s okay to trim them down.
To safely clean them:
$ sudo journalctl --vacuum-size=500M
sudo journalctl --vacuum-time=2weeks
Move Big Files to an External Drive
Got some big files you can’t delete but don’t need on your main drive? Move them to an external disk:
$ sudo mv /path/to/large/files /mnt/external_drive/
After a little bit of clean up here is how my drive looks like:
Filesystem Size Used Avail Use% Mounted on
/dev/root 29G 14G 16G 46% /
devtmpfs 3.9G 0 3.9G 0% /dev
tmpfs 3.9G 0 3.9G 0% /dev/shm
tmpfs 789M 920K 789M 1% /run
tmpfs 5.0M 0 5.0M 0% /run/lock
tmpfs 3.9G 0 3.9G 0% /sys/fs/cgroup
/dev/loop1 64M 64M 0 100% /snap/core20/2379
/dev/loop2 92M 92M 0 100% /snap/lxd/24061
/dev/loop3 92M 92M 0 100% /snap/lxd/29619
/dev/loop4 39M 39M 0 100% /snap/snapd/21759
/dev/sda15 105M 6.1M 99M 6% /boot/efi
/dev/sdb1 16G 28K 15G 1% /mnt
/dev/loop6 64M 64M 0 100% /snap/core20/2434
/dev/loop0 45M 45M 0 100% /snap/snapd/23258
tmpfs 789M 0 789M 0% /run/user/1000
So, next time your Linux system gives you the low-disk-space warning, smile and say: “I’ve got this.” Because now you do.
Happy cleaning! 🧹