David - Musings of an SRE

Deleting files and getting your diskspace back without rebooting

When you run out of diskspaces and need to delete files quickly to recover them, most of the time, your deleted files will not free up the diskspace until the process that is using it is restarted or deleted.

To force the filesystem to free up the lock to the file so that the files can be cleared up, you’d need to find the process that is using the file and truncate it.

Finding and deleting the file

First, find the process PID that is using it and look out for deleted files that the process has a lock on.

$ ls -l /proc/<PID>/fd

lr-x------ 1 tmp tmp 64 lut  1 00:06 3 -> /home/tmp/x (deleted)

Once you’ve found out which is the fd that the deleted file is linked to, in the above example its, 3, you can immediately truncate it to release the file lock and allow the filesystem to release the diskspace.

$ :>/proc/<PID>/fd/3

You should immediately see your file diskspace released after the above command.


Note: It should be emphasized that this might cause some unintended consequences to your running process. Please use this with caution.


References