Linux server out of disk space: how to find what is eating it
In this article8 sections
“No space left on device” is one of the most disruptive errors on a Linux server. The database stops accepting writes, the website returns errors, updates fail halfway, mail stops arriving, and sometimes you cannot even log in properly because the shell cannot create a temporary file.
The fix is almost never mysterious. A few places account for nearly all disk problems, and a systematic search finds the cause in a few minutes. The part that needs care is what you delete, because the wrong rm on a production server makes a bad day worse. This guide shows how to measure, where to look, what is safe to clear, and how to stop it from happening again.
Step 1: Is it space, or inodes?
A disk can be “full” in two different ways. Check both:
df -h
df -i
df -h shows how much space is used per filesystem. df -i shows inodes, which are the entries that identify files. A filesystem has a fixed number of inodes, and millions of tiny files (mail queues, session files, cache files) can use them all while plenty of space remains. If IUse% is 100% but Use% is low, you are looking for many small files, not large ones; skip ahead to the inode section.
Note which filesystem is full (the Mounted on column). Often it is /, but a separate /var, /home or data volume can fill alone.
Step 2: Find the biggest folders
Work from the top down. The -x option keeps du on one filesystem so it does not wander into other mounts:
sudo du -xh --max-depth=1 / 2>/dev/null | sort -h | tail -12
Take the largest result and repeat one level deeper:
sudo du -xh --max-depth=1 /var 2>/dev/null | sort -h | tail -10
sudo du -xh --max-depth=1 /var/log 2>/dev/null | sort -h | tail -10
A friendlier tool, if you can install it, is ncdu, an interactive browser:
sudo apt install ncdu
sudo ncdu -x /
To find single huge files anywhere on the filesystem:
sudo find / -xdev -type f -size +500M -printf '%s %p\n' 2>/dev/null | sort -n | tail -10
Step 3: The usual culprits, and the safe fix for each
Logs
The most common cause. Look in /var/log:
sudo du -sh /var/log/* 2>/dev/null | sort -h | tail
Do not delete a log file that a running service has open: the service keeps writing to the deleted file and the space is not released (see “deleted but still open” below). Empty it instead:
sudo truncate -s 0 /var/log/nginx/access.log
Then fix the cause: log rotation. logrotate should compress and discard old logs. Check that the rotation rule for the log exists in /etc/logrotate.d/, and test it with sudo logrotate -d /etc/logrotate.conf (the -d flag only simulates). A runaway error log is a symptom: read its last lines and fix whatever is producing the noise.
The systemd journal
journalctl --disk-usage
sudo journalctl --vacuum-size=200M
To keep it limited permanently, set SystemMaxUse=200M in /etc/systemd/journald.conf and restart systemd-journald.
Package caches and old kernels
sudo apt clean
sudo apt autoremove --purge
autoremove removes old kernels that are no longer needed, and it keeps the running one. Read its list before you confirm.
Docker
Docker can use tens of gigabytes without you noticing:
docker system df
docker image ls
Remove unused data, with care. The prune commands delete stopped containers, unused networks, and dangling images, and with -a every image not used by a container:
docker system prune
Do not add --volumes unless you are sure: volumes hold databases and uploads. Also look at container logs, which grow without limit by default; set max-size and max-file for the json-file log driver in /etc/docker/daemon.json.
Databases
MySQL and MariaDB write binary logs that can fill a disk if they are never expired. Check them:
sudo mysql -e "SHOW BINARY LOGS;"
sudo mysql -e "SHOW VARIABLES LIKE '%expire_logs%';"
Never delete these files by hand. Purge through the database, after confirming that no replica or backup process still needs them:
sudo mysql -e "PURGE BINARY LOGS BEFORE NOW() - INTERVAL 3 DAY;"
Set a retention so it does not recur (binlog_expire_logs_seconds on current MySQL, expire_logs_days on older versions). Slow query logs and general logs also grow quickly if left on. Check /var/lib/mysql for large tables, which may need pruning or archiving: SELECT table_schema, table_name, ROUND((data_length+index_length)/1024/1024) AS mb FROM information_schema.tables ORDER BY mb DESC LIMIT 10;.
Backups stored on the same server
A local backup folder that grows nightly will eventually fill the disk it is supposed to protect. Look in /var/backups, /home/*/backup* and the panel’s backup directory. Keep the newest few locally, ship the rest to storage elsewhere, and make sure old ones are pruned by the backup job itself.
Web application caches and uploads
On WordPress: cache plugins, image-optimisation plugins that keep originals, backup plugins that store archives under wp-content. On Laravel: storage/logs and storage/framework. Check these folders inside each site.
Large mail queues and mailboxes: sudo postqueue -p | tail -1 for Postfix, exim -bpc for Exim. A queue full of undeliverable spam means a compromised account or script; fixing it is a security task, not a cleanup.
Crash dumps, snaps and temporary files
sudo du -sh /var/crash /var/lib/systemd/coredump /tmp /var/tmp 2>/dev/null
snap list --all | awk '/disabled/{print $1, $2, $3}'
Core dumps are safe to remove once you have looked at why a process crashed. Old disabled snap revisions can be removed with sudo snap remove <name> --revision=<n>.
Deleted, but the space did not come back
You deleted a huge file and df still shows the disk full. A running process still holds the file open, and the space is released only when it closes. Find them:
sudo lsof +L1 | head
The NLINK column shows 0 for files that are deleted but open. Restarting the owning service (for example sudo systemctl restart nginx or the database) releases the space. This is the main reason to truncate logs rather than delete them.
When the problem is inodes
Count files per directory to find where millions of tiny files are:
sudo find /var -xdev -type f 2>/dev/null | cut -d/ -f1-4 | sort | uniq -c | sort -rn | head
Typical culprits: PHP session files in /var/lib/php/sessions, a mail spool, a cache folder that never expires entries, or an application writing one file per request. Clean old files by age, not blindly, for example sessions older than a day:
sudo find /var/lib/php/sessions -type f -mtime +1 -delete
Preview with -print instead of -delete first, and then fix whatever creates them.
If you simply need more room
Sometimes the data is legitimate and the disk is too small. On a cloud or virtual server you can enlarge the volume at the provider, then extend the partition and the filesystem. Confirm the layout before touching anything:
lsblk
df -hT /
For an ext4 root filesystem on the first partition of /dev/vda:
sudo growpart /dev/vda 1
sudo resize2fs /dev/vda1
For XFS, use sudo xfs_growfs / after growpart. Take a snapshot first, and double-check the device names, because these commands act on live disks.
Stop it recurring
- Alert at 80% and 90% for space and for inodes, from outside the server where possible (see the guide on monitoring a small server, which includes a script).
- Rotate and expire logs: system logs, the journal, database binary logs, and container logs.
- Keep backups off the server, and prune them automatically.
- Put growing data on its own volume, so a runaway log cannot take the system down with it.
- Review growth monthly:
duon the main directories, noting what increased.
When it is worth getting help
If the server is down right now, if you are not sure what is safe to delete, or if the growth has no obvious source, a careful second pair of eyes is cheap compared with deleting the wrong thing. I diagnose disk, memory and load problems on Linux servers and set up the alerting that catches them early.
You can see my profile on Upwork or read about monitoring and upkeep. If you are looking at your server for the first time, the guide to auditing an inherited server lists what else to check.