Chapter 5 · Lesson 2 of 10
Fix it

Fix a full disk on a Linux server

A full disk is fixed by finding the biggest directories with df and du, then removing or shrinking what filled it, such as logs, journal files, Docker data or deleted files still held open.

By the end you will

Find what filled the disk of a Linux server and free space safely without deleting data you still need.

Time
20 min
Level
Beginner
Check
4 questions
You need
  • SSH access to the server with sudo

Writes start failing, a deploy dies halfway, a database refuses to start, and the error says No space left on device. The disk is full, and you need to find out what filled it before you delete anything. The fast path is df to find the full filesystem, du to find the directory, and a short list of usual suspects: logs, the journal, Docker data, files held open after deletion, and inodes.

How it works#

A filesystem has two limits: bytes and inodes. Bytes are the data, and an inode is one record per file or directory. Either can run out. When the root filesystem is full, almost everything on the server breaks at once, because services need to write logs, sockets, temporary files and databases.

Linux frees a file’s space only when two things are true: no directory entry points at it, and no process has it open. That is why deleting a large log from under a running process often frees nothing. Keep that in mind during Step 4.

Step 1: Find the full filesystem#

bash
df -h

Read the Use% column and the Mounted on column. A server can have several filesystems, and only one may be full. Ignore tmpfs entries unless one is full, since they live in memory. Note the mount point of the full one, for example /, and use it as the starting point below. See check disk, memory and CPU usage for more on reading this output.

Step 2: Find the directory that holds the space#

bash
sudo du -xh --max-depth=1 / 2>/dev/null | sort -h

The -x flag keeps du on one filesystem, so it does not wander into other mounts and give you misleading totals. Sort puts the biggest entry last. Take the biggest directory and repeat the command on it, for example sudo du -xh --max-depth=1 /var | sort -h. Two or three rounds usually lands on the culprit. Do not run du on /proc or other virtual filesystems, and expect it to be slow on very large trees.

Step 3: Check the usual suspects#

Logs. Look in /var/log. A single application log that grows without rotation is the classic cause. Check whether logrotate covers it, and for containers see limit Docker log size.

The systemd journal.

bash
journalctl --disk-usage
sudo journalctl --vacuum-size=500M

The vacuum command deletes the oldest archived journal files until the total is under the size. Pick a size that still keeps the history you need. Old log entries are gone for good afterwards.

Docker.

bash
docker system df

This table shows images, containers, volumes and build cache, with a RECLAIMABLE column. Do not rush to prune. Volumes can hold your database. Follow free disk space with docker prune safely, which separates the safe commands from the dangerous ones. Container JSON logs live under /var/lib/docker/containers/ and are not shown as a separate row.

Package caches and old kernels. On Ubuntu, sudo apt clean clears downloaded packages, and sudo apt autoremove lists old unused packages and asks before removing them.

Step 4: Look for deleted files that are still open#

If df says the disk is nearly full but du finds far less, a process is holding a deleted file:

bash
sudo lsof +L1

+L1 lists open files with a link count below one, meaning deleted. The SIZE/OFF column shows how big each one is and COMMAND and PID show who holds it. The space comes back when that process closes the file, which for most services means restarting it (sudo systemctl restart nginx, or docker restart <container>). Restart only what you understand, and do it in a quiet moment if the service is serving users. Never delete a log file that is in use as a fix: truncate it from the application’s own rotation instead.

Step 5: Check inodes#

If writes fail but df -h shows free bytes, check inodes:

bash
df -i

When IUse% is at 100 percent, you have too many small files. To find the directory with the most files, count entries per top directory:

bash
sudo find /var -xdev -type f | cut -d/ -f1-4 | sort | uniq -c | sort -n | tail

Common sources are session files, mail queues, cache directories and build artifacts. Remove the old files only after you know what owns them.

Step 6: Free the space#

Now act on what you found. Delete or compress old backups you have already copied elsewhere, vacuum the journal, prune Docker data you checked, and remove old release directories. For a log that must keep its file but lose content, truncate is safe for a running process:

bash
sudo truncate -s 0 /var/log/myapp/app.log

This empties the file and cannot be undone, so use it only on logs you do not need. Then fix the cause, not just the symptom: add log rotation, set Docker log limits, or grow the disk.

Check it worked#

bash
df -h /
df -i /

Use% should have dropped well below 100, and the service that failed should start again. Watch it for a day: if usage climbs back fast, something is still writing. sudo du -xh --max-depth=1 /var | sort -h on the same directory shows whether it regrew.

Prevent it#

  • Rotate logs and cap Docker logs with max-size and max-file.
  • Add a disk usage alert at around 80 percent, using uptime monitoring or your provider’s tools.
  • Keep backups on another machine, not in /var/backups on the same disk.

Edge cases and gotchas#

  • Reserved blocks: ext4 reserves a slice for root, so Avail can reach zero for normal users while root can still write.
  • Mounts hiding data: Files written to a directory before a disk was mounted over it still use space on the lower filesystem, and du cannot see them until you unmount.
  • Sparse and large database files: A file’s apparent size and real size can differ. du -h shows disk use, ls -lh shows apparent size.
  • Snap and container layers: Old snap revisions and overlay layers are kept on purpose. Remove them with the tool that owns them, not with rm.
  • Deleting while a deploy runs: Free space before retrying a failed deploy, since a half-written image layer may remain.
  • Backups in the same tree: A backup job that writes to the disk it is backing up can fill it when it fails to clean up.

Troubleshooting#

SymptomLikely causeFix
df 100 percent, du much smallerDeleted file still held opensudo lsof +L1, restart the holding process
No space left but df -h shows freeInodes exhausteddf -i, remove piles of small files
/var/lib/docker is hugeImages, build cache or volumesdocker system df, then prune with care
Journal uses gigabytesNo size limit setjournalctl --vacuum-size, set SystemMaxUse
Space returns then vanishesA process keeps writingFind it with du over time, fix the logging
du is very slowScanning network or virtual mountsUse -x and start from the right directory

Quick reference#

bash
df -h
df -i
sudo du -xh --max-depth=1 / | sort -h
journalctl --disk-usage
sudo journalctl --vacuum-size=500M
docker system df
sudo lsof +L1

Next#

Continue with Fix 504 Gateway Timeout behind a reverse proxy.

Try it: Find what filled the disk

Walk from the full filesystem down to the directory that owns the space, then check Docker and the journal.

practice, nothing runs on your machine

Want a real machine? Free browser labs: Killercoda

Check what you learned4 questions
1Which command shows how full each mounted filesystem is?
2Why use du -x when hunting for big directories from /?
3df shows 100 percent used, du totals are far smaller, and you deleted a big log earlier. What is likely?
4Writes fail with No space left on device but df -h shows free space. What do you check next?

Frequently asked questions

Why is my Linux server disk full?

The usual causes are growing log files, a large systemd journal, Docker images and build cache, old backups, or a deleted file that a running process still holds open. df shows which filesystem is full and du shows where the space went.

Why does df say the disk is full but du shows less?

A process probably still holds a deleted file open, so the space is not released until the process closes it. Run sudo lsof +L1 to list those files, then restart the process.

How do I free disk space on Ubuntu quickly?

Find the largest directories with du, vacuum the journal with journalctl --vacuum-size, and clean unused Docker data with the docker prune commands. Check what each command removes before running it.

What does No space left on device mean when df shows free space?

The filesystem may have run out of inodes, not bytes. Run df -i to check the IUse% column.

Sources

View MD Edit
Share
Helpful?
Progress is saved in this browser only.

Keep going: guides and tutorials

View MD Edit
Share
Helpful?
Run this on your own server

Levelrail deploys apps from git with health-checked releases and one-click rollback.

docker pull ghcr.io/glincker/levelrail:latest Install Levelrail From GLINR Studios