Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
How to delete all files and folders from a path in C#?
Deleting all files and folders from a directory is a common task in C# file operations. The System.IO namespace provides the DirectoryInfo class and Directory class that offer multiple approaches to accomplish this task safely and efficiently.
Syntax
Following is the syntax for using DirectoryInfo to delete directories and files −
DirectoryInfo di = new DirectoryInfo(path); di.Delete(true); // true = recursive delete
Following is the syntax for using Directory class methods −
Directory.Delete(path, true); // true = recursive delete
Using DirectoryInfo for Recursive Deletion
The DirectoryInfo class provides detailed control over directory operations. The Delete(true) method recursively removes all subdirectories and files −
using System;
using System.IO;
class Program {
static void Main(string[] args) {
string path = @"C:\TempDemo";
// Create a test directory with files and subdirectories
Directory.CreateDirectory(path);
Directory.CreateDirectory(Path.Combine(path, "SubFolder1"));
Directory.CreateDirectory(Path.Combine(path, "SubFolder2"));
File.WriteAllText(Path.Combine(path, "file1.txt"), "Test content 1");
File.WriteAllText(Path.Combine(path, "SubFolder1", "file2.txt"), "Test content 2");
Console.WriteLine("Before deletion: Directory exists = " + Directory.Exists(path));
// Delete all contents using DirectoryInfo
DirectoryInfo di = new DirectoryInfo(path);
if (di.Exists) {
di.Delete(true); // Recursive deletion
Console.WriteLine("After deletion: Directory exists = " + Directory.Exists(path));
}
}
}
The output of the above code is −
Before deletion: Directory exists = True After deletion: Directory exists = False
Using Directory Class for Simple Deletion
The static Directory.Delete() method provides a more straightforward approach −
using System;
using System.IO;
class Program {
static void Main(string[] args) {
string path = @"C:\TempDemo2";
// Create test structure
Directory.CreateDirectory(path);
Directory.CreateDirectory(Path.Combine(path, "Images"));
File.WriteAllText(Path.Combine(path, "document.txt"), "Sample document");
File.WriteAllText(Path.Combine(path, "Images", "photo.jpg"), "Sample image data");
Console.WriteLine("Files before deletion:");
foreach (string file in Directory.GetFiles(path, "*", SearchOption.AllDirectories)) {
Console.WriteLine(" " + Path.GetFileName(file));
}
// Delete entire directory tree
Directory.Delete(path, true);
Console.WriteLine("Directory deleted successfully!");
Console.WriteLine("Directory exists: " + Directory.Exists(path));
}
}
The output of the above code is −
Files before deletion: document.txt photo.jpg Directory deleted successfully! Directory exists: False
Deleting Only Contents (Keeping Root Directory)
Sometimes you need to delete all contents but preserve the root directory −
using System;
using System.IO;
class Program {
static void Main(string[] args) {
string rootPath = @"C:\TempDemo3";
// Create test structure
Directory.CreateDirectory(rootPath);
Directory.CreateDirectory(Path.Combine(rootPath, "Data"));
File.WriteAllText(Path.Combine(rootPath, "config.xml"), "<config></config>");
File.WriteAllText(Path.Combine(rootPath, "Data", "data.json"), "{"key":"value"}");
DirectoryInfo di = new DirectoryInfo(rootPath);
// Delete all files in root directory
foreach (FileInfo file in di.GetFiles()) {
file.Delete();
Console.WriteLine("Deleted file: " + file.Name);
}
// Delete all subdirectories
foreach (DirectoryInfo dir in di.GetDirectories()) {
dir.Delete(true);
Console.WriteLine("Deleted directory: " + dir.Name);
}
Console.WriteLine("Root directory exists: " + Directory.Exists(rootPath));
Console.WriteLine("Root directory is empty: " + (di.GetFiles().Length == 0 && di.GetDirectories().Length == 0));
// Cleanup
Directory.Delete(rootPath);
}
}
The output of the above code is −
Deleted file: config.xml Deleted directory: Data Root directory exists: True Root directory is empty: True
Comparison
| Method | Use Case | Performance | Control Level |
|---|---|---|---|
| DirectoryInfo.Delete(true) | When you need to check directory properties or handle individual files | Slightly slower | High |
| Directory.Delete(path, true) | Simple, straightforward deletion of entire directory tree | Faster | Low |
| Manual iteration | When you need custom logic for each file/folder | Slowest | Highest |
Conclusion
C# provides multiple approaches for deleting directories and their contents. Use Directory.Delete(path, true) for simple scenarios, and DirectoryInfo when you need more control over the deletion process. Always use the recursive parameter true to delete subdirectories and files.
