TIL deleting a file on Linux doesn't free the space if a process still has it open
Disk was at 100%, I deleted a huge log file, and df still said 100%. Confusing for a few minutes.
The file was still held open by the service writing to it, so the space isn't released until that process closes it or restarts. lsof | grep deleted showed it right away.
Truncating the file instead of deleting it, or restarting the service, sorts it out. Anyone else have a "the disk is lying to me" story?
Comments · 4
You can fix it without restarting using
lsof +L1to find the deleted-but-open files. Then truncate via the process's file descriptor in /proc.Restarting the service releases the file handle and frees the space. And configure log rotation so it doesn't happen again.
Why does Linux keep the file around? Is it a reference count on the file itself, so it's only removed when nothing points at it?
Exactly. The file name is just a link, and the data is only freed when the last link and the last open handle are gone.