Why rm Doesn't Free Disk Space in Linux and How to Fix It
In this article
The `rm` command in Linux does not free disk space when a file is still held open by a running process, or when the file is on a filesystem that uses copy-on-write or snapshots. To actually reclaim the space, you must close the process holding the file, delete snapshot references, or use tools like `lsof` and `fstrim` depending on the underlying cause.
Background: How rm and Filesystems Interact
When you run `rm file.txt`, the system does not erase the data blocks immediately. Instead, it removes the directory entry (the name) and marks the inode as free. The actual disk space becomes available only after every reference to that inode is gone. This includes hard links, open file descriptors, and filesystem-level features like snapshots or deduplication.
On modern Linux systems, the most common reason disk space stays occupied after `rm` is that a process still has the file open. This is especially frequent with log files, database files, or temporary files that a daemon keeps writing to. The kernel keeps the inode alive as long as the file descriptor exists, even though the file is no longer visible in the directory.
How to Find Which Process Holds the File
The first step is to identify the process that still holds the deleted file open. The `lsof` command lists open files, and you can filter for deleted ones:
- Run
lsof +L1to list all files with a link count of zero (i.e., deleted but still open). - Note the PID and the file name. For example,
COMMAND PID USER FD TYPE DEVICE SIZE/OFF NLINK NAME. - Check the process with
ps -p PID -o comm=to see what it is. - If it is a service you control, restart it gracefully:
systemctl restart servicenameor kill it withkill PID(usekill -9only if necessary). - After the process closes, run
df -hto confirm the space is freed.
If the file is held by a kernel thread or a process you cannot stop, you may need to schedule a maintenance window. For a quick temporary fix, you can truncate the file with : > /proc/PID/fd/N (where N is the file descriptor number from `lsof`), but this only reduces the file size, not the inode itself.
Why Snapshots and Copy-on-Write Keep Space
If you use a filesystem with snapshots (e.g., Btrfs, ZFS, or LVM thin snapshots), deleting a file does not free space until the snapshot that references the old data is removed. Snapshots work by keeping a copy of the file’s metadata and data blocks as they were at the snapshot time. When you delete the original file, the snapshot still points to those blocks, so they remain allocated.
To free space:
- List snapshots:
snapper list(openSUSE),zfs list -t snapshot, orlvsfor LVM. - Remove old snapshots you no longer need:
snapper delete SNAPSHOT_ID,zfs destroy pool/snapname, orlvremove /dev/vg/snapname. - After deletion, run
df -hto see the space reappear. For Btrfs, you may also need to runbtrfs balanceto reclaim unused block groups.
Note that on Btrfs, even without explicit snapshots, the filesystem may keep stale data blocks if the transaction is not committed. A normal sync or a short wait usually resolves that.
SSD Trim and Sparse Files: Hidden Space Usage
On solid-state drives, the filesystem must issue a TRIM command to tell the SSD that blocks are no longer in use. Without TRIM, the SSD still considers those blocks as occupied and cannot reuse them, so `df` may show free space but the drive’s usable capacity shrinks over time. Most modern distributions enable TRIM automatically via fstrim.timer (systemd).
To manually trim:
sudo fstrim -v /
This scans the mounted filesystem and issues TRIM for all free blocks. Run it periodically or enable the timer with systemctl enable --now fstrim.timer.
Sparse files are another subtlety. A file with holes (e.g., a downloaded torrent or a VM disk) may report a large apparent size but use little actual space. When you delete such a file, the space freed may be much less than the file size. Use du -h file to see the actual disk usage, not ls -lh.
Common Mistakes and Misconceptions
Many users assume that `rm` is broken when `df` still shows the same usage. Here are frequent pitfalls:
- Running `rm` on a file that is a hard link: the space is freed only when the last link is removed. Use
find / -inum INODEto locate all links. - Deleting a file on a filesystem that is mounted with the `noatime` or `lazytime` option: the inode may not be written back immediately, but the space is still freed after a short delay.
- Using `rm -f` on a file that is actually a mount point: you need to unmount it first, otherwise `rm` fails silently or removes the directory entry only.
- Checking disk space with `df` on a different filesystem than the one the file was on (e.g., checking `/` when the file is on `/home`).
- Forgetting that deleted files in a directory opened by a process (e.g., a shell’s current working directory) prevent the directory inode from being freed, but this rarely affects disk space significantly.
Alternatives and Preventative Measures
To avoid the problem entirely, consider these practices:
- Use
logrotateto manage log files so they are closed and reopened periodically. - For databases, use the vendor’s recommended way to truncate or archive WAL files, rather than deleting them manually.
- If you frequently need to delete files that may be held open, use
truncateto shrink them before deleting, or use theO_TMPFILEflag when creating temporary files. - Monitor open deleted files with a cron job that runs
lsof +L1and alerts you.
In emergency cases where you cannot stop the process, you can also remount the filesystem with the remount option to force a sync, but this does not release open descriptors.
Frequently Asked Questions
Why does df still show 100% usage after I deleted a huge file?
Most likely the file is still open by a process. Use lsof +L1 to find it and restart that process. If the file is on a snapshot-based filesystem, check your snapshots and remove the ones that reference the file.
Can I free space without restarting the process?
Yes, you can truncate the file via : > /proc/PID/fd/FD to reduce its size to zero. This frees the data blocks while keeping the inode alive. The process can continue writing to the file, but it will start from offset 0.
Why does fstrim not free space on my SSD?
fstrim only tells the SSD which blocks are free; it does not change the filesystem’s view of free space. If `df` shows free space but the SSD is slow, fstrim may not have run or the drive may not support TRIM. Check with lsblk -D to see if the discard feature is enabled.
What is the difference between rm and shred?
rm simply unlinks the file, while shred overwrites the data before unlinking. shred is slower and not always effective on journaling filesystems or SSDs, but it can prevent data recovery. It does not help with open file descriptors.
Does deleting a file on a Btrfs filesystem free space immediately?
Not always. Btrfs uses copy-on-write and may keep old data blocks until a transaction commit or a balance operation. Run sync and then btrfs balance start -dusage=0 / to reclaim unused space.
How can I prevent deleted files from being held open in the future?
Ensure that services that write to log files use logrotate with the `copytruncate` option, or configure them to close and reopen their log files on a signal. For custom scripts, always close file handles after writing.