Deleted a file with rm but disk space wasn't freed: finding the process holding the deleted file open with lsof
You rm a huge log on Linux and df usage does not drop, or the du total and df don't match. The cause is a process that still has the deleted file open. I reproduced it on a 1 GB ext4 image to check how to find the culprit with lsof +L1 or /proc/PID/fd, then free the space by restarting or truncating, and pull back a file you deleted by mistake.
Contents
An alert says /var is full. I look in the log directory and find a single log several hundred MB in size. I rm it for now and run df -h again, and usage has not dropped by even 1%. Totaling with du -sh shows that hardly any files are left, yet df alone insists the disk is full.
Anyone who works on Linux servers runs into this sooner or later, and it is almost always because some process still has the file you deleted open. In this article I briefly explain why that happens, then cover how to find the process holding the file, how to get the space back (including when you cannot restart it), how to pull out a file you deleted by accident, and how to handle logs so it doesn’t happen again.
The output in this article was taken on Ubuntu 24.04 (lsof 4.95.0), with a 1 GB ext4 image mounted on /mnt/data and a small program that keeps writing a log. The steps to reproduce it yourself are at the end.
1. Reproducing “deleted but not freed”
I start from a state where /mnt/data/log/app.log holds an 800 MB log and an application keeps appending one line per second to it.
$ df -h /mnt/data
Filesystem Size Used Avail Use% Mounted on
/dev/loop0 974M 801M 107M 89% /mnt/data
Delete the log and look at df again.
$ rm /mnt/data/log/app.log
$ df -h /mnt/data
Filesystem Size Used Avail Use% Mounted on
/dev/loop0 974M 801M 107M 89% /mnt/data
$ du -sh /mnt/data
28K /mnt/data
du walks the directories and adds up file sizes, so it does not count a file whose name is gone. df, on the other hand, shows the amount of blocks the file system is managing as “in use”. The 800 MB of blocks have not been freed yet, which is why the two numbers are this far apart.
2. Why it isn’t freed
All rm does is remove the file name from the directory (unlink). The file’s contents and its management information (the inode) are freed only when both of the following are true.
- No name (hard link) pointing to the file is left
- No process has the file open
An application that keeps writing a log opens app.log at startup and from then on writes to the opened file (a file descriptor), not to the name. Even after the name is gone, the application’s writes don’t fail; it keeps quietly appending to a file nobody can see. The disk is not freed until the application exits or closes the file.
A similar thing happens when you rename a log with mv: the application keeps writing not to a new app.log but to the renamed app.log.1. What it holds open is the file itself, not the name.
$ mv /mnt/data/log/app.log /mnt/data/log/app.log.1
$ ls -l /proc/3470/fd/3
l-wx------ 1 root root 64 Nov 26 07:53 /proc/3470/fd/3 -> /mnt/data/log/app.log.1
On servers where log rotation is done with home-made mv and rm, these two combine easily into “it keeps writing to the old log I thought I deleted, and the space isn’t freed either”.
3. Finding the process that has it open
lsof +L1
The +L1 option of lsof shows only files with a link count below 1, that is, files whose names are gone but that are still open. Run it as root or with sudo so that it can also see other users’ processes.
$ sudo lsof +L1
COMMAND PID USER FD TYPE DEVICE SIZE/OFF NLINK NODE NAME
python3 3366 root 3w REG 7,0 838860893 0 14 /mnt/data/log/app.log (deleted)
Here is what to look at.
| Column | Meaning |
|---|---|
COMMAND / PID |
The process holding the file open. Here, python3 with PID 3366 |
FD |
File descriptor number and how it is opened. 3w means number 3, opened for writing |
SIZE/OFF |
File size (bytes). 838,860,893 bytes is about 800 MiB |
NLINK |
Number of names. 0 means it is gone from the directory |
NAME |
The name before it was deleted, followed by (deleted) |
SIZE/OFF tells you which process is holding how much space. When the numbers are too long to read easily, convert them to GiB as in the data unit conversion guide. The M and G in df -h are also powers of 1024.
To narrow it down to one file system, add the mount point. Don’t forget -a when you do. When you list several conditions, lsof selects anything that matches “any of them”, so lsof +L1 /mnt/data shows not just deleted files but every file open on /mnt/data, and current directories too.
$ sudo lsof -a +L1 /mnt/data
COMMAND PID USER FD TYPE DEVICE SIZE/OFF NLINK NODE NAME
python3 3366 root 3w REG 7,0 838860893 0 14 /mnt/data/log/app.log (deleted)
The lsof | grep deleted found in older material also works, but it lists every open file of every process before filtering, so it is slow on servers with many processes.
Without lsof: /proc/PID/fd
Minimal servers and containers sometimes don’t have lsof. In that case, look at /proc directly. Each process’s open files are listed as symbolic links in /proc/<PID>/fd/, and for deleted ones the link target ends with (deleted).
$ sudo find /proc/[0-9]*/fd -ls 2>/dev/null | grep '(deleted)'
6556 0 l-wx------ 1 root root 64 Nov 26 07:52 /proc/3366/fd/3 -> /mnt/data/log/app.log\ (deleted)
To get the size, follow the link with stat -L.
$ sudo stat -L -c '%s bytes, %h links' /proc/3366/fd/3
838861017 bytes, 0 links
It is a little larger than the lsof figure earlier because the application kept appending in the meantime.
This list can also include things that don’t use the disk, such as files in /dev/shm. If you are chasing a space problem, look at the paths and deal only with those on the file system that is currently full.
4. Getting the space back
The basic fix is to restart the process
The surest way is to restart the process holding the file open. When the process exits, the file is closed, and the blocks are freed at that moment. For a service running under systemd, find the unit name from the PID that lsof gave you and restart it.
systemctl status 3366 # find which service the PID belongs to
sudo systemctl restart <service-name>
Some software can reopen its log files without a restart. For example, nginx reopens its log files with nginx -s reopen (the USR1 signal). What it reopens, though, is the log path in its configuration, so a new app.log is created in place of the file whose name is already gone, and the deleted file is closed.
Watch out for file descriptors inherited by child processes. When a shell script’s output is redirected, child processes started by that script, such as sleep, keep the same file open too. If stopping only the parent doesn’t free the space, run lsof +L1 again and check that no process is left.
When you can’t stop it, empty the contents
When you can’t restart the production process right now but the disk is about to overflow, you can truncate the file to 0 bytes through /proc.
$ sudo sh -c ': > /proc/3366/fd/3'
$ df -h /mnt/data
Filesystem Size Used Avail Use% Mounted on
/dev/loop0 974M 32K 907M 1% /mnt/data
: is a command that does nothing, and the > redirection truncates the file to 0 bytes. truncate -s 0 /proc/3366/fd/3 does the same thing. Writing sudo : > ... would make the redirection happen outside sudo and fail with a permission error, so I wrapped it in sh -c. The process keeps running and the file stays open, so this is a stopgap to get space back for now; the real fix is to restart the process later.
What happens after truncating depends on how the application opened the file.
- Opened in append mode (
O_APPEND,'a'in Python,>>in the shell): the next write starts at the beginning of the file. In my test, the file was 93 bytes (3 lines) 3 seconds after truncating. - Opened in overwrite mode (
'w'in Python,>in the shell): the process keeps writing from where it was before the truncation, so the file’s apparent size soon goes back up. A file truncated after 300 MB had been written showed as 314,572,986 bytes instat, but it actually used only 8 blocks (4 KB in 512-byte units). It is a sparse file whose beginning is a “hole”, and asdfsees it, the free space has come back.
In the latter case, ls -l and the SIZE/OFF in lsof stay large, so it is easy to think “the truncation didn’t work”. Judge whether it worked by df.
Truncating throws the contents away, so if you need that log to investigate an incident, pull it out first with the method in the next section.
5. Pulling out a file you deleted by accident
The same mechanism can rescue a file you rm’d by mistake. While a process still has it open, reading /proc/<PID>/fd/<number> gives you the contents as they are, so you can cp them back into a file with a name.
$ sudo cp /proc/3536/fd/3 /root/app.log.recovered
$ sudo head -2 /root/app.log.recovered
2026-11-26 07:54:58 request ok
2026-11-26 07:54:59 request ok
You get the contents up to the moment of the copy. If the process keeps running, anything appended after that is not in the copy. And the moment the process exits, this trick stops working, so rescue first and restart afterward. Copy to somewhere other than the full file system. Copy 800 MB to the same place and this time the copy fails for lack of space.
6. Keeping it from happening again
Deleting logs with rm when they grow large is the most common way into this problem. If you want to empty a log the application is in the middle of writing, don’t delete the file; truncate only its contents.
sudo truncate -s 0 /var/log/app/app.log
The file stays the same and becomes 0 bytes, so the application can keep writing and the space is freed right away (for an application that opened it in overwrite mode, it becomes a sparse file, as in the previous section).
For cleaning up old logs regularly, leave it to logrotate. logrotate has two approaches: having the application reopen its log, and not asking anything of the application.
/var/log/app/*.log {
daily
rotate 7
compress
delaycompress
missingok
notifempty
create 0640 app app
sharedscripts
postrotate
systemctl reload app > /dev/null 2>&1 || true
endscript
}
create renames the old log and creates a new empty one, and postrotate makes the application reopen its log. As we saw in section 2, renaming alone leaves the application writing to the old file, so this approach depends on having a way to make it reopen (reload or a signal). Whether systemctl reload reopens logs depends on the application, so check that software’s documentation.
If the application has no way to reopen its log, specify copytruncate instead of create and postrotate. It copies the log and then truncates the original, so the application keeps writing to the same file without knowing anything happened. However, lines written between the copy and the truncation can be lost, as the logrotate manual also says. For logs where nothing may be dropped, choose the reopening approach.
7. When df and du disagree but there are no deleted files
If lsof +L1 shows nothing but there is still a large gap between df and du, suspect another cause. These two are common.
Files hidden under a mount. For example, if logs were written to the empty directory /mnt/data/log/ before a disk was mounted on /mnt/data, those files become invisible after the mount. du can’t reach them, yet the space on the underlying file system (here /) is still used. Bind-mount the root file system at a second location and the hidden files become visible.
$ sudo mkdir -p /mnt/rootview
$ sudo mount --bind / /mnt/rootview
$ ls -lh /mnt/rootview/mnt/data/log/
total 50M
-rw-r--r-- 1 root root 50M Nov 26 07:53 old.log
$ sudo umount /mnt/rootview
ext4 reserved blocks. By default ext4 reserves 5% of the total for root, and ordinary users can’t use that portion. On the 1 GB image used here, Reserved block count in tune2fs -l was 13,107 blocks (about 51 MiB in 4 KB units). This is why Size minus Used in df does not equal Avail, and why root can still write a little even when Use% is 100%.
Also, running out of inodes rather than space (blocks) gives “No space left on device” too. For directories with huge numbers of small files, check df -i as well.
Reproducing it yourself
Try “deleted but not freed” on a 1 GB ext4 image (Linux, root)
This uses a loopback device, so run it as root. The working image file is created in /tmp.
# Create a 1 GB ext4 image and mount it
truncate -s 1G /tmp/demo.img
mkfs.ext4 -q /tmp/demo.img
mkdir -p /mnt/data
mount -o loop /tmp/demo.img /mnt/data
mkdir /mnt/data/log
# A program that keeps writing a log (append mode)
cat > /tmp/writer.py <<'EOF'
import time
with open('/mnt/data/log/app.log', 'a') as f:
while True:
f.write(time.strftime('%F %T') + ' request ok\n')
f.flush()
time.sleep(1)
EOF
# Prepare an 800 MB log and start writing
dd if=/dev/zero of=/mnt/data/log/app.log bs=1M count=800 status=none
nohup python3 /tmp/writer.py >/dev/null 2>&1 &
df -h /mnt/data # Used is around 801M
rm /mnt/data/log/app.log
df -h /mnt/data # unchanged
lsof -a +L1 /mnt/data # python3 has the (deleted) file open
kill %1 # end the process and...
df -h /mnt/data # the space is freed
# Clean up
umount /mnt/data
rm /tmp/demo.img /tmp/writer.py
Instead of kill, you can also truncate with : > /proc/<PID>/fd/3 or pull the file out with cp /proc/<PID>/fd/3 /tmp/recovered.log to try what sections 4 and 5 describe.
Before deleting, look at who has it open
The closer the disk is to overflowing, the more you want to rm the largest files first. But if the file is a log being written to, rm doesn’t free the space; it only hides a log you could have used to investigate the incident. If you check for processes holding it open with sudo lsof <file> before deleting, you can decide on the spot whether to truncate or to arrange a restart first.
If you have already deleted it, find the culprit with lsof +L1, rescue the contents from /proc if you need them, and then get the space back by restarting or truncating. Remember this order and, even while chasing a disk-full alert, you won’t lose the log you deleted too.
If you need to start by finding which directory the large files are in, I covered narrowing down targets with find in Searching for strings across directories (in Japanese).