How to Check and Free Up Disk Space on Ubuntu Server Print

  • 0

No space left on device” is one of those errors that takes a whole server down at 3 am: databases stop writing, sites throw 500s, even logging in gets weird. Finding what ate the disk on Ubuntu Server takes two commands, and freeing space usually takes five minutes once you know the usual suspects. Here is the full playbook.

Step 1 — How bad is it?

df -h

Look at the Use% of / (and any separate /var or /home). Above 90% on a database server is an emergency. If df says the disk is full but you cannot find the files, check inodes too: df -i — millions of tiny files (sessions, cache) can exhaust inodes with space to spare.

Step 2 — Find the space hogs

sudo du -xh / --max-depth=1 2>/dev/null | sort -rh | head
# then drill into the biggest directory:
sudo du -xh /var --max-depth=2 2>/dev/null | sort -rh | head
Finding what uses disk space on Ubuntu with df and du sorted by size

Or install the interactive ncdu (sudo apt install ncdu, then sudo ncdu -x /) and browse with the arrow keys — the fastest tool for this job.

Step 3 — The usual suspects, and safe cleanups

Systemd journal (often 10–30 GB!)

journalctl --disk-usage
sudo journalctl --vacuum-size=200M

Cap it permanently in /etc/systemd/journald.conf with SystemMaxUse=200M.

Old packages and kernels

sudo apt autoremove --purge -y
sudo apt clean

Giant log files

sudo find /var/log -type f -size +100M -exec ls -lh {} \;
sudo truncate -s 0 /var/log/huge-file.log   # empty a live log safely

Never rm a log a running daemon has open — the space is not released until the process restarts; truncate avoids the problem.

Docker leftovers

docker system df
docker system prune -a --volumes

If you run Docker, unused images and build cache routinely hold many gigabytes. (Careful: --volumes removes anonymous volumes' data.)

Snap package revisions

sudo snap set system refresh.retain=2
Freeing disk space on Ubuntu with journalctl vacuum and apt autoremove

Step 4 — Keep it from happening again

Add log rotation for your applications (/etc/logrotate.d/), cap the journal, keep autoremove in your update routine, and set up a cron job that alerts you at 85%:

#!/bin/bash
USE=$(df / --output=pcent | tail -1 | tr -dc '0-9')
[ "$USE" -ge 85 ] && echo "Disk at ${USE}% on $(hostname)" | mail -s "Disk alert" [email protected]

Related Ubuntu guides

Prefer to have experts handle it?

LFA IT provides fully managed Ubuntu VPS and dedicated servers — initial setup, security hardening, monitoring and 24/7 support, so you can focus on your business. Explore our hosting and server management services or open a support ticket and our engineers will take it from there.


Was this answer helpful?

« Back