To remove a directory in Linux: rmdir mydir if it is empty, and rm -r mydir if it has anything in it. Add -f to suppress prompts and errors, giving you the famous rm -rf. That is the answer, and it is genuinely all there is to the syntax. The reason this question has thousands of results is not that the command is hard - it is that rm has no undo, no recycle bin, and no confirmation once you add -f. On a laptop that is a bad afternoon. On a server holding the only copy of something, it is an incident. The commands take a minute to learn; the habits are what save you.
Every result tells you rm -rf works. Fewer mention that it works on absolutely everything, including the things you did not mean, at the speed of the disk.
Table of contents
- The commands
- Why rmdir still exists
- rm -rf and how people lose data with it
- Habits that cost nothing and save you
- When the directory refuses to go
- This is different on a server
- How this fits the rest of the stack
- FAQ
The commands
# Empty directory only. Fails loudly if it is not empty.
rmdir mydir
# Directory and everything inside it. Prompts on protected files.
rm -r mydir
# Same, but never prompts and never complains.
rm -rf mydir
# Prompt once before the whole operation. The good middle ground.
rm -rI mydir
# Remove a chain of empty directories.
rmdir -p a/b/c
The flags, plainly:
-r- recursive. Required for any directory with contents.-f- force. Suppresses prompts and ignores missing files. This is the dangerous one.-i- prompt before every file. Safe, unusable on anything large.-I- prompt once for the whole operation. The underrated option.-v- verbose. Prints what it deleted, which is worth having.
rmdir failing on a non-empty directory is a feature, not an obstacle. It is the one command here that refuses to do something catastrophic.
Why rmdir still exists
It looks redundant next to rm -r. It is not - it is a safety rail.
rmdir only removes empty directories. If the directory has contents, it refuses and tells you so. That refusal is the point: when you believe a directory is empty and rmdir disagrees, you have just learned something before destroying it rather than after.
Use rmdir when you expect empty. Use rm -r when you mean to delete contents. Encoding your intent in the command means the system can catch you when your intent is wrong.
rm -rf and how people lose data with it
rm -rf is not evil - it is precise, fast, and does exactly what you said. The failure mode is that what you said is occasionally not what you meant.
The classic ways this goes wrong:
- A stray space.
rm -rf /home/app /datadeletes two things.rm -rf /home/app/datadeletes one. One keystroke apart. - An unset variable.
rm -rf $DIR/where$DIRis empty expands torm -rf /. Modern rm refuses/specifically, butrm -rf $DIR/*has no such protection. - A trailing slash on a symlink. Different behaviour than you expect, and the difference is destructive.
- Wrong directory. You thought you were in staging.
pwdcosts nothing. - A glob that matched more than you thought.
rm -rf *in the wrong place is instant.
There is no undelete. On a typical filesystem, recovery from rm ranges from expensive to impossible, and any writes afterwards make it worse. The backup you took yesterday is the recovery plan. There is no other one.
Habits that cost nothing and save you
None of these are clever. All of them work.
lsit first. Runlson the exact path beforerm -rfon it. If the path is wrong,lstells you for free.- Use
-Iinstead of-f. One prompt for the whole operation is not friction, it is a moment to read what you typed. - Never use variables unquoted.
rm -rf "$DIR"beatsrm -rf $DIR. An unset variable then deletes nothing rather than everything. - Prefer absolute paths in scripts. Relative paths depend on where the script happens to run.
- Check
pwdbefore destructive globs. The most common precondition failure is being somewhere else. - In scripts,
set -u. The script exits on an unset variable instead of expanding it to nothing.
The set -u one is the highest-value line in this article for anyone writing deployment scripts. An unset variable that silently becomes an empty string is how rm -rf $BUILD_DIR/ becomes rm -rf /.
When the directory refuses to go
A few genuine failures that are not user error:
- Permission denied - you do not own it. Check with
ls -labefore reaching for sudo. Needing root to delete something is a hint worth reading. - Device or resource busy - something has it open, or it is a mount point.
lsof +D /pathfinds the holder. Deleting a mount point does not do what you want. - Directory not empty from rmdir - working as intended. There are hidden files.
ls -lashows them. - Argument list too long - the glob expanded past the shell’s limit. Use
find . -deleteinstead ofrm *.
That last one is the reason find exists for bulk deletion: find /path -type f -name '*.log' -delete handles arbitrarily many files and lets you preview the set by dropping -delete first. Previewing before deleting is the whole theme here.
This is different on a server
On your laptop, a bad rm costs you an afternoon and some annoyance. On a server it can cost the only copy of something, while a service is live, with no undo.
The differences worth respecting:
- You often cannot see what you deleted. No file manager, no trash, no dialog. Just a prompt returning silently.
- Deletion can be immediate and total. No versioning unless you built it.
- Something may be reading that path right now. Deleting a file a running process holds open frees space only when the process exits - which is why disk-full sometimes survives a delete.
- Automation repeats mistakes at machine speed. A bad
rmin a deploy script runs on every deploy, everywhere.
This is the real argument for a platform where the artifacts are managed rather than hand-tended: not that rm is hard, but that a hand-managed filesystem eventually meets a tired human at 2am with root and a typo.
How this fits the rest of the stack
Deleting files is free. Storing them is not, and neither is the storage you are still paying for because nobody is sure what is safe to remove. Old builds, orphaned volumes, logs nobody reads - they bill every month while contributing nothing. The RunxBuild hosting calculator puts storage next to compute, bandwidth, and the database so you can see what each layer costs before you commit, rather than discovering it on an invoice. The RunxBuild dashboard is where the team sees what is actually being used.
Useful related references:
- Remove Directory Linux: rmdir for Empty, rm -rf for Recursive
- Remove Directory Linux Not Empty: rm -rf, find -delete, and Safety
- Delete Directory Linux: rmdir, rm -rf, and find -delete
- Services on RunxBuild
FAQ
What is the difference between rmdir and rm -r in Linux?
rmdir removes only empty directories and fails with an error if the directory has contents. rm -r removes a directory and everything inside it, recursively. The failure of rmdir is a safety feature: use it when you expect the directory to be empty, so the system tells you when your expectation is wrong rather than destroying the contents you forgot about.
How do I force remove a directory in Linux?
rm -rf directory removes it recursively without prompting or reporting missing files. Consider rm -rI directory instead - it prompts once for the whole operation, which costs you one keystroke and gives you a moment to read the path you actually typed. Always quote variables: rm -rf "$DIR", never rm -rf $DIR.
Can I undo rm in Linux?
No. There is no recycle bin and no built-in undo - rm unlinks the file immediately. Recovery tools exist but range from unreliable to impossible on modern filesystems, and any subsequent writes reduce the odds further. The realistic recovery plan for an accidental rm is your backup. If you do not have one, you do not have a recovery plan.
How do I remove a directory that is not empty?
rm -r directory. rmdir refuses non-empty directories by design. If rmdir unexpectedly reports the directory is not empty, run ls -la first - there are almost certainly hidden dotfiles you did not account for, and that surprise is worth understanding before you delete.
Why does rm say device or resource busy?
Something is holding the path open, or it is a mount point. Use lsof +D /path to find the process. If it is a mount point, unmount it first - deleting a mount point does not remove the underlying data. Note that deleting a file a running process has open frees the disk space only when that process exits, which explains disk-full conditions that survive a delete.