How to Delete Empty Folders in C#?

Do you want to delete empty folders in C#? In this tutorial, I will show you different approaches on how to delete empty folders in C#.

In C#, to delete empty folders, you can use one of the below methods. I have explained each method with examples.

  • Using Directory.Delete()
  • Using DirectoryInfo Class
  • Using Recursion to Delete Nested Empty Folders

Delete Empty Folders in C#

Now, let us explore the above 3 methods to delete empty folders in C# with complete code and example.

Delete Empty Folders in C# using Directory.Delete()

Now, let us see how to use the Directory.Delete() method in C# to delete empty folder. The easiest way to delete an empty folder in C# is by using the System.IO.Directory.Delete() method. The code will ensure the folder is empty before deleting it.

Here is the complete code:

using System;
using System.IO;

class Program {
    static void Main() {
        string folderPath = @"C:\MyEmptyFolder";

        if (Directory.Exists(folderPath)) {
            if (Directory.GetFileSystemEntries(folderPath).Length == 0) {
                Directory.Delete(folderPath);
                Console.WriteLine("Empty folder deleted successfully.");
            } else {
                Console.WriteLine("Folder is not empty.");
            }
        } else {
            Console.WriteLine("Folder does not exist.");
        }
    }
}

You can see in below screenshot I have a folder that is not empty, and when I run the above code, it is displaying “Folder is not empty.”.

Delete Empty Folders in C#

And when I make this folder empty, the folder has been deleted.

Delete Empty Folders using DirectoryInfo Class in C#

The DirectoryInfo class also allows us to perform the necessary checks to ensure the folder is empty before deleting it.

using System;
using System.IO;

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

        if (di.Exists) {
            if (di.GetFileSystemInfos().Length == 0) {
                di.Delete();
                Console.WriteLine("Empty folder deleted successfully.");
            } else {
                Console.WriteLine("Folder is not empty.");
            }
        } else {
            Console.WriteLine("Folder does not exist.");
        }
    }
}

Delete Empty Folders in C# Using Recursion

If you need to search through nested directories and delete only the empty ones, recursion can be a helpful technique. Here is a complete example in C#.

using System;
using System.IO;

class Program {
    static void Main() {
        string rootFolder = @"C:\MyRootFolder";
        DeleteEmptyFolders(rootFolder);
    }

    static void DeleteEmptyFolders(string startLocation) {
        foreach (var directory in Directory.GetDirectories(startLocation)) {
            DeleteEmptyFolders(directory);
            if (Directory.GetFileSystemEntries(directory).Length == 0) {
                Directory.Delete(directory, false);
                Console.WriteLine($"Deleted empty folder: {directory}");
            }
        }
    }
}

Conclusion

I hope now you have an idea of how to delete empty folders in C# using different methods.

You may also like: