Deleting Files Doesn’t Free Up Space on Linux

PowerADM.com / Linux / Deleting Files Doesn’t Free Up Space on Linux

In Linux, you may sometimes find that after deleting a file from a disk, no free space is reclaimed. This is because when a particular file is being used by a running process, the file descriptor is not freed until the process releases it.

Let’s look at what to do when you need to free up disk space after deleting a file on a Linux host. In our example, there is almost no free space left on the partition:

# df -h

df - no fre space left on Linux

Find the 10 largest files in Linux with the command:

# du -ahx / | sort -rh | head -10

Linux find largest files

For example, you have determined that the largest file you can delete right now is /var/log/mysql/error.log. Delete it:

# rm -rf /var/log/mysql/error.log

However, the used space is not reclaimed after the file is deleted. The file is simply marked as deleted, but is not actually removed from a disk.

Let’s find out which process is using the deleted file:

# lsof | grep deleted | grep error.log

free space not being released after deleting files

The mariadb service uses this file. Because this daemon serves multiple production DBs, it cannot be stopped or restarted at this time.

Get the file descriptor of the deleted file by the process ID (PID) you received (4441 in this case):

# ls -l /proc/4441/fd | grep error.log

In this example, 200 is a file descriptor.

To free space on a partition, empty the contents of a file by specifying the process PID and the file descriptor in the command:

# > /proc/4441/fd/200

Check for free disk space.

In general, it is better to make it a rule (especially for log files) that it is better to clear the file content rather than delete it:

# truncate -s 0 /var/log/mysql/error.log

The target file will be truncated to zero size without deleting it.

Leave a Reply

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