If Linux reports that a disk is full even after you delete large files, an application may still have one of those files open. Removing a file with rm deletes its directory entry, but the disk space is not necessarily freed while a process retains an open file descriptor. This commonly happens with logs that are deleted or rotated while a service continues writing to them. To clear an lsof deleted file, first identify the process and file descriptor responsible, then use the safest way to make the process close or reopen the file. This guide explains how to find deleted-but-open files, reclaim their disk space, and avoid disrupting important applications or data.
What Does “Deleted” Mean in lsof?
Linux separates a file’s name in a directory from the underlying file data. When you delete a file, Linux removes its directory entry. If no process is using the file and no other hard links point to it, the filesystem can release its storage. But if a process still has the file open, the data remains available through that process’s file descriptor. The space is generally reclaimed only after the final reference is closed.
The lsof command, short for “list open files,” can show these files with a (deleted) marker in the name column. The file may no longer appear in ls, yet still consume substantial disk space. A common symptom is that df -h shows a nearly full filesystem while du cannot account for all the used space.
Check Whether Deleted Open Files Are the Problem
Start by checking filesystem usage:
df -h
Then look for deleted files that are still open. Run the command with sudo so it can inspect processes owned by other users:
sudo lsof -nP +L1
The +L1 option selects open files with a link count below one, which commonly means they have been unlinked. The -nP options prevent name and port lookups, making the output faster and easier to read. If you prefer, search the output for the deletion marker:
sudo lsof -nP | grep '(deleted)'
The second command is a useful visual check, while +L1 is generally the more direct way to find unlinked open files. If lsof is not installed, install it using your distribution’s package manager, such as sudo apt install lsof on Debian or Ubuntu, or sudo dnf install lsof on Fedora.
Understand the lsof Output
Typical output includes columns such as COMMAND, PID, USER, FD, TYPE, SIZE/OFF, and NAME. For example:
COMMAND PID USER FD TYPE SIZE/OFF NAME
myservice 2480 app 7w REG 4.2G /var/log/myservice.log (deleted)
Here, process ID 2480 has file descriptor 7w open. The number 7 identifies the descriptor; the trailing w indicates that it was opened for writing. The file’s reported size is about 4.2 GB, so it may be responsible for a noticeable amount of space in use.
Do not assume that the largest listed file is safe to modify. Check the process name, owner, file path, and service role. A deleted log file is often recoverable by having its application reopen the log. A deleted database file, application data file, or file used by a critical service requires much greater caution.
Find the Process and Confirm the Disk Impact
Use the PID from lsof to learn more about the process. For example, replace 2480 with the actual PID:
ps -fp 2480
sudo readlink /proc/2480/exe
You can inspect all open descriptors for that process with:
sudo lsof -nP -p 2480
To see the descriptor’s target through the process filesystem, use the descriptor number shown by lsof:
sudo readlink /proc/2480/fd/7
The target may include (deleted). You can also inspect descriptor information with stat, but remember that the path under /proc is a reference to a file held open by the process, not an ordinary path you can find in its original directory.
If you are troubleshooting a full filesystem, compare df -h with a directory-usage check such as sudo du -xhd1 /var. Adjust the directory to match the affected mount point. The -x option keeps du on one filesystem. A large gap between filesystem usage and visible file totals can point to deleted open files, but filesystem metadata, reserved space, snapshots, and mount boundaries can also affect the comparison.
Best Fix: Make the Application Reopen the File
The safest general solution is to have the owning application close the deleted file and open a new one. For a service, use its documented log-reopen mechanism, reload command, or service manager. Some programs respond to a signal such as SIGHUP, but signal behavior varies: in one application it may reopen logs, while in another it may reload configuration or perform a different action. Check the application’s documentation before sending a signal.
If the application does not support reopening the file, arrange a controlled restart during an appropriate maintenance window. A graceful restart lets the program close its file descriptors cleanly. Once the last open reference is closed, the filesystem can reclaim the space. For a systemd-managed service, the usual form is:
sudo systemctl restart service-name
Replace service-name with the correct unit. Confirm that the service can tolerate a restart and check its status afterward:
sudo systemctl status service-name
For production workloads, consider availability, active requests, and any application-specific shutdown procedure before restarting. If the file belongs to a database or another stateful application, follow the vendor’s operational guidance rather than forcing the process to close it.
Emergency Option: Truncate the Open File Descriptor
When a deleted file is a confirmed disposable log and restarting or reopening the service is not currently possible, an administrator may be able to truncate the open descriptor. This reduces the file’s length while the process keeps running, which can reclaim its allocated space. It is a last-resort workaround, not the preferred fix: the application may keep writing to the same descriptor, and its behavior after truncation depends on how it manages the file.
First double-check the PID, descriptor number, process, and file type. Then, only if you are certain it is safe, use the descriptor path. For the example above:
sudo truncate -s 0 /proc/2480/fd/7
Another method is to redirect an empty stream to the descriptor, but shell permissions and redirection behavior can make this less predictable. Use a controlled command and verify its effect rather than copying commands blindly.
Truncation destroys the file’s contents. Do not use it on database files, queues, active application data, or any file whose contents may be needed for recovery or auditing. It may also fail because of permissions, security settings, filesystem behavior, or the process’s access mode. If it succeeds, the application might continue writing at its previous file offset, potentially creating a sparse region or producing unexpected results. Plan to make the application reopen the file or restart it cleanly as soon as practical.
Why You Should Not Kill Processes Blindly
Terminating the PID shown by lsof can close the descriptor and free space, but an abrupt kill is risky. The process may be serving users, managing transactions, or writing important data. A forced command such as kill -9 does not give the application a chance to save state or shut down cleanly. It should not be the default solution to a deleted log file.
If a restart is necessary, use the service manager or the application’s documented shutdown procedure. Avoid killing a process just because its name looks familiar, and never assume that every deleted file is a log. When the process or file’s purpose is unclear, investigate before taking action.
Prevent Deleted Log Files from Filling the Disk Again
Repeated incidents usually indicate a log rotation or application configuration problem. A log rotation tool may rename or remove a log while the program continues writing to the old, now-unlinked file. Review the rotation configuration and confirm that it uses the application’s supported reopen signal or restart procedure. Also check whether the application can rotate its own logs safely.
- Set sensible log retention and size limits so old logs do not accumulate indefinitely.
- Verify that log rotation actually causes the service to reopen its output file.
- Monitor filesystem capacity and alert before a mount reaches its limit.
- Check for recurring deleted files with
sudo lsof +L1during routine troubleshooting. - Document safe restart and log-reopen procedures for important services.
Deleting a large file does not guarantee immediate free space if a process still holds it open. Addressing the cause prevents the same file descriptor from quietly consuming disk capacity again.
Special Cases: Containers and Other Mount Namespaces
On a server using containers, a deleted file may belong to a process inside a container. The host’s lsof output may still reveal the host PID, but the process name, paths, and filesystem view can differ from what you see inside the container. Identify the correct container and use its normal service-management procedure rather than stopping an unrelated host process.
Permissions and namespaces can also limit what lsof can see. If the command returns no results but the filesystem remains unexpectedly full, check that you are inspecting the right host, mount point, and user context. In some environments, you may need appropriate privileges or to run diagnostics from the host namespace. Also consider other explanations, including filesystem snapshots, reserved blocks, deleted files held by processes in another namespace, or a different mounted filesystem.
Verify That the Space Has Been Reclaimed
After the process closes the file, or after a carefully chosen truncation, run the checks again:
sudo lsof -nP +L1
df -h
The affected entry should disappear once no process holds the deleted file open. The free-space figure for the correct filesystem should increase, although the exact change may differ due to filesystem accounting and other activity. If the entry remains, another process may still have the file open, or the application may have reopened or recreated a log. Recheck the PID and descriptor list instead of repeating a potentially destructive command.
Conclusion
To clear an lsof deleted file in Linux, identify it with sudo lsof -nP +L1, inspect the owning process and descriptor, and confirm that the file is safe to release. The preferred approach is to make the application reopen the file or restart its service gracefully. Truncating a descriptor through /proc/PID/fd/FD can reclaim space in an emergency, but it permanently removes file contents and should be limited to files you have verified are disposable. Finally, confirm the result with lsof and df, then fix log rotation or monitoring so the issue does not return.
Disclaimer: Articles are written by Humans, AI or Both. Verify Important information.