Sub topic 8
When df and du disagree: deleted files still held open
This chapter explains why df can report a full filesystem while du accounts for far less data. It shows how to find the cause and how to release the space without breaking services. It assumes you can use a Linux shell over SSH, understand files, directories and permissions, use sudo, and read command output. You should also know the basics of df, df -i and du from the earlier chapters.
The symptom
You run df -h and see a filesystem at 98% full. You then run du on the mount point and add up the results. The total is 12 GB, but df says 90 GB is in use.
df -hT /var
sudo du -xsh /varThe two commands measure different things:
dfasks the filesystem how many blocks are allocated. This number comes from the filesystem's own counters.duwalks the directory tree, reads each file's metadata, and adds up what it finds.
A file that has been deleted has no directory entry, so du cannot see it. If a process still has the file open, the filesystem keeps its blocks allocated, so df still counts them. The gap between the two numbers is the space held by deleted-but-open files.
How a deleted file can keep its space
A filename is a directory entry that points to an inode. The inode records the file's size, owner and block locations. Two counts control when an inode is freed:
- The link count is the number of directory entries that point to the inode.
- The open count is the number of processes that currently have the file open.
Running rm removes a directory entry and lowers the link count. The filesystem frees the inode and its blocks only when the link count is zero and no process has the file open. If a process still holds the file, the data stays on disk until that process closes it or exits.
The diagram below shows the sequence.
This is common with logs. An administrator deletes a large log to free space, but the daemon that writes to it keeps its file handle. The daemon keeps appending to a file nobody can see, and df does not improve. The file can even keep growing.
Finding the deleted files
Use lsof with +L1. This option selects open files whose link count is less than one.
sudo lsof -nP +L1The output looks like this:
COMMAND PID USER FD TYPE DEVICE SIZE/OFF NLINK NODE NAME
nginx 1423 root 5w REG 253,0 83886080000 0 524301 /var/log/nginx/access.log (deleted)The important columns are:
| Column | Meaning |
|---|---|
| COMMAND and PID | The process that holds the file open |
| FD | The file descriptor number. A trailing `w` means open for writing, `r` for reading, `u` for both |
| SIZE/OFF | The file size in bytes, which is how much space it holds |
| NLINK | The link count. It is 0 for a deleted file |
| NAME | The old path, followed by `(deleted)` |
Check the SIZE/OFF value against the gap between df and du. If one file accounts for most of the difference, you have found the cause.
Run lsof with sudo. Without it you see only your own processes, and you can miss the one that matters.
Filtering by filesystem
A server can have many deleted files on different mounts. To look at one filesystem, pass the mount point to lsof:
sudo lsof -nP +L1 /varWhen you give lsof a mount point, it lists files on that filesystem. This keeps the output relevant to the mount you are investigating.
Using /proc when lsof is missing
Each process has a directory of file descriptors under /proc. Deleted files show up as symbolic links that end in (deleted).
sudo ls -l /proc/*/fd 2>/dev/null | grep '(deleted)'This output does not show the PID on each line, but ls -l /proc/<pid>/fd for a suspect process does. Use lsof when it is installed, because it reports the process name and size directly.
Releasing the space
You have two safe options. Choose based on whether the process can be restarted or reloaded.
Option 1: restart or reload the process
When the process exits or closes its handle, the filesystem frees the blocks. Many daemons reopen their log files on a signal or a reload:
sudo systemctl reload nginxA reload is the gentlest choice. It keeps connections alive and makes the daemon open a fresh log file. If the service does not support reload, a restart works but interrupts clients. Plan a restart for a quiet period on a production server.
After the process closes the file, confirm that the space came back:
df -h /var
sudo lsof -nP +L1 /varOption 2: truncate the file through /proc
If you cannot restart the process, you can empty the deleted file through its descriptor. Use the PID and FD from the lsof output. In the example above, PID 1423 and FD 5:
sudo truncate -s 0 /proc/1423/fd/5This frees the blocks immediately while the process keeps running. It destroys the data in that file. Use it only when you are sure you do not need the contents. Also check that the process handles truncation well. A process that writes at a stored offset leaves a sparse file, which has a large apparent size but few allocated blocks, and the file can look large again after the next write.
Avoiding the problem
The cause is nearly always deleting a file that is still in use. For logs, truncate the file in place instead of removing it:
sudo truncate -s 0 /var/log/myapp/app.logTruncation sets the size to zero but keeps the same inode, so the process continues to write to a file that is visible and counted. Better still, let logrotate manage rotation. The next chapter covers it.
Other reasons for a gap
Deleted-but-open files are the most common cause, but not the only one. If lsof +L1 shows nothing, check these:
- Data hidden under a mount point. You wrote files into
/databefore mounting another filesystem on it. The mount hides those files, soducannot see them, but they still use space on the parent filesystem. Unmount the upper filesystem, or bind-mount the parent to look underneath:
``bash sudo mkdir -p /mnt/rootview sudo mount --bind / /mnt/rootview sudo du -xsh /mnt/rootview/data sudo umount /mnt/rootview ``
- Permissions.
durun withoutsudoskips directories it cannot read and prints errors that are easy to miss when you redirect them to/dev/null. Always compare againstsudo du. - Filesystem overhead. Journals, metadata and reserved blocks are counted by
dfbut are not files. On ext4 they can account for a few percent of the size. - Snapshots and subvolumes. On Btrfs, snapshots hold blocks that the live tree no longer references. Use the filesystem's own tools to see this.
- Crossing filesystems.
duwithout-xfollows into other mounts, which inflates its total instead of shrinking it. Use-xso the comparison is for one filesystem.
A diagnostic routine
Follow the same order each time:
- Run
df -hTto identify the full filesystem and its mount point. - Run
sudo du -xsh <mount>to measure what is visible. - If the gap is large, run
sudo lsof -nP +L1 <mount>. - If nothing is listed, check for data under mount points and for snapshots.
- Release the space by reloading the process, or truncate through
/procif you must. - Run
dfagain to confirm.
Exercise
df -h /var reports 60 GB used. sudo du -xsh /var reports 9 GB. sudo lsof -nP +L1 /var shows one process, PID 880, holding a deleted file open on FD 7 with a SIZE/OFF of about 51 GB. Explain the gap, and give two ways to free the space.
Show answer
The 51 GB file was deleted, so du cannot see it, but PID 880 still holds it open, so the filesystem keeps its blocks and df counts them. The 9 GB du total plus the 51 GB file roughly equals the 60 GB df figure. To free the space, either reload or restart the process so it closes the file (for example with systemctl reload), or, if the process cannot be restarted, empty the file with sudo truncate -s 0 /proc/880/fd/7. Confirm afterward with df -h /var and sudo lsof -nP +L1 /var.
Exercise
An administrator wants to empty a busy application's log without a restart and without creating this problem again. What command should they use, and why is it better than rm?
Show answer
Use sudo truncate -s 0 /var/log/myapp/app.log. It sets the file size to zero but keeps the same inode and directory entry, so the process's open handle stays valid and the file stays visible to du. rm removes only the directory entry, and the space is not freed while the process keeps the file open.