Reclaiming Disk Space from Open-but-Deleted Files
A Linux file can disappear from every directory listing and still occupy disk space. This happens when a process already has the file open: rm removes the directory entry, but the inode remains until the last file descriptor is closed. The result is a classic incident where df reports a full filesystem while du cannot find the files consuming it. Knowing how to identify and release those hidden blocks is a useful operational skill.
Why df and du disagree
df asks the filesystem how many blocks are allocated. du walks directory entries and sums the sizes of files it can see. If a file has been unlinked but is still open, du cannot find it, while df still counts its blocks.
1df -h /var
2du -xhd1 /var | sort -h | tailThe -x flag keeps du on one filesystem, which avoids confusing results from nested mounts. If df shows 90% usage but du accounts for much less, open-but-deleted files are a likely cause.
Find the process holding the file
The quickest check is lsof with a link count filter. A file with fewer than one hard link has been removed from the namespace:
1sudo lsof +L1A typical result looks like this:
1COMMAND PID USER FD TYPE DEVICE SIZE/OFF NLINK NODE NAME
2java 1842 app 7w REG 253,1 524288000 0 393221 /var/log/app/app.log (deleted)The important fields are PID, FD, SIZE/OFF, and NAME. Here, process 1842 is still holding a 500 MB deleted log through file descriptor 7.
Broaden the output with -nP to skip DNS and port-name lookups:
1sudo lsof +L1 -nPIf lsof is unavailable, the same information can be assembled from /proc:
1for fd in /proc/[0-9]*/fd/*; do
2 target=$(readlink "$fd" 2>/dev/null) || continue
3 case "$target" in
4 *" (deleted)"*)
5 pid=${fd#/proc/}
6 pid=${pid%%/*}
7 cmd=$(tr '\0' ' ' < "/proc/$pid/cmdline" 2>/dev/null)
8 printf '%s\t%s\t%s\n' "$pid" "$cmd" "$target"
9 ;;
10 esac
11done | sort -uRun that as root to inspect every process. The output is tab-separated: PID, command line, and deleted target.
Release the space
There are three common ways to close the last reference. Choose the least disruptive one that fits the service.
| Method | Command pattern | Best for | Risk |
|---|---|---|---|
| Reload or restart | sudo systemctl restart SERVICE | Services that can safely reopen files | Brief interruption |
| Send a reopen signal | sudo kill -USR1 PID | Log-heavy daemons such as nginx | Must be supported by the app |
Truncate via /proc | sudo truncate -s 0 /proc/PID/fd/FD | Immediate recovery without a restart | Existing content is discarded |
For example, nginx reopens its logs when the master process receives USR1:
1pid=$(cat /run/nginx.pid)
2sudo kill -USR1 "$pid"
3sudo lsof +L1 | grep nginxFor a generic systemd service, a restart is simpler:
1sudo systemctl status rsyslog
2sudo systemctl restart rsyslog
3df -h /varWhen a restart is undesirable and the file is only a log, truncate it through the open descriptor:
1pid=1842
2fd=7
3sudo truncate -s 0 "/proc/$pid/fd/$fd"
4df -h /varThe process keeps writing to the same inode, but the inode now has zero length, so the allocated blocks are returned immediately. This is often the fastest emergency fix.
Avoid this trick for files used for random access, databases, or memory-mapped data. Truncating such a file under a running process can corrupt state or cause SIGBUS when mapped regions are accessed. Restart the owning service in those cases.
As a last resort, killing the process also closes its descriptors, but prefer a graceful signal or service restart first:
1sudo systemctl kill -s TERM myapp.serviceReproduce it locally
The following demo creates a 200 MB file, holds it open with tail -f, deletes the directory entry, and then frees the space through /proc:
1truncate -s 200M "$HOME/demo.log"
2tail -f "$HOME/demo.log" >/dev/null &
3holder=$!
4
5df -h "$HOME" | tail -1
6rm "$HOME/demo.log"
7
8lsof -p "$holder" | grep 'demo.log'
9fd=$(lsof -p "$holder" -Fn | awk '
10 /^f/ { f=substr($0,2) }
11 /^n.*demo\.log.*\(deleted\)/ { print f; exit }
12')
13
14if [ -n "$fd" ]; then
15 truncate -s 0 "/proc/$holder/fd/$fd"
16fi
17
18df -h "$HOME" | tail -1
19kill "$holder"The second df should show the space returned. The tail process continues running until killed, but its old log inode is empty.
Prevent the next occurrence
Most cases come from log rotation that renames or deletes a file without telling the application to reopen it. A rotation rule should either recreate the file and signal the daemon, or use copytruncate as a fallback.
A signal-based rotation rule looks like this:
1/var/log/myapp/*.log {
2 daily
3 rotate 14
4 compress
5 missingok
6 notifempty
7 create 0640 myapp adm
8 sharedscripts
9 postrotate
10 systemctl kill -s HUP myapp.service >/dev/null 2>&1 || true
11 endscript
12}copytruncate copies the contents and then truncates the original path, so an application that never reopens its logs keeps writing to the same inode:
1/var/log/myapp/app.log {
2 daily
3 rotate 7
4 copytruncate
5 missingok
6 notifempty
7}Use copytruncate only when the application cannot be signaled. It has a small window where writes can be lost between the copy and the truncate.
Useful habits include:
- Alert when filesystem usage crosses a threshold, not only when it reaches 100%.
- Run
sudo lsof +L1during incidents before restarting services blindly. - Check rotation configs after deployments that change log paths or service names.
- Prefer application-native log reopening over
copytruncate. - Verify reclamation with
dfafter taking action, not just by restarting.
Quick checklist
- Compare
df -hwithdu -xhd1on the affected mount. - Run
sudo lsof +L1to find deleted files that are still open. - Prefer a service reload, reopen signal, or graceful restart.
- If the file is disposable, truncate it via
/proc/PID/fd/FD. - Confirm with
df -h, then fix the rotation or shutdown path that caused it.
Related Articles
This article was generated with AI assistance and reviewed by a human. Corrections are welcome.