How to Check and Free Up Disk Space on a Linux Server

Server out of disk space? Find the culprit, win back gigabytes.

A full disk is one of the few things that can bring a whole server down: databases stop writing, logs stop logging and you can't even install the tool to find out why. Two commands tell you how full things are and where the space went, and there are a handful of safe places to reclaim gigabytes.

Quick reference

TaskCommand
How full is each disk?df -h
What's big in this folder?`du -sh *sort -h`
Interactive explorersudo ncdu /
Clear apt leftoverssudo apt autoremove -y && sudo apt clean
Shrink the journalsudo journalctl --vacuum-size=200M
Clear unused Docker stuffdocker system prune -a

1. How full is the disk?

df -h

Look at the line for / (and /boot if it's listed). Use% is the number that matters. Ignore tmpfs, udev and /snap/... lines — they're not real disks.

2. Where did the space go?

sudo du -sh /* 2>/dev/null | sort -h

That lists the size of every top-level folder, smallest to largest. Then drill into the biggest:

sudo du -sh /var/* | sort -h

The usual suspects are /var/log, /var/lib/docker, /home and /var/cache.

Quicker still, install ncdu — an interactive version you can navigate with arrow keys and delete from:

sudo apt install ncdu -y
sudo ncdu /

3. Safe things to delete

Package leftovers. Old kernels, downloaded .deb files:

sudo apt autoremove -y
sudo apt clean

The system journal. Can quietly reach several gigabytes:

sudo journalctl --vacuum-size=200M

Old compressed logs in /var/log (files ending .gz or .1):

sudo find /var/log -name "*.gz" -delete

Docker. Unused images, stopped containers and build caches are the biggest hoarder on most home servers:

docker system prune -a

Add --volumes only if you're sure no stopped container's data matters.

Snap. Ubuntu keeps old versions of every snap:

sudo snap set system refresh.retain=2

Your own stuff. Downloads folders, old backups, ISO files. ncdu ~ finds them.

4. A full /boot

If /boot is at 100%, old kernels are the cause and apt may refuse to run. Free it with:

sudo apt autoremove --purge -y

If that fails because apt itself is stuck, list kernels with dpkg -l | grep linux-image, then remove an old one by name with sudo dpkg --purge linux-image-6.8.0-31-generic (never the one uname -r reports as running).

5. Stop it happening again

Troubleshooting

Deleted a big file but df hasn't changed. A program still has it open. Restart that program (or find it with sudo lsof | grep deleted).

"No space left on device" but df shows space free. You've run out of inodes (file slots), usually from millions of tiny files. df -i confirms it; find the folder with sudo find / -xdev -type d -size +1M.

Can't run anything because the disk is 100% full. sudo apt clean and sudo journalctl --vacuum-size=50M need no free space to work. Start there.

Wrapping up

df -h to see how bad it is, ncdu to find the culprit, then apt autoremove, journalctl --vacuum, and docker system prune for the easy wins. Put a limit on the journal and you'll rarely have to do this again.

Call us