HOWTO · Csharp
How to Delete All Files in a Directory in C#
Use DirectoryInfo.GetFiles() or EnumerateFiles() to remove files in C#. DirectoryInfo.Delete(true) removes the directory it is called on and its contents; to keep a parent directory, call it individually on each child directory.
On this page
To remove every file directly inside a directory, enumerate its files and call FileInfo.Delete() for each one. This leaves the target directory and its child directories in place. If you also remove child directories, their contents are deleted recursively, so choose that behavior only when it is intended. (C#; DirectoryInfo.Delete(true)) (DirectoryInfo.Delete())
Delete files only with DirectoryInfo.GetFiles()
DirectoryInfo.GetFiles() returns an array of FileInfo objects for files directly inside the target directory. Calling FileInfo.Delete() on each removes those files; it does not descend into child directories. The example creates its own temporary target and sample files, removes the files, and reports that the target still exists. It then removes that temporary target during cleanup.
Run this example:
using System;
using System.IO;
class Program
{
static void Main()
{
string path = Path.Combine(Path.GetTempPath(), "csharp-file-cleanup-" + Guid.NewGuid().ToString("N"));
Directory.CreateDirectory(path);
File.WriteAllText(Path.Combine(path, "first.txt"), "one");
File.WriteAllText(Path.Combine(path, "second.txt"), "two");
var directory = new DirectoryInfo(path);
int removed = 0;
foreach (FileInfo file in directory.GetFiles())
{
file.Delete();
removed++;
}
Console.WriteLine($"Files removed: {removed}");
Console.WriteLine($"Target directory remains: {directory.Exists}");
Directory.Delete(path);
}
}
Files removed: 2
Target directory remains: True
The path is under the operating system’s temporary directory. In application code, set it to the intended location and ensure the process has permission to modify it. If a file is locked or access is denied, deletion can fail; handle the relevant I/O or authorization error for your application rather than assuming every call succeeds.
Delete files and subdirectories with DirectoryInfo.GetDirectories()
To remove files and child directories, first delete the files directly in the target, then use DirectoryInfo.GetDirectories() to retrieve its child directories and call DirectoryInfo.Delete(true) on each. The true argument recursively removes that child and its contents. The target directory itself remains. This is a destructive choice: inspect the target path and confirm recursive removal is appropriate before running it.
The operations are foreach (FileInfo file in directory.GetFiles()) file.Delete(); followed by foreach (DirectoryInfo child in directory.GetDirectories()) child.Delete(true);. Keep the two loops in this order and use the actual target DirectoryInfo instance in your program.
Delete files with DirectoryInfo.EnumerateFiles()
DirectoryInfo.EnumerateFiles() supplies an enumerable sequence of FileInfo objects, so it can be consumed directly by a foreach loop. By comparison, GetFiles() returns an array. Both approaches can delete the files directly in the target; neither removes child directories. Choose based on whether you need an array of results or want to iterate the sequence. Enumeration is not a guarantee of better performance for every workload.
For files-only deletion, the loop body remains file.Delete():
using System;
using System.IO;
class Program
{
static void Main()
{
string path = Path.Combine(Path.GetTempPath(), "csharp-enumerate-files-" + Guid.NewGuid().ToString("N"));
Directory.CreateDirectory(path);
File.WriteAllText(Path.Combine(path, "sample.txt"), "content");
var directory = new DirectoryInfo(path);
foreach (FileInfo file in directory.EnumerateFiles())
{
file.Delete();
}
Console.WriteLine($"Files remaining: {directory.GetFiles().Length}");
Directory.Delete(path);
}
}
Files remaining: 0
Delete subdirectories with DirectoryInfo.EnumerateDirectories()
DirectoryInfo.EnumerateDirectories() yields the child directories of the target. Call Delete(true) for each child when its complete tree should be removed. This operation does not delete files that sit directly in the target, so combine it with a files-only loop if both kinds of contents must be removed. The target directory remains after its children are deleted.
The child-directory operation is foreach (DirectoryInfo child in directory.EnumerateDirectories()) child.Delete(true);. Do not put this loop under a files-only cleanup unless recursive removal is intended.
Choose an approach and verify the result
Use GetFiles() when an array is useful, or EnumerateFiles() when you want to process file results as an enumerable sequence. Both are files-only operations. Add a child-directory loop with Delete(true) only when recursive removal is part of the task. Verify the selected path before deletion and check the directory contents afterward with GetFiles() and GetDirectories(); a missing or inaccessible path, or a file that cannot be removed, can prevent the operation from completing.