File Management
Directory Operations
Data Deletion
Computer Tutorials
Operating System Tips

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:

bash
find /path/to/target -mindepth 1 -delete

-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:

bash
find /path/to/target -mindepth 1

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:

powershell
Get-ChildItem "C:\path\to\target" -Force | Remove-Item -Recurse -Force

This removes files and folders inside the target directory while leaving the target directory itself in place.

To preview what would be removed first:

powershell
Get-ChildItem "C:\path\to\target" -Force

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:

python
1from pathlib import Path
2import shutil
3
4
5def clear_directory(path_str: str) -> None:
6    path = Path(path_str)
7
8    if not path.is_dir():
9        raise ValueError(f"Not a directory: {path}")
10
11    for child in path.iterdir():
12        if child.is_dir():
13            shutil.rmtree(child)
14        else:
15            child.unlink()
16
17
18clear_directory("/tmp/demo")

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:

bash
find /path/to/target -mindepth 1 -delete

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:

python
1from pathlib import Path
2import shutil
3
4path = Path("/tmp/demo")
5shutil.rmtree(path)
6path.mkdir(parents=True, exist_ok=True)

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 -delete is a strong general-purpose option.
  • On Windows, PowerShell with Get-ChildItem and Remove-Item works 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.

Course illustration
Course illustration

All Rights Reserved.