How to delete a non-empty folder: rmdir /s on Windows, rm -rf on Linux
Codemia Knowledge Hub ·
To delete a non-empty folder, use rmdir /s /q C:\path\to\folder in Windows Command Prompt or rm -rf /path/to/folder on macOS and Linux. In Python, use shutil.rmtree(); in Node.js, use fs.rmSync() with the recursive option. Each removes the directory and everything inside it, including nested subdirectories and files.
Plain rmdir on Unix-like systems and basic Remove-Item in PowerShell refuse to delete directories that still contain files. That is a safety measure, so you need a different command or flag when you really want to remove a whole tree.
Windows
Command Prompt: rmdir /s /q
/s: removes the directory and all files and subdirectories inside it/q: quiet mode, suppresses the confirmation prompt
rmdir /s may fail if paths in the tree exceed 260 characters. Use the \\?\C:\path\to\folder prefix for long path support, or use PowerShell, which handles long paths better.
PowerShell: Remove-Item -Recurse -Force
-Recurse: deletes all child items-Force: removes hidden and read-only files without prompting
Wildcards let you delete several directories at once:
Linux and macOS
rm -rf
-r(recursive): descend into subdirectories and delete everything inside-f(force): skip confirmation prompts and ignore nonexistent files
Nothing goes to a trash folder. Deletion is permanent and immediate.
Deleting only directories that match a pattern
This finds all directories whose names start with cache_ and removes them recursively, leaving the rest intact.
Moving to the trash instead
If you want to be able to recover the folder, use a trash utility:
In code
Python: shutil.rmtree
shutil.rmtree walks the tree from the bottom up, removing files and then directories. It raises FileNotFoundError if the path does not exist, so guard it when the directory may already be gone:
On Windows it raises PermissionError on read-only files by default. An onerror handler can clear the read-only flag and retry:
Node.js: fs.rmSync
force prevents an error if the directory does not exist. The asynchronous version:
Java: Files.walk in reverse order
Files.walk traverses the tree depth-first. Sorting in reverse order deletes files before their parent directories.
Comparison
| Method | Platform | Recoverable | Handles Read-Only |
rm -rf | Linux, macOS | No | Yes (with force) |
trash-put / trash | Linux, macOS | Yes (trash) | No |
rmdir /s /q | Windows CMD | No | No |
Remove-Item -Recurse -Force | Windows PowerShell | No | Yes |
shutil.rmtree() | Python (cross-platform) | No | With error handler |
fs.rmSync({recursive: true}) | Node.js (cross-platform) | No | Yes (with force) |
Files.walk + Files.delete | Java (cross-platform) | No | No |
Permission denied and locked files
On Linux and macOS you can get "Permission denied" even with -f when you do not own the files or the directory has restricted permissions:
On Windows, running processes may lock files. Close the application using the file, or use a tool like Handle from Sysinternals to find which process holds the lock.
Avoiding accidental damage with rm -rf
A typo can wipe the filesystem. A misplaced space or variable expansion in a script can turn the command into rm -rf / or rm -rf /*. Double-check paths and quote variables: rm -rf "${TARGET_DIR}" rather than an unquoted expansion.
Empty variables are catastrophic. If $DIR is empty, rm -rf $DIR/ expands to rm -rf /. Validate variables before using them in destructive commands:
Symlinks behave differently with a trailing slash. rm -rf on a symlink removes the link itself, not the target directory. But rm -rf symlink/ may follow the link and delete the target's contents. Behavior varies across systems.
Related reading
- How do I resolve Cannot find module error using Node.js?
- How do I resolve ClassNotFoundException?
- How do I resolve git saying "Commit your changes or stash them before you can merge"?
- How do I resolve Please make sure that the file is accessible and that it is a valid assembly or COM component?
- How do I resolve the java.net.BindException Address already in use JVM_Bind error?
- How do I resolve these tensorflow warnings?
- How do I rotate the Android emulator display?
- How do I see if Wi-Fi is connected on Android?
.png&w=3840&q=75)
Tackling System Design Interview Problems
A short course that equips you with the skills to approach system design interviews methodically.
Start the free courseNo course covers this one yet
Basis Lab writes one for you from a sentence about what you want to be able to do, then teaches it and checks you understood. Your first course is free.
Build my courseInterview Questions practice on Codemia
Over 11,000 interview practice questions for 300+ companies, searchable by company and role.