Sobes.tech
Senior

Deleted a large file, but disk space was not freed. Why? How to find and fix it?

sobes.tech AI

Answer from AI

If you have deleted a large file but the disk space has not been freed, likely reasons include:

  1. The file is still open by a process. In Unix-like systems, deleting a file removes its name from the filesystem, but if a process holds a file descriptor to it, the space is not freed until the process closes the file.

  2. The deletion occurred in the wrong location or on a different partition.

  3. The filesystem or cache has not updated immediately.


How to find the problem:

  • Use the lsof (list open files) command to find processes holding deleted files:
lsof | grep '(deleted)'

This will show files that have been deleted but are still open by processes.

  • If you find a process holding a file, you can:
    • Restart the process to close the file.
    • Or, if possible, ask the process to close the file programmatically.

Example fix:

# Find the PID of the process holding the deleted file
lsof | grep '(deleted)'

# Restart the process (for example, with PID 1234)
kill -HUP 1234
# or
kill 1234

After this, the disk space will be freed.

If lsof is not installed, you can use fuser:

fuser -v /path/to/deleted/file

In Go applications, log files or temporary files may sometimes remain open, so it is important to close files properly.