Linux – How to reclaim storage of “deleted”, but still used files on Linux

deleted-filesfileslinux

SERVER:~ # df -mP /home/
Filesystem         1048576-blocks      Used Available Capacity Mounted on
/dev/mapper/rootvg-home_lv       496       491         0     100% /home
SERVER:~ # 
SERVER:/home # lsof | grep -i deleted | grep -i "home" | grep home
badprocess   4315     root  135u   REG      253,2   133525523      61982 /home/username/tr5J6fRJ (deleted)
badprocess2  44654     root  133u   REG      253,2   144352676      61983 /home/username/rr2sxv4L (deleted)
...
SERVER:/home # 

Files were deleted while they were still in use. So they still consume space. But we don't want to restart the "badprocess*". OS is SLES9, but we are asking this "in general".

Question: How can we remove these already deleted files without restarting the process that holds them, so the space would free up?

Best Answer

You can use the entries in /proc to truncate such files.

# ls -l /proc/4315/fd

That will show all the files opened by process 4315. You've already used lsof and that shows that the deleted file is file descriptor 135, so you can free the space used by that deleted file as follows:

# > /proc/4315/fd/135

The same goes for the other deleted file opened by process 44654, there it's file descriptor 133, so:

# > /proc/44654/fd/133

You should now see that the space is freed up.

You can also use this to copy the contents of a file that's been deleted but still held open by a process, just cp /proc/XXX/fd/YY /some/other/place/filename.

Related Question