How to delete all files in a directory in C#
By FoxLearn 12/25/2024 2:02:38 AM 14
The Directory.EnumerateFiles()
method returns an IEnumerable<string>
of file paths in the specified directory.
1. Deleting All Files in a Directory
using System.IO; foreach(var filePath in Directory.EnumerateFiles(@"C:\data\")) { File.Delete(filePath); }
This code will delete every file in the C:\data\
directory. However, it will leave the directory itself intact.
2. Deleting Files with a Specific Extension
You can refine your search by using a searchPattern
parameter, allowing you to target specific file types. For example, if you want to delete only .txt
files, you can use a wildcard search pattern like "*.txt"
.
using System.IO; foreach (var filePath in Directory.EnumerateFiles(@"C:\data\", searchPattern: "*.txt")) { File.Delete(filePath); }
In this example, only .txt
files in the specified directory will be deleted. Any other file types will remain unaffected.
3. Deleting Files in Root Directory and Subdirectories
If you need to delete files not only in the root directory but also in its subdirectories, you can make use of the SearchOption.AllDirectories
parameter. This will ensure that files in all nested directories are included in the deletion process.
using System.IO; foreach (var filePath in Directory.EnumerateFiles(@"C:\data\", "*", SearchOption.AllDirectories)) { File.Delete(filePath); }
Here, the search pattern "*"
ensures that all files (regardless of extension) are deleted. The SearchOption.AllDirectories
ensures that files from subdirectories are also included in the operation.
The Directory.EnumerateFiles()
method requires a searchPattern
argument. In this case, "*"
is used as a catch-all pattern to target all files, but you can use other patterns (like "*.jpg"
or "*.log"
) based on your needs.
4. Deleting All Files and Subdirectories
If you want to delete everything in a directory both files and subdirectories you’ll need to first delete the subdirectories and then delete the files in the root directory. This requires two separate steps.
using System.IO; // Delete all subdirectories recursively foreach (var subdirPath in Directory.EnumerateDirectories(@"C:\data\")) { Directory.Delete(subdirPath, recursive: true); } // Delete all files in the root directory foreach (var filePath in Directory.EnumerateFiles(@"C:\data\")) { File.Delete(filePath); }
This code first deletes all subdirectories recursively using Directory.Delete()
with the recursive: true
flag. After that, it deletes all remaining files in the root directory. This ensures that the entire directory (including its files and subdirectories) is deleted, leaving the root directory itself empty.
Why Use EnumerateFiles()
and EnumerateDirectories()
?
- Efficiency: Both
Directory.EnumerateFiles()
andDirectory.EnumerateDirectories()
returnIEnumerable<T>
collections, which means they can iterate over file paths without loading all paths into memory at once. This makes them more efficient when dealing with large directories. - Flexibility: The methods offer flexibility with search patterns and directory options, making it easy to delete specific files or traverse subdirectories.
The Directory.EnumerateFiles()
method in C# provides a powerful way to interact with files within a directory. Whether you’re deleting all files, targeting specific extensions, or working with files in subdirectories, EnumerateFiles()
and its associated parameters like SearchOption.AllDirectories
give you precise control.