C# – How to delete a directory

The simplest way to delete a directory is by using Directory.Delete() (in System.IO). You specify the directory path (absolute or relative) and an optional parameter that tells it to delete everything in the directory. Here’s an example of how to use this to delete directories:

using System.IO; //Delete an empty directory Directory.Delete(@"C:\HelloWorld\"); //Delete the directory and everything in it Directory.Delete(@"C:\Projects\HelloWorld\", recursive: true); //Delete w/ a relative path Directory.Delete("data");
Code language: C# (cs)

Note: If you’re working with DirectoryInfo objects already, you can use DirectoryInfo.Delete().

In this article, I’ll go into more details about deleting directories in different scenarios.

Delete a directory that contains files and subdirectories

You can only delete a directory if it’s empty. Otherwise you get the following exception:

System.IO.IOException: The directory is not empty.

You have to empty out the directory by deleting all of its files and subdirectories. You can do that by passing in the recursive flag to Directory.Delete(), like this:

using System.IO; Directory.Delete(@"C:\HelloWorld\", recursive: true);
Code language: C# (cs)

This deletes the non-empty directory by first deleting its subdirectories and files. After the directory is cleared out, it gets deleted.

Guard against DirectoryNotFoundException

If you try to use Directory.Delete() on a non-existent directory, it throws an exception:

System.IO.DirectoryNotFoundException: Could not find a part of the path

To guard against this, check if the directory exists before deleting it and catch the DirectoryNotFoundException, like this:

using System.IO; var path = @"C:\HelloWorld\"; try { if (Directory.Exists(path)) Directory.Delete(path); } catch(DirectoryNotFoundException) { //This means the directory was deleted //between calling Exists() and Delete() //Don't need to do anything here }
Code language: C# (cs)

Just checking if the directory exists before deleting it isn’t enough. There’s a race condition: after checking if a directory exists, it could get deleted by something else, so the call to Directory.Delete() would end up throwing DirectoryNotFoundException anyway. That’s why you still need to catch the exception.

Note: Since you have to catch the exception anyway, is there any point to using Directory.Exists() here? Yes, because in general, it’s best to avoid using exceptions for flow control.

Delete all files/subdirectories without deleting the directory itself

Let’s say you want to clear out a directory by deleting its files and/or subdirectories. You can loop through a directory’s content by using Directory.EnumerateDirectories()/EnumerateFiles(), and then use the appropriate delete method on them. Here’s an example:

using System.IO; var dirPath = @"C:\HelloWorld\"; //Delete all subdirectories foreach(var subdirPath in Directory.EnumerateDirectories(dirPath)) { Directory.Delete(subdirPath, recursive: true); } //Delete all top-level files foreach (var filePath in Directory.EnumerateFiles(dirPath)) { File.Delete(filePath); }
Code language: C# (cs)

Note: Use the recursive flag when deleting subdirectories to avoid the ‘directory not empty’ IOException.

Leave a Comment