Deleting all files in a directory is a common server maintenance task: clearing logs, cache, temporary files, backup contents. There are several approaches, and the choice depends on whether hidden files need to be removed, subdirectories preserved, or everything deleted recursively.
rm: Basic Deletion
Delete all regular files in a directory:
rm /path/to/folder/*
The * character is a glob pattern that the shell expands into a list of all files in the directory. Subdirectories and hidden files are not affected.
Important: rm deletes files instantly with no recycle bin. The operation cannot be undone. Always verify the path before running.
Delete Files with Confirmation for Each
rm -i /path/to/folder/*
The -i (interactive) flag requests confirmation before deleting each file. It slows things down but protects against accidentally removing the wrong files.
Delete Including Hidden Files
Files beginning with a dot (.bashrc, .env, .htaccess) are not matched by the * pattern. To remove them as well, use .[!.]*:
rm /path/to/folder/* /path/to/folder/.[!.]*
The .[!.]* pattern matches files starting with . but excludes . and .. (current and parent directory). This matters — deleting . or .. would be catastrophic.
In bash, dotglob can be enabled so that * includes hidden files:
shopt -s dotglob
rm /path/to/folder/*
shopt -u dotglob # turn off afterwards
Delete Everything Recursively (Files and Subdirectories)
rm -rf /path/to/folder/*
-r — recursive (including subdirectories), -f — no confirmation prompts (force). Together — the most powerful and most dangerous combination.
⚠️ Never run
rm -rf /orrm -rf /*— this will destroy the entire filesystem. Double-check the path before running.
find: Targeted Deletion with Filters
find gives full control over exactly what to delete — by type, extension, age, or size.
Delete only files (not directories) in a folder:
find /path/to/folder -maxdepth 1 -type f -delete
-maxdepth 1 — current level only, not recursive. -type f — regular files only. -delete — remove what was found.
Delete files recursively across all subdirectories:
find /path/to/folder -type f -delete
Delete files by extension:
find /path/to/folder -name "*.log" -delete
find /path/to/folder -name "*.tmp" -delete
Delete files older than N days:
find /path/to/folder -type f -mtime +30 -delete
-mtime +30 — files last modified more than 30 days ago. Useful for log rotation.
Delete files larger than a certain size:
find /path/to/folder -type f -size +100M -delete
Preview what will be deleted — run without -delete, using -print instead:
find /path/to/folder -type f -mtime +30 -print
Clear a Folder but Keep Subdirectories
Sometimes all files need to be removed while leaving the directory structure intact — for example, clearing cache while keeping placeholder directories.
find /path/to/folder -type f -delete
Without -maxdepth 1, the command walks all subdirectories and deletes every file, leaving empty directories behind.
rsync: Unconventional but Reliable
Syncing a folder against an empty directory effectively clears it:
mkdir /tmp/empty_dir
rsync -a --delete /tmp/empty_dir/ /path/to/folder/
--delete removes from the target everything that does not exist in the source. The source is empty — so the target becomes empty too. This runs faster than rm -rf on folders with millions of small files (typical for PHP cache or node_modules).
Delete Contents Without Removing the Folder Itself
Sometimes it matters that the folder keeps its original permissions and ownership — deleting and recreating it may alter the metadata.
rm -rf /path/to/folder/* /path/to/folder/.[!.]*
The folder itself remains. Only its contents are deleted.
Safe Check Before Deletion
Before running a destructive command — verify the path:
ls /path/to/folder/
Or use echo to preview what the glob pattern will expand to, without deleting anything:
echo rm /path/to/folder/*
echo just prints the command with expanded filenames. A safe way to check exactly what will be targeted.
Common Errors
rm: cannot remove '/path/*': No such file or directory The folder is empty or the path is wrong. The glob * does not expand in an empty directory — bash passes the literal * to rm, which finds no such file.
Hidden files remain after rm * The * pattern does not match dotfiles. Run rm .[!.]* separately or use find.
rm -rf does not delete the directory itself With rm -rf /path/to/folder/* — the contents are deleted but not the folder. To delete the folder too: rm -rf /path/to/folder.
Accidentally deleted the wrong folder rm has no recycle bin. Recovery is only possible with tools like extundelete or testdisk — and only if the disk has not been overwritten. Always keep backups on production servers.
Quick Reference
| Task | Command |
|---|---|
| Delete all files in folder | rm /path/to/folder/* |
| Including hidden files | rm /path/to/folder/* /path/to/folder/.[!.]* |
| Recursively with subdirectories | rm -rf /path/to/folder/* |
| Only files via find | find /path/to/folder -maxdepth 1 -type f -delete |
| Recursively only files | find /path/to/folder -type f -delete |
| By extension | find /path/to/folder -name "*.log" -delete |
| Older than 30 days | find /path/to/folder -type f -mtime +30 -delete |
| Preview without deleting | find /path/to/folder -type f -mtime +30 -print |
| Fast cleanup (rsync) | rsync -a --delete /tmp/empty/ /path/to/folder/ |