How to delete all files and folders in a directory?
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Introduction
Deleting everything inside a directory is a powerful operation used in build cleanup, cache resets, and deployment scripts. The command is easy to run and easy to misuse, so safety checks are essential. The best practice is to verify target path, preview contents, and run deletion commands that keep the parent directory intact.
Safety Checklist Before Deleting
Use this checklist every time:
- Print absolute target path.
- Verify directory exists.
- Preview what will be removed.
- Confirm you are not at a critical root path.
Linux or macOS preview:
PowerShell preview:
This simple step prevents most accidental data loss incidents.
Linux and macOS: Delete Contents Only
To remove all contents while keeping parent directory:
Why this is safer than rm -rf *:
- Works even when shell glob behavior is unusual.
- Handles hidden files without extra patterns.
- Keeps parent directory.
Alternative with rm:
This is more fragile and easier to mistype, so prefer find when available.
Windows PowerShell: Remove All Children
PowerShell keeps parent directory when you target wildcard child path.
For long paths or locked files, run elevated shell and retry after closing applications using those files.
Windows CMD Variant
Command Prompt approach:
This uses one command for files and one loop for subdirectories.
Python Script Option for Automation
For cross-platform scripts, Python is often clearer than shell one-liners.
This keeps behavior explicit and easier to test in CI.
Handle Permissions and Locked Files
Deletion failures are usually one of:
- missing permissions.
- file currently in use.
- read-only attributes.
Troubleshooting steps:
- Run shell with elevated privileges if appropriate.
- Close processes using target files.
- Remove read-only flags before deletion.
On Linux, check process locks with lsof when needed.
Keep Parent Directory Intact in Automation
Many cleanup tasks need directory to remain for next steps. Confirm command only removes children, not directory itself.
Bad pattern in scripts:
Safer pattern:
This distinction prevents downstream failures when code expects the directory to exist.
Add Dry-Run and Logging
In production scripts, print targets and counts before delete.
For Python automation, log each removed top-level entry or at least total deleted count for auditing and rollback context.
Common Pitfalls
A common pitfall is running delete commands from the wrong directory due to stale shell context. Another issue is using wildcard patterns that miss hidden files, leaving partial cleanup state. Teams also accidentally delete the parent directory and break downstream jobs expecting it to exist. Ignoring permission or lock errors can result in inconsistent cleanup and flaky builds. Finally, running destructive commands without preview or backups creates avoidable recovery incidents.
Summary
- Always verify target path before destructive deletion.
- Prefer commands that remove children but keep parent directory.
- Use
find ... -mindepth 1 -deleteon Unix-like systems for robust cleanup. - Use PowerShell child-item removal for Windows workflows.
- Add preview, logging, and error handling in automation scripts.

