Find and recover space used by deleted files, shown by df but not by du

When you’re running low on space on a file system, that can cause various unexpected behavior of the system, depending on which file system is filling up. For me, when that happens, I usually first issue a disk free (df) to see which is the file system that is almost full. Once I know which file system, I go and search which files take up the most space in that file system and take action. Sometimes, df show that a file system is almost full while, when summing up all the space by all files doesn’t even come near that value.

When the space that is reported by df is different then what you see whit du, it’s usually caused by a file that was deleted but wasn’t release by a certain process. This means that when you would reboot the system or stop the process that’s holding the deleted file, the space would be released. This sounds very easy and it is but often you can’t just reboot the machine or even restart the offending process because that service should be always there or just can’t be missed at that moment.

When you can’t stop the process holding the file, one option could be to grow the file system using LVM but at a certain moment, your space is all used and you can’t keep growing your file system. Especially when using XFS, this is not recommended since you can’t shrink after expanding.

The best solution, which can be used in most of the cases, is to go and find which file is still kept on the file system, but marked deleted, and to delete the contents of the file without removing the file descriptor. Be warned that some processes write data to a deleted file on purpose so overwriting the contents of that file could potentially cause problems for the process owning the file descriptor.

Simulate the problemand create a difference between df and du

First, let’s have a look how the situation, which is the initial problem, looks like. To simulate a process holding a file while it is deleted, I’ll create a small C program that opens a file for reading and then waits infinite.

To be able to compile the source of our program, we need a compiler:

For RHEL, CentOS and derivatives::

For Debian, Ubuntu and dderivatives

Next, we need to create a file, lets say openfile.c, that contains the source of the small program. The program simply opens the file that is given as an argument, throws and error if there was a problem open it, and then just goes into an infinite loop until the program is stopped.

After creating the source file openfile.c, we need to compile it with our just-installed compiler: (the command is the same for CentOS and Debian):

The output of the above command is the executable file openfile. Now that we have our test-program, I’ll create a ~800MB file that will fill up my /var filesystem:

Now that we have the file, I’ll let it be opened in the openfile program and check the status of the /var file system:

When we check where the space is used on the /var file system with du, we see what we would expect:

When we delete the file now, it will still be in use by our C-program but we won’t be able to see where the space has been used with du:

As you can see, after deleting the file, the space hasn’t been freed up since df is still showing that the /var file system is 95% full. Usually at this point is where you get alerted and when checking with du, we see that it reports that only 145MB of file exist in /var.

Investigate the problem and find the process which is keeping deleted files

Now that we, finally, simulated the situation where df and du are reporting different value, I can show how we can find which process is holding the space.

A minimal RHEL or CentOS installation doesn’t normally contain the lsof program, which is a key in solving this problem so we’ll have to install it. For Debian this step shouldn’t be needed.

Now let’s search for processes that keep files open which are marked as deleted:

[jensd@cen1 ~]$ lsof|grep deleted
openfile 3183 jensd 3r REG 253,2 800000000 3592 /var/tmp/testfile (deleted)

As you see, lsof reports us that openfile is keeping a file with name /var/tmp/testfile that is about 800MB in size and is held open by a process openfile with pid 3183.

Now that we know which process it is, we can determine if we could stop the process or not to free up the space. In case you can safely stop or restart the process, the space should be freed.

Solve the problem and free up the space without stopping the process

In case you can’t stop the program that is holding up the space, there is another “trick” to free up the space. As I warned before, this could potentially break things in the process or make you loose data. In some cases, this is the only option because with a full file system, the system will have general problems and there is a big chance that the process keeps running fine.

In the above step, we got to know the pid of the process holding the file. With that information, we can try to find the file descriptor for the deleted file in the /proc file system:

In the above command, you need to replace 3183 with the reported pid from lsof. In the output we can see that the deleted file is represented by file descriptor 3.

To free up the space, we can simply write null to the file descriptor. This makes the size of the file that is kept 0 and the space will be freed without stopping the process:

The first command write null to file descriptor 3 of process with pid 3183. The second command shows that after that action, the process didn’t stop while taking the first action. The third command shows that our space is free up, just as we wanted.

As said before this “trick” should be used as a last resort but it can save you in some cirtical situations in an environment that can’t afford downtime.

2 thoughts on “Find and recover space used by deleted files, shown by df but not by du

Leave a Reply

Your email address will not be published. Required fields are marked *