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 while keeping the parent directory is a common maintenance task, but it is also one of the easiest ways to destroy the wrong data if you rush. The safe approach is to be explicit about the target path, preview what will be removed, and choose a command that matches your operating system and use case.
Deleting Contents on Linux or macOS
In a Unix-like shell, the goal is often “remove everything inside this directory, but not the directory itself.” One reliable approach is find with -mindepth 1:
-mindepth 1 protects the top-level directory itself, while -delete removes files and subdirectories below it.
If you want a safer preview first, list the candidates without deleting anything:
That preview step is worth the extra few seconds when the path matters.
Deleting Contents on Windows with PowerShell
In PowerShell, Get-ChildItem piped into Remove-Item is the usual pattern:
This removes files and folders inside the target directory while leaving the target directory itself in place.
To preview what would be removed first:
That is the PowerShell equivalent of a dry run by inspection.
Deleting from Python
If you are writing application code or a maintenance tool, Python gives you better control than shell commands. The code below deletes directory contents while keeping the parent folder:
This is useful when deletion is part of a larger workflow and you want explicit checks, logging, or exception handling.
Why Not Use a Blind Wildcard Command
Commands such as rm -rf * are short, but they are also easy to misuse. They depend on your current working directory and shell behavior, which increases the chance of deleting the wrong location.
A path-explicit command is safer because it makes the target visible in the command itself:
That extra clarity matters more than saving a few keystrokes.
Handling Hidden Files
One reason people think deletion “did not work” is that hidden files remain. In Unix-like systems, dotfiles are hidden from normal ls output unless you ask for them. find with -mindepth 1 includes hidden entries automatically, which is another reason it is a better general-purpose choice than simple glob patterns.
On Windows, using -Force with Get-ChildItem helps include hidden and system items where permissions allow it.
When You Also Want to Recreate the Directory
Sometimes the real goal is a clean reset. In that case, it may be clearer to remove the directory and recreate it:
That is a different operation from “clear contents,” but it can be easier to reason about in build scripts and temporary workspaces.
Common Pitfalls
The biggest mistake is running a destructive command without verifying the path. Always print, echo, or inspect the target directory first.
Another pitfall is forgetting hidden files. A command may appear to clear the folder while leaving important dotfiles behind.
Permissions are also a common issue. If some files are owned by another user or protected by the OS, deletion may fail partially unless you fix permissions or elevate carefully.
Finally, shell wildcards can behave differently than expected. A command that looks concise may skip certain files or depend on the current directory in ways that are easy to overlook.
Summary
- Be explicit about the target path before deleting directory contents.
- On Linux and macOS,
find /path -mindepth 1 -deleteis a strong general-purpose option. - On Windows, PowerShell with
Get-ChildItemandRemove-Itemworks well. - In application code, Python gives you safer checks and better error handling.
- Preview the contents first and watch for hidden files, permissions, and path mistakes.

