Delete All Files in a Directory With a Specific Extension in C#

Do you want to delete all files in a directory with an extension in C#? Check out this complete tutorial; I have explained how to delete all files in a directory with a specific extension in C#.

I will show you several methods, such as using DirectoryInfo, Directory, and LINQ.

Delete All Files in a Directory With a Specific Extension in C#

Below are the 3 methods you can use

  • Using DirectoryInfo Class
  • Using Directory Class
  • Using LINQ

Delete All Files in a Directory With Extension in C# using DirectoryInfo Class

The DirectoryInfo class provides instance methods for creating, moving, and browsing through directories and subdirectories. Below is a complete C# code snippet that demonstrates how to delete all .txt files in a given directory using DirectoryInfo.

using System;
using System.IO;

class Program
{
    static void Main()
    {
        string directoryPath = @"C:\ExampleDirectory";
        DirectoryInfo di = new DirectoryInfo(directoryPath);

        foreach (FileInfo file in di.GetFiles())
        {
            if (file.Extension == ".txt")
            {
                file.Delete();
            }
        }
    }
}

Here is the code explanation:

  • Import the System.IO namespace to use the DirectoryInfo class.
  • Define the path of the directory where the files are located.
  • Create an instance of DirectoryInfo and pass the directory path to it.
  • Iterate through all the files in the directory.
  • Use an if statement to check the extension of each file.
  • Delete the file if its extension is .txt.

You can see the below screenshot. I have a folder having a few files with different extensions like “.txt”, “.docx” and “.xlsx”.

c# delete all files in directory with extension

Now, when I run the console application, you can see it deletes both the .txt files, but it does not delete the other extension files; check the screenshot below.

delete all files in directory with extension C#

Delete All Files in a Directory With Extension in C# using Directory Class

The Directory class provides static methods for creating, moving, and enumerating through directories and subdirectories in C#. Below is the code snippet using Directory to delete all .txt files in a directory in C#.

using System;
using System.IO;

class Program
{
    static void Main()
    {
        string directoryPath = @"C:\ExampleDirectory";
        string[] files = Directory.GetFiles(directoryPath);

        foreach (string file in files)
        {
            if (Path.GetExtension(file) == ".txt")
            {
                File.Delete(file);
            }
        }
    }
}

Here is the complete code explanation.

  • Import the System.IO namespace.
  • Define the path of the directory.
  • Use Directory.GetFiles() to get all files in the directory.
  • Iterate through the files and check their extensions.
  • Delete the file if its extension is .txt.

Delete All Files in a Directory With Extension in C# using LINQ

If you are comfortable with LINQ (Language-Integrated Query), you can use the below code to delete all .txt files in a directory in C#.

using System;
using System.IO;
using System.Linq;

class Program
{
    static void Main()
    {
        string directoryPath = @"C:\ExampleDirectory";
        var txtFiles = Directory.GetFiles(directoryPath).Where(file => Path.GetExtension(file) == ".txt");

        foreach (string file in txtFiles)
        {
            File.Delete(file);
        }
    }
}

Here is the complete code explanation:

  • Import the System.IO and System.Linq namespaces.
  • Define the directory path.
  • Use LINQ to filter files with .txt extension.
  • Delete the files.

Conclusion

We’ve explored three different methods to delete all files with a specific extension in a directory using C#:

  • Using the DirectoryInfo class for instance-level operations.
  • Using the Directory class for static methods.
  • Using LINQ for a more concise codebase.

You may also like the following tutorials: