How do you recursively delete all files and folders in a directory?
How do you recursively delete all files and folders in a directory?
Ravi Vishwakarma is a dedicated Software Developer with a passion for crafting efficient and innovative solutions. With a keen eye for detail and years of experience, he excels in developing robust software systems that meet client needs. His expertise spans across multiple programming languages and technologies, making him a valuable asset in any software development project.
Anubhav Kumar
25-May-2025To recursively delete all files and subfolders in a directory (but not the root directory itself), you can use built-in APIs depending on your programming language. Here's how to do it in C#, Java, Python, and Bash:
In C#
path
) intact.FileAttributes.Normal
in case files are marked read-only.In Java
Call with:
clearDirectory(new File("your/path"))
.In Python
shutil.rmtree()
for recursive folder deletion.In Bash (Linux/macOS)
echo
to test paths before runningrm -rf
.Tips