C# Directory Delete() Method

Beginner
5 min read
Updated: Nov 2026
3 examples
System.IO

Definition

In C#, Directory.Delete() removes a folder from disk. By default the folder must be empty. Pass true as the second argument and it removes the folder together with everything inside it.

The method does not use the Recycle Bin. A deleted folder is gone, so always double-check the path before you call it.

This is a static method of System.IO.Directory. Call it as Directory.Delete(...); there is no new Directory(). For an object that remembers one folder, see DirectoryInfo.

Syntax

Use the one-argument form for an empty folder and the two-argument form to remove a folder with contents.

C#
string folder = Path.Combine(Path.GetTempPath(), "codetofun-delete-demo");
Directory.CreateDirectory(folder);

Directory.Delete(folder);          // only works when the folder is empty
// Directory.Delete(folder, true); // removes the folder and all its contents
CallWhat it does
Directory.Delete(path)Removes an empty folder. Throws IOException if anything is inside.
Directory.Delete(path, false)Same as above. false means "do not remove contents".
Directory.Delete(path, true)Removes the folder, every file, and every subfolder. Powerful and permanent.

Warning: Delete(path, true) cannot be undone. Never build the path from user input without checks, and never point it at a real folder while you are learning. Use a demo folder inside Path.GetTempPath().

Library declarations
C#
public static void Delete(string path);
public static void Delete(string path, bool recursive);

Parameters

ParameterDescription
pathThe folder to remove. It must exist.
recursivetrue removes subfolders and files too. false (the default) removes only an empty folder.

Return Value

Return ValueDescription
voidNothing is returned. If the call finishes without an exception, the folder is gone.

C# Examples

Example 1 — Delete an Empty Folder

Create a folder, remove it with the one-argument call, and confirm it is gone.

C#
using System;
using System.IO;

class Program {
    static void Main() {
        string folder = Path.Combine(Path.GetTempPath(), "codetofun-delete-empty");
        Directory.CreateDirectory(folder);
        Console.WriteLine(Directory.Exists(folder));

        Directory.Delete(folder);
        Console.WriteLine(Directory.Exists(folder));
    }
}

How It Works

  1. Start with something to delete. CreateDirectory makes the empty demo folder, and the first Exists prints True.
  2. No second argument. Delete(folder) is the plain form. It succeeds because nothing is inside the folder.
  3. Check again. The second Exists call asks the disk, not a saved answer, so it prints False.

Example 2 — Why Delete Needs recursive for a Full Folder

Try the plain delete on a folder that has contents, then delete with true.

C#
using System;
using System.IO;

class Program {
    static void Main() {
        string root = Path.Combine(Path.GetTempPath(), "codetofun-delete-full");
        if (Directory.Exists(root)) Directory.Delete(root, true);

        Directory.CreateDirectory(Path.Combine(root, "inner"));
        File.WriteAllText(Path.Combine(root, "a.txt"), "data");

        try {
            Directory.Delete(root);
        } catch (IOException) {
            Console.WriteLine("Folder is not empty");
        }
        Console.WriteLine(Directory.Exists(root));

        Directory.Delete(root, true);
        Console.WriteLine(Directory.Exists(root));
    }
}

How It Works

  1. Give it contents. The demo folder now holds one subfolder, inner, and one file, a.txt.
  2. Plain delete refuses. The folder is not empty, so Delete(root) throws IOException. That is a safety net against deleting by accident.
  3. Nothing was lost. The failed call changed nothing, so Exists still prints True.
  4. Opt in with true. Delete(root, true) walks through the tree and removes the file, the subfolder, and then the root itself.

Example 3 — Write a Delete-If-Exists Helper

See DirectoryNotFoundException on a missing folder, then wrap the call in a small helper that never throws for that reason.

C#
using System;
using System.IO;

class Program {
    static bool DeleteIfExists(string path) {
        if (!Directory.Exists(path)) return false;
        Directory.Delete(path, true);
        return true;
    }

    static void Main() {
        string folder = Path.Combine(Path.GetTempPath(), "codetofun-delete-missing");
        if (Directory.Exists(folder)) Directory.Delete(folder, true);

        try {
            Directory.Delete(folder);
        } catch (DirectoryNotFoundException) {
            Console.WriteLine("Nothing to delete");
        }

        Directory.CreateDirectory(folder);
        Console.WriteLine("First call: " + DeleteIfExists(folder));
        Console.WriteLine("Second call: " + DeleteIfExists(folder));
    }
}

How It Works

  1. Missing means exception. The folder was removed at the start, so Delete(folder) throws DirectoryNotFoundException and the catch block reports it.
  2. Guard first. DeleteIfExists checks Directory.Exists and returns false early, so it can be called without a try block.
  3. Return what happened. The helper returns true only when it really removed something, which makes the result easy to log.
  4. Two calls, two answers. The folder exists on the first call, so the result is True. On the second call it is gone, so the result is False.

Frequently Asked Questions

Use Directory.Delete(path, true). The true argument means recursive, so the folder, its files, and its subfolders are all removed.
The most common reason is that the folder is not empty and you did not pass true. A program holding a file or folder open (or the current directory being inside it) can cause it too.
Directory.Delete throws DirectoryNotFoundException. Check with Directory.Exists first, or catch the exception.
No. The folder is removed permanently. There is no undo, so check the path carefully before you use the recursive option.
Only on a folder you created yourself, such as one inside Path.GetTempPath(). Never point a recursive delete at a folder you did not create.

Next: EnumerateDirectories()

Stream subfolder paths one at a time without loading them all first.

EnumerateDirectories() tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

8 people found this page helpful