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
Call
What 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
Parameter
Description
path
The folder to remove. It must exist.
recursive
true removes subfolders and files too. false (the default) removes only an empty folder.
Return Value
Return Value
Description
void
Nothing 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));
}
}
Output
True
False
How It Works
Start with something to delete.CreateDirectory makes the empty demo folder, and the first Exists prints True.
No second argument.Delete(folder) is the plain form. It succeeds because nothing is inside the folder.
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));
}
}
Output
Folder is not empty
True
False
How It Works
Give it contents. The demo folder now holds one subfolder, inner, and one file, a.txt.
Plain delete refuses. The folder is not empty, so Delete(root) throws IOException. That is a safety net against deleting by accident.
Nothing was lost. The failed call changed nothing, so Exists still prints True.
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));
}
}
Output
Nothing to delete
First call: True
Second call: False
How It Works
Missing means exception. The folder was removed at the start, so Delete(folder) throws DirectoryNotFoundException and the catch block reports it.
Guard first.DeleteIfExists checks Directory.Exists and returns false early, so it can be called without a try block.
Return what happened. The helper returns true only when it really removed something, which makes the result easy to log.
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.