Delete Huge Files in Linux: 7 Safe Methods
In this article
To delete huge files in Linux, use the rm command with the full path to the file, such as rm /path/to/hugefile. For files larger than a few gigabytes, this is usually instant, but you should check for open file handles and use shred or truncate if you need to securely erase or quickly free disk space.
Why Deleting Large Files Is Different
On most Linux filesystems, deleting a file is a metadata operation—the system removes the directory entry and marks the disk blocks as free. The actual data remains on the disk until overwritten. This means rm is typically just as fast for a 100 GB file as for a 1 KB file. The real challenges are:
- Disk space not freed because a process still holds the file open.
- Accidental deletion of the wrong file, which is harder to recover when it is huge.
- Secure deletion requirements, where you need to overwrite the data to prevent recovery.
Basic Deletion with rm
The rm command is the standard tool. Its syntax is simple:
rm /path/to/hugefile
To delete multiple files at once, list them all:
rm /path/to/hugefile1 /path/to/hugefile2
To delete a directory and everything inside it, use the recursive flag:
rm -r /path/to/hugedirectory
Be careful: rm does not move files to a trash folder. Once deleted, the file is gone from the filesystem, though the raw data may remain on disk until overwritten.
Using find to Locate and Delete Large Files
If you need to find large files before deleting them, the find command is your best ally. To find files larger than 1 GB in the current directory and its subdirectories:
find . -type f -size +1G
To delete them directly, add -delete:
find . -type f -size +1G -deleteThis is a powerful one-liner, but test it first without
-deleteto see exactly which files match. You can also use-size +500Mfor megabytes or-size +1Gfor gigabytes.Deleting Files That Are “In Use”
A common problem: you delete a huge file, but
df -hshows no free space gained. This happens when a process still has the file open. On Linux, the file is not truly removed until all file descriptors are closed.To find which process is holding a file, use
lsof:lsof /path/to/hugefileIf the output lists a process, you can either kill that process (if safe) or use
truncateto shrink the file to zero bytes, which frees the disk space immediately while the process keeps writing to a zero-length file:truncate -s 0 /path/to/hugefileThis is often used for log files that are still being written to. After truncating, the space is released, and the process can continue writing.
Securely Deleting with shred
If the file contains sensitive data, a simple
rmleaves the data recoverable with forensic tools. Theshredcommand overwrites the file with random data before deleting it:shred -u /path/to/hugefileThe
-uflag tellsshredto remove the file after overwriting. By default, it overwrites three times, which is usually sufficient for modern hard drives. For SSDs,shredis less effective because the drive’s wear-leveling may prevent overwriting all physical blocks. In that case, full-disk encryption is a better safeguard.Note:
shredis not available on all filesystems, such as some network filesystems. Check withman shredfor details.Deleting Files with Special Characters in Names
Large files often have spaces, quotes, or other special characters in their names. To delete them safely, always quote the filename:
rm "my huge file.bin"If the filename starts with a hyphen, use
--to signal the end of options:rm -- -hugefileAlternatively, use a relative or absolute path that does not start with a hyphen, such as
rm ./-hugefile.Alternatives: Emptying Files Instead of Deleting
Sometimes you do not need to delete the file, just empty its contents. This is useful for log files or temporary files that should remain for a process to write to. The quickest way is:
> /path/to/hugefileThis truncates the file to zero bytes without changing its permissions or ownership. The
truncatecommand does the same thing and is more explicit:truncate -s 0 /path/to/hugefileBoth methods free the disk space instantly. This is a common technique for managing application logs that grow without bound.
Frequently Asked Questions
Why does df still show no free space after I deleted a huge file?
This usually means a process still has the file open. Use
lsof /path/to/fileto find the process, then either restart it or usetruncate -s 0 /path/to/fileto free the space without closing the file.Is rm permanent? Can I recover a deleted file?
Yes,
rmis permanent—it does not move the file to a trash folder. The data may still be recoverable with specialized tools if the disk blocks have not been overwritten, but this is not guaranteed and is much harder for large files. For sensitive data, useshred.How do I delete all files larger than 1 GB in a directory?
Use
find /path/to/dir -type f -size +1G -delete. Test it without-deletefirst to see what will be removed.What is the difference between rm and shred?
rmsimply unlinks the file, leaving the data on disk.shredoverwrites the file’s contents with random data before unlinking, making recovery much more difficult. Useshredfor sensitive files.Can I delete a file that is being written to by a process?
Yes, you can run
rmon it, but the space will not be freed until the process closes the file. If you want to free space immediately, usetruncate -s 0to empty the file instead.What does the -delete option in find do?
The
-deleteoption removes the files that match the preceding criteria. It is equivalent to runningrmon each file, but it is faster and handles special characters in filenames more safely.