Skip to main content

Linux and servers · Intermediate · 14 min

Fix a full disk: deleted open files, inodes and reserved space

By Padding · Updated 18 Nov 2026
Ubuntu 26.04.1 LTS · lsof 4.99.4 · e2fsprogs 1.47.2

You will diagnose the full-disk cases du cannot explain, free space held by deleted files that are still open, find and clear inode-hungry directories, read and tune ext4 reserved blocks, and keep a read-only triage script for the next outage.

On this page

The usual full-disk fix is to run du, find the big folder and clear it. That is covered in Find and free up disk space. This tutorial is for the times that method fails:

  • df says the disk is full, but du adds up to far less.
  • Writes fail with No space left on device while df -h still shows free space.
  • df shows Avail at 0 and Use% at 100, yet Size minus Used is several gigabytes; root can still write but services running as normal users get No space left on device.

Each one has a different cause: a deleted file that a process still holds open, a filesystem that has run out of inodes, and ext4's reserved blocks. You will reproduce the first two in a throwaway folder, ~/fulldisk-demo, look at reserved blocks on an ext4 image file, then write a small read-only script that checks all three on any server. The last step is a first-aid order to follow when a production server has stopped writing.

On small, low-cost VPS plans (20 to 25 GB disks are common) these cases turn up sooner than you expect. If you manage servers over mobile data, run the steps inside tmux so a dropped SSH session does not cut a command off.

Step 1 · Install the tools and name the kind of full

lsof lists open files and is not on a stock server. e2fsprogs provides tune2fs, used later for ext4:

Bash
sudo apt update
sudo apt install -y lsof e2fsprogs

Now compare three numbers for the root filesystem: space, inodes, and what du can actually find.

Bash
df -h /
df -i /
sudo du -xsh / 2>/dev/null

-x keeps du on one filesystem, so it does not count other mounts under /. Read the results this way:

  • Use% near 100 and du well below Used. Space is held by files du cannot see. Usually deleted files that are still open. Go to the next step.
  • IUse% near 100. The filesystem has run out of inodes, one per file or directory, however small. Go to the inode step.
  • Avail at 0 and Use% at 100, but Size minus Used is still positive. The filesystem is into ext4's reserved blocks: processes running as root can still write, services running as normal users cannot. On a healthy ext4 filesystem Avail is always a little smaller than Size minus Used; that gap alone is not a problem. The fix is still to free real space, as in the next bullet, and the reserved-blocks step shows how to lower the reserve as a stopgap.
  • Use% near 100 and du close to Used. Ordinary full disk, whether or not you have reached the reserve. Free real space with the du tutorial.

To check another filesystem, give its mount point instead of /, such as df -h /var/lib/postgresql.

Check it worked

You can say which of the four cases you are in, and for which mount point. Write it down before you change anything.

Step 2 · Find space held by deleted files

When you delete a file, Linux removes its name. The data stays on disk until the last process that has the file open closes it. A log file deleted while the service is still writing to it is the classic case: du no longer sees it, df still counts it.

Reproduce it. This makes a 200 MB log and starts a small loop that keeps appending to it, standing in for a service:

Bash
mkdir -p ~/fulldisk-demo
dd if=/dev/zero of=~/fulldisk-demo/app.log bs=1M count=200 status=none
bash -c 'while :; do date; sleep 1; done' >> ~/fulldisk-demo/app.log 2>/dev/null &
echo $! > ~/fulldisk-demo/writer.pid

Now delete the log the way someone in a hurry would:

Bash
rm ~/fulldisk-demo/app.log
ls ~/fulldisk-demo

The file is gone from the listing, but the 200 MB is still in use. lsof +L1 lists open files whose link count is below one, meaning no name points at them any more:

Bash
sudo lsof -nP +L1 | grep fulldisk-demo

You see a line like this:

Text
bash    4127 dev    1w   REG  252,1 209715260     0 393231 /home/dev/fulldisk-demo/app.log (deleted)

Read it left to right: the command (bash), its PID (4127), the file descriptor (1w, number 1, open for writing), the size in bytes, the link count (0) and the old path with (deleted). On a busy server, run sudo lsof -nP +L1 without the grep and look for the largest sizes. You will also see entries such as /memfd:... (deleted) or deleted shared-memory files held by systemd, browsers and other programs; these are normal, usually small, and not worth freeing.

Check it worked

The lsof line shows the deleted log with a size above 200,000,000 bytes and NLINK of 0. df -h ~ shows no space returned.

Step 3 · Free the space by truncating or restarting

You have two ways out. Restarting the owner is the clean one: the process closes the file and the kernel frees it. When you cannot restart (a database mid-job, a service with no maintenance window), you can empty the file through the process's file descriptor instead.

Every open file a process holds appears as a link under /proc/<pid>/fd/. Look at the stand-in service's links:

Bash
PID=$(cat ~/fulldisk-demo/writer.pid)
sudo ls -l /proc/$PID/fd

The entry for the deleted log reads something like 1 -> /home/dev/fulldisk-demo/app.log (deleted). The number before the arrow is the descriptor, the same number lsof showed as 1w. On your own server, take both numbers from the lsof line.

This empties the file the process is writing to. Do it only for a file you have identified as a log or other disposable output, never for a database or data file, whose contents would be lost:

Bash
FD=1
sudo truncate -s 0 /proc/$PID/fd/$FD

The demo loop opened the file with >> (append mode), so its next write goes to the new end of the file at offset zero. A program that does not use append mode keeps writing at its old offset, which leaves a sparse file: ls reports the old size but almost no blocks are used. That is harmless, and the restart below fixes it properly.

For a real service, the clean fix is a restart. Find its systemd unit from the PID, then restart it:

Bash
ps -o unit= -p "$PID"
sudo systemctl restart <unit-name>

Replace <unit-name> with the unit ps printed, such as myapp.service. The demo loop is not a service, so stop it directly:

Bash
kill "$PID"

To stop this happening again, find out why the file was deleted under a running process. The usual causes are a hand-run rm on a live log, or a log rotation rule that moves the file without telling the program to reopen it. Use copytruncate or a postrotate reload in the app's file under /etc/logrotate.d/.

Check it worked

The deleted file no longer appears:

Bash
sudo lsof -nP +L1 | grep fulldisk-demo || echo "nothing held open"

Within a second or two after you stop the loop (its last sleep also holds the file until it ends), the output is nothing held open. The 200 MB itself came back at the truncate, not here; running df -h ~ now shows the same space available as just after that command.

Step 4 · Find what has used up the inodes

Every file and directory takes one inode. On ext4 the number of inodes is fixed when the filesystem is created, so millions of tiny files (session files, cache entries, mail queue items, unrotated per-request logs) can use them all while gigabytes of space remain. Then every attempt to create a file fails with No space left on device.

Make a directory with 20,000 empty files, as a runaway session store would:

Bash
mkdir -p ~/fulldisk-demo/sessions
cd ~/fulldisk-demo/sessions
seq 1 20000 | sed 's/^/sess_/' | xargs touch
cd ~

du --inodes counts files and directories instead of bytes. Rank the folders one level down:

Bash
du --inodes -x --max-depth=1 ~/fulldisk-demo | sort -n

The last two lines show sessions with 20001 entries (the files plus the directory itself) and the total for ~/fulldisk-demo. On your own server, start broad and work down a level at a time:

Bash
sudo du --inodes -x --max-depth=1 / 2>/dev/null | sort -n | tail -n 5
sudo du --inodes -x --max-depth=1 /var 2>/dev/null | sort -n | tail -n 5

Check it worked

You have a single directory whose count dwarfs the others. Before you remove anything, also check what owns it (ls -ld <dir>) and which application writes there.

Step 5 · Clear old files by age, not by guesswork

Files in a session or cache directory are usually safe to remove once they are older than the application's own expiry. Pretend just over half of the demo files are old by giving them a January timestamp:

Bash
find ~/fulldisk-demo/sessions -name 'sess_1*' -exec touch -t 202601010000 {} +

Count before you delete. This lists how many files are older than seven days:

Bash
find ~/fulldisk-demo/sessions -type f -mtime +7 | wc -l

It prints 11111. Change the directory and the age to match your own application, and check the number makes sense, before you run the next command. It deletes every file it matched and there is no undo:

Bash
find ~/fulldisk-demo/sessions -type f -mtime +7 -delete

On a real directory with millions of files, find ... -delete is the right tool: rm sess_* fails with Argument list too long, and ls alone can take minutes.

Clearing files buys time. The real fix is in the application: turn on its own expiry or garbage collection, or schedule the same find with a timer. If a data volume genuinely needs millions of small files, format the next one with more inodes, which the next step shows.

Check it worked

Bash
find ~/fulldisk-demo/sessions -type f | wc -l

It prints 8889. On your server, df -i on that mount shows IUse% well below 100 and services can create files again.

Step 6 · Read and tune ext4 reserved blocks

By default ext4 keeps 5 per cent of its blocks for root. Normal users and services that run as normal users cannot use them, and df leaves them out of Avail. The reserve lets root-run services and an administrator keep working when the disk fills, and it limits fragmentation.

Work on a 1 GiB ext4 image file, so nothing touches a real disk:

Bash
truncate -s 1G ~/fulldisk-demo/fs.img
mkfs.ext4 -q -F ~/fulldisk-demo/fs.img
tune2fs -l ~/fulldisk-demo/fs.img | grep -E '^(Block count|Reserved block count|Block size|Inode count):'

You see something like this:

Text
Inode count:              65536
Block count:              262144
Reserved block count:     13107
Block size:               4096

13,107 blocks of 4 KiB is about 51 MiB, 5 per cent of the filesystem. On a 2 TB data volume the same 5 per cent is 100 GB that your application cannot use.

Lower the reserve with -m, which takes a percentage. tune2fs can change this on a mounted filesystem:

Bash
tune2fs -m 1 ~/fulldisk-demo/fs.img
tune2fs -l ~/fulldisk-demo/fs.img | grep '^Reserved block count:'

The count drops to about 2,621.

On your server, find the device behind a mount point and read its values:

Bash
DEV=$(findmnt -no SOURCE /)
sudo tune2fs -l "$DEV" | grep -E '^(Block count|Reserved block count|Block size)'

Guidelines for changing it:

  • Root filesystem: leave it at 5 per cent, or 2 to 3 per cent on a large disk. Do not set it to 0. The reserve is what lets root-run system services and you keep working on a full disk.
  • Large data-only volume: 1 per cent is a common choice, with sudo tune2fs -m 1 "$DEV".
  • Emergency: lowering the reserve frees space at once, which can let a stuck service write again. Treat it as a stopgap and put the value back after you have fixed the cause.

This applies to ext4 only. XFS has no root-only reserve to tune this way, and tune2fs does not work on it.

The same image shows why inodes run out on ext4. Inode count is 65,536: one inode for every 16 KiB of space, set when the filesystem was made. To give a new volume four times as many, pass a smaller bytes-per-inode value to mkfs.ext4:

Bash
truncate -s 1G ~/fulldisk-demo/small-files.img
mkfs.ext4 -q -F -i 4096 ~/fulldisk-demo/small-files.img
tune2fs -l ~/fulldisk-demo/small-files.img | grep '^Inode count:'

It prints a line like Inode count: 262144. Existing ext4 filesystems cannot be given more inodes without reformatting.

Check it worked

Reserved block count on fs.img went from about 13,107 to about 2,621, and small-files.img has four times the inodes of fs.img.

Step 7 · Write a read-only triage script

During an outage you want every check in one command that changes nothing. Save this script:

Bash
cat > ~/fulldisk-demo/disk-triage.sh <<'EOF'
#!/usr/bin/env bash
# Report why a filesystem is full. Reads only; deletes nothing.
# Usage: sudo bash disk-triage.sh [path]   (default /)
set -euo pipefail

target=${1:-/}
mnt=$(findmnt -no TARGET -T "$target")
src=$(findmnt -no SOURCE -T "$target")
fstype=$(findmnt -no FSTYPE -T "$target")

echo "== $mnt ($src, $fstype)"
echo "-- space"
df -h "$mnt"
echo "-- inodes"
df -i "$mnt"

echo "-- deleted files still open, largest first (bytes, command, pid, fd)"
{ lsof -nP +L1 2>/dev/null || true; } |
  awk 'NR > 1 && $8 == 0 { printf "%14s  %s pid=%s fd=%s\n", $7, $1, $2, $4 }' |
  sort -rn | head -n 10

if [ "$fstype" = ext4 ]; then
  echo "-- ext4 reserved blocks"
  tune2fs -l "$src" | grep -E '^(Block count|Reserved block count|Block size):'
fi

echo "-- regenerable space"
if command -v journalctl >/dev/null; then journalctl --disk-usage || true; fi
du -sh /var/cache/apt/archives 2>/dev/null || true
EOF

It prints the space and inode use for the filesystem holding the path you give, the ten largest deleted-but-open files on the system, the ext4 reserve when that applies, and two things you can usually clear safely: the journal and the apt package cache.

Run it against the root filesystem:

Bash
sudo bash ~/fulldisk-demo/disk-triage.sh /

Check it worked

The output starts with a == / line naming the device and filesystem type, then the df tables, then any deleted-but-open files. If it prints nothing under the deleted-files heading, no process is holding deleted space. Copy the script to the servers you look after, for example to /usr/local/sbin/disk-triage.

Step 8 · Follow a first-aid order on a server that has stopped writing

When a production disk fills, the pressure is to delete something large straight away. That is how people lose databases. Work in this order and stop as soon as services can write again:

  1. Find out which filesystem and which kind of full. Run the triage script, or df -h and df -i on the path that is failing. Space, inodes and the reserve each need a different fix.
  2. Free deleted-but-open files. Restart the owning service, or truncate its log through /proc/<pid>/fd. This removes nothing that still has a name.
  3. Clear what can be regenerated. Package cache with sudo apt-get clean, and the journal with sudo journalctl --vacuum-size=500M (see keep the journal small).
  4. Find the growth with du or du --inodes, then act on what you can name. Compress or move old logs, remove expired cache or session files by age. If you are not sure what something is, move it to another filesystem rather than deleting it.
  5. On ext4, lower the reserve as a stopgap. tune2fs -m 1 on a data volume buys space at once. Put it back once the cause is fixed.
  6. Grow the disk if the data is legitimate. See grow a cloud server disk.

Never, under pressure: rm an active log (truncate it), delete anything under a database's data directory (such as /var/lib/postgresql or /var/lib/mysql), or run rm -rf on a path you cannot explain.

Clean up the practice folder, including the image files:

Bash
rm -rf ~/fulldisk-demo

Check it worked

ls ~/fulldisk-demo reports that the folder does not exist, and you have the first-aid order and the triage script somewhere you can find at 3 a.m., such as your team's runbook.

Next steps

  • Cap the journal so it never becomes the problem: /tutorials/keep-the-systemd-journal-small-and-persistent
  • Find and signal the process holding a file: /tutorials/manage-processes-with-ps-kill-and-signals
  • Add space when the data is real: /tutorials/grow-a-cloud-server-disk-without-downtime
  • Review the everyday clean-up with du and df: /tutorials/find-what-is-using-disk-space-linux

Need this done for you?

Padding does this work for growing companies, from the first diagnosis to a system that keeps working.

Platform Engineering

Get new tutorials

New tutorials, about twice a month.