Delete all files and subdirectories in a directory with C#
Given a directory, delete all files and subdirectories present in the directory using C#.
There are two variations to this problem. In the first variation, we delete all files and subdirectories from the specified directory along with the root directory. In the second variation, we delete all files and subdirectories but retain the root directory.
1. Delete the root directory
To delete the specified directory and all its subdirectories, use the Directory.Delete() method. The following example demonstrates its usage. The second argument to this method indicates whether to delete subdirectories and files.
|
1 2 3 4 5 6 7 8 9 10 11 |
using System.IO; public class Example { public static void Main() { string path = @"C:\some\path"; Directory.Delete(path, true); } } |
We can also use an instance of the DirectoryInfo class to delete subdirectories and files. It has the DirectoryInfo.Delete() method, which works similar to the Directory.Delete() method discussed above.
|
1 2 3 4 5 6 7 8 9 10 11 12 |
using System.IO; public class Example { public static void Main() { string path = @"C:\some\path"; DirectoryInfo directory = new DirectoryInfo(path); directory.Delete(true); } } |
2. Retain the root directory
To delete all files and subdirectories without deleting the directory, the idea is to loop through all files/subdirectories and individually delete them. We can use the GetFiles and GetDirectories() methods to retrieve the existing files and directories.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
using System.IO; public class Example { public static void Main() { string path = @"C:\Pending"; DirectoryInfo directory = new DirectoryInfo(path); foreach (FileInfo file in directory.GetFiles()) { file.Delete(); } foreach (DirectoryInfo dir in directory.GetDirectories()) { dir.Delete(true); } } } |
We can also use EnumerateFiles and EnumerateDirectories() methods to enumerate over the existing files and directories. This is more efficient when working with many files and directories.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
using System.IO; public class Example { public static void Main() { string path = @"C:\some\path"; DirectoryInfo directory = new DirectoryInfo(path); foreach (FileInfo file in directory.EnumerateFiles()) { file.Delete(); } foreach (DirectoryInfo dir in directory.EnumerateDirectories()) { dir.Delete(true); } } } |
This can also be done using LINQ’s ForEach() method instead of the foreach loop:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
using System.IO; using System.Linq; public class Example { public static void Main() { string path = @"C:\some\path"; DirectoryInfo directory = new DirectoryInfo(path); directory.EnumerateFiles() .ToList().ForEach(f => f.Delete()); directory.EnumerateDirectories() .ToList().ForEach(d => d.Delete(true)); } } |
Here’s an alternative method that first deletes the specified directory and all its subdirectories using the Directory.Delete() method and then re-create the directory. Note that this will permanently erase a few attributes on the root directory.
|
1 2 3 4 5 6 7 8 9 10 11 12 |
using System.IO; public class Example { public static void Main() { string path = @"C:\some\path"; Directory.Delete(path, true); Directory.CreateDirectory(path); } } |
That’s all about deleting all files and subdirectories in a directory with C#.
Thanks for reading.
To share your code in the comments, please use our online compiler that supports C, C++, Java, Python, JavaScript, C#, PHP, and many more popular programming languages.
Like us? Refer us to your friends and support our growth. Happy coding :)