Why Deleted Files Don't Free Disk Space on Linux
This article explains why disk space often stays occupied after a file is deleted on Linux. It breaks down the directory entry, inode, and data block model, shows how link counts and open file descriptors keep deleted files alive, and covers the classic deleted-log-file scenario. You will learn how to find the processes holding deleted files, how to decide whether a restart is safe, and how to prevent the issue with log rotation and monitoring.
1. The Symptom: Deletion Succeeds, but Space Stays Put
A common source of confusion in Linux operations and day-to-day development is this: you run a delete command, it returns success, the file is gone when you list the directory, yet disk usage barely changes. The first instinct is often “the delete failed” or “the filesystem is broken,” followed by repeated deletions, reboots, and sometimes even plans to reformat.
In most cases, the deletion itself did succeed. The real reason is that on Linux, a file’s name and the file’s data are two separate concepts, and removing the name does not immediately destroy the data. As long as a process still “holds” the file open, the kernel keeps its data blocks, so the space cannot be reclaimed.
Understanding this is the starting point for troubleshooting.
2. How It Works: Directory Entries, Inodes, and Link Counts
To understand why space is not released, you need to go back to the three-layer structure of a Linux filesystem:
- Directory entry: essentially a mapping from file names to numbers. A directory is itself a special file that records a set of “name-to-number” associations.
- inode: stores the file’s metadata, such as permissions, ownership, timestamps, and data block locations. It does not contain the file name.
- Data blocks: where the file’s actual bytes live.
The core action of deleting a file is removing that mapping from the directory and decrementing the inode’s link count. This introduces a key field, the link count:
- Every time a name is created pointing to the same inode, the link count increases by one.
- Deleting a name decreases the link count by one.
- Only when the link count reaches zero and no process holds an open handle to the file will the kernel actually free the inode and data blocks.
This explains a frequently overlooked fact: a file can exist without having a name. Once a process opens a file, even if the path is deleted, the process can keep reading and writing the original content through the file descriptor it already holds, and the data blocks are not reclaimed.
The process looks roughly like this:
Create file -> directory entry exists, link count = 1Process opens -> holds file descriptor fdDelete name -> directory entry gone, link count = 0-> but fd is still valid, data blocks retainedProcess closes fd -> data blocks truly freed, space reclaimed
3. The Most Typical Case: Delete-Style Log Rotation
Logs are the easiest trigger for this problem. A service keeps appending to a log file, and an operator deletes the file directly to “free up space.” The result:
- The service’s internal write pointer still targets the original inode, so writes keep landing on that now-nameless data.
- New log output does not automatically switch to a new file.
- The old log is invisible in the directory, yet disk usage stays high or keeps growing.
The essence of this problem is that the writer never reopened the file, so the handle was never released. The correct approach is usually not a direct delete, but a rotation strategy of “rename, signal the service to reopen the file, then clean up the old file,” or truncating the file to reclaim its content while keeping the inode. If the service supports a signal or interface for reopening logs, prefer that; if it does not, the only option is often to restart the service to release the handle.
4. General Troubleshooting: Find Who Is Holding the Deleted File
The goal is clear: identify the processes that still hold handles to deleted files. There are two common approaches.
The first is to use a system tool to list files with a link count of zero that are still open. The output marks the file as deleted and shows the space in use, the link count, and the owning process. To narrow the scope, limit the query to a specific mount point to avoid the cost of a full-disk scan:
# List deleted files that are still held open (illustrative)sudo <handle-checking tool> +L1# Limit the check to a specific mount point (illustrative)sudo <handle-checking tool> +L1 /var
In the output, focus on three things: the space in use, the link count (0 means the directory entry is gone), and the path marked as deleted at the end.
The second approach, when no dedicated tool is available, is to walk the process handle directories directly. Each process exposes its open files as symbolic links under its fd directory, and links to deleted files carry a “deleted” marker:
# View a process's open handles (illustrative)sudo ls -l /proc/<PID>/fd
Replace <PID> with the actual process ID. If a line points to the target file and is marked deleted, you have confirmed that the space is held by that process. Checking processes one by one is tedious, so filter by process name to narrow down the suspect service before digging deeper.
5. Handling Principles: Decide Whether a Restart Is Possible First
Once you have located the process, do not rush to kill everything. The strategy should be based on whether the process can be safely restarted:
- Ordinary applications that can be restarted: stop them gracefully and start them again; the space is reclaimed immediately once the handle is released. This is the cleanest approach.
- Core services that cannot be restarted freely: prefer their supported log-reopen mechanism; if a restart is unavoidable, follow a proper change process to avoid impacting the business.
- Genuinely hung or abnormal processes: terminate them after assessing the impact to release the handle.
- Multiple processes sharing the same file: all processes holding handles must be dealt with; space is reclaimed only after the last one closes.
It is worth stressing that you should not try to solve this by “deleting more files,” which only creates more nameless usage and makes troubleshooting harder.
6. Prevention and Practices: Avoid the Problem at the Source
- Rotate logs instead of deleting them: manage the log lifecycle through rename and reopen mechanisms, rolling over by time and size.
- Decouple the writer from the cleanup process: the cleanup strategy should notify the writer rather than deleting files unilaterally.
- Set up monitoring and alerts: configure threshold alerts for disk usage and inode usage so you can step in as soon as something looks abnormal. In cloud environments, you can combine monitoring, alerting, and log services for centralized management.
- Pay extra attention in container environments: a file deleted inside a container is not released either if a process in the container holds it; troubleshoot from the process-handle angle rather than only looking at the container file layer.
- Keep good operational habits: before cleaning up, confirm whether any process is using the file, and make the handle check a standard operations step.
7. Summary
“Deletion succeeds but space is not released” on Linux is not a failed delete. It is the inevitable result of a mechanism where the file name and the data are separate, and the data’s lifecycle is determined jointly by the link count and open handles. Once you grasp the three-layer model of directory entry, inode, and data block, and understand the roles of link counts and file descriptors, you can quickly find the root cause when facing full logs or handle leaks. When handling such issues, use “can it be restarted” as the decision criterion, prefer rotation and reopen mechanisms, and back them with monitoring alerts and standard processes, and disk space reclamation becomes controllable and predictable.
