Delete All Files in a Directory in C#

  1. Delete All the Files Inside a Directory With the DirectoryInfo.GetFiles() Method in C
  2. Delete All the Files Inside a Directory With the DirectoryInfo.GetDirectories() Method in C
  3. Delete All the Files Inside a Directory With the DirectoryInfo.EnumerateFiles() Method in C
  4. Delete All the Directories Inside a Directory With the DirectoryInfo.EnumerateDirectories() Method in C

This tutorial will discuss the methods to delete all the files inside a directory in C#.

Delete All the Files Inside a Directory With the DirectoryInfo.GetFiles() Method in C

The DirectoryInfo.GetFiles() method in C# gets all the files inside a specified directory. The DirectoryInfo.GetFiles() method takes the path as an argument and returns an array of the FileInfo class objects that contain information about each file in that directory. We can get all the files inside the specified directory with the DirectoryInfo.GetFiles() method and then delete the files with the FileInfo.Delete() method. The following code example shows us how we can delete all the files inside a directory with the DirectoryInfo.GetFiles() and FileInfo.Delete() method in C#.

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Delete_all_files
{
    class Program
    {
        static void Main(string[] args)
        {
            DirectoryInfo di = new DirectoryInfo(@"C:\Sample");
            FileInfo[] files = di.GetFiles();
            foreach (FileInfo file in files)
            {
                file.Delete();
            }
            Console.WriteLine("Files deleted successfully");
        }
    }
}

Output:

Files deleted successfully

In the above code, we deleted all the files inside the path C:\Sample\ with the DirectoryInfo.GetFiles() and the FileInfo.Delete() methods in C#.

Delete All the Files Inside a Directory With the DirectoryInfo.GetDirectories() Method in C

If we also want to delete all the sub-directories inside the C:\Sample\ directory, we can use the DirectoryInfo.GetDirectories() method to get all the sub-directories inside the C:\Sample\ directory. The DirectoryInfo.GetDirectories() method returns an array of the DirectoryInfo class objects that contain information about all the sub-directories. We can delete the sub-directories with the DirectoryInfo.Delete() method. We can also delete all the sub-directories inside each sub-directory by specifying true inside the arguments of the DirectoryInfo.Delete() method. The following code example shows us how we can delete all the files and sub-directories inside a directory with the DirectoryInfo.GetDirectories() method in C#.

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Delete_all_files
{
    class Program
    {
        static void Main(string[] args)
        {
            DirectoryInfo di = new DirectoryInfo(@"C:\Sample");
            FileInfo[] files = di.GetFiles();
            foreach (FileInfo file in files)
            {
                file.Delete();
            }
            DirectoryInfo[] subDirectories = di.GetDirectories();
            foreach (DirectoryInfo subDirectory in subDirectories)
            {
                subDirectory.Delete(true);
            }
            Console.WriteLine("Files and Sub-directories deleted successfully");
        }
    }
}

Output:

Files and Sub-directories deleted successfully

In the above code, we deleted all the files and all the sub-directories inside the C:\Sample directory with the FileInfo.Delete() and the DirectoryInfo.Delete() methods.

Delete All the Files Inside a Directory With the DirectoryInfo.EnumerateFiles() Method in C

The methods discussed in the previous section do the job well, but there is a more robust way to do the same thing. The DirectoryInfo.EnumerateFiles() method iterates through each file inside the directory. This approach is much faster than the previous approach. The DirectoryInfo.EnumerateFiles() method can be used inside the foreach loop to get an object of the FileInfo class in each iteration. We can delete each file with the FileInfo.Delete() method. The following code example shows us how we can delete all the files inside a directory with the DirectoryInfo.EnumerateFiles() method in C#.

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Delete_all_files
{
    class Program
    {
        static void method2()
        {

        }
        static void Main(string[] args)
        {
            DirectoryInfo di = new DirectoryInfo(@"C:\Sample");
            foreach (FileInfo file in di.EnumerateFiles())
            {
                file.Delete();
            }
            foreach (DirectoryInfo subDirectory in di.EnumerateDirectories())
            {
                subDirectory.Delete(true);
            }
            Console.WriteLine("Files deleted successfully");
        }
    }
}

Output:

Files deleted successfully

The above code deletes all the files inside the C:\Sample directory with the DirectoryInfo.EnumerateFiles() and the FileInfo.Delete() methods in C#.

Delete All the Directories Inside a Directory With the DirectoryInfo.EnumerateDirectories() Method in C

If we also want to delete all the subdirectories inside our main directory, we can also use the DirectoryInfo.EnumerateDirectories() method in C#. The DirectoryInfo.EnumerateDirectories() method is used to iterate through each sub-directory inside the main directory. The DirectoryInfo.EnumerateDirectories() method can be used inside the foreach loop to get an object of the DirectoryInfo class in each iteration. We can delete each file with the DirectoryInfo.Delete() method. We can also specify true as the parameter of the DirectoryInfo.Delete() method if we want to delete all the sub-directories inside each sub-directory. The following code example shows us how we can delete all the files and sub-directories inside a directory with the DirectoryInfo.EnumerateFiles() method in C#.

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Delete_all_files
{
    class Program
    {
        static void method2()
        {

        }
        static void Main(string[] args)
        {
            DirectoryInfo di = new DirectoryInfo(@"C:\Sample");
            foreach (FileInfo file in di.EnumerateFiles())
            {
                file.Delete();
            }
            foreach (DirectoryInfo subDirectory in di.EnumerateDirectories())
            {
                subDirectory.Delete(true);
            }
            foreach (DirectoryInfo subDirectory in di.EnumerateDirectories())
            {
                subDirectory.Delete(true);
            }
            Console.WriteLine("Files and sub-directories deleted successfully");
        }
    }
}

Output:

Files and sub-directories deleted successfully

We deleted all the files and all the sub-directories inside the C:\Sample directory with the FileInfo.Delete() and the DirectoryInfo.Delete() methods in C#.

Contribute
DelftStack is a collective effort contributed by software geeks like you. If you like the article and would like to contribute to DelftStack by writing paid articles, you can check the write for us page.

Related Article - Csharp File

  • Check Whether a File Exists in C#
  • Download a File From a URL in C#
  • Related Article - Csharp Directory

  • Get All File Names in a Directory in C#
  • Unzip a Zip File in C#