Skip to main content

How to Delete a File with File.Delete in C#

· 6 min read
Jagdish Kumawat
Founder @ Dewiride

File.Delete("draft.txt") removes the file named draft.txt. When the file isn't there, it does nothing, so you don't have to check for the file first.

The .NET API reference: Deletes the specified file. It takes one path, and the reference on that parameter: The name of the file to be deleted. Wildcard characters are not supported. A name such as draft.txt is looked up in the current folder: Relative path information is interpreted as relative to the current working directory.

Syntax​

Syntax
File.Delete(<path>);
PartWhat it is
File.DeleteA static method of the File class. It returns nothing (void)
<path>The one file to delete: a name such as draft.txt, a relative path, or a full path, with no wildcards such as *

The Example​

A console app called FileDelete creates draft.txt, so there's a file to delete, and checks with File.Exists before and after File.Delete. Then it deletes the same file a second time, when it's already gone.

Prerequisites​

  • The .NET 10 SDK. dotnet --version prints a version that starts with 10.0.

Step 1: Create a Console App​

Terminal
dotnet new console -n FileDelete
cd FileDelete
Output
The template "Console App" was created successfully.

Processing post-creation actions...
Restoring /Users/you/source/FileDelete/FileDelete.csproj:
Determining projects to restore...
Restored /Users/you/source/FileDelete/FileDelete.csproj (in 45 ms).
Restore succeeded.

dotnet new console -n FileDelete creates a folder named FileDelete with a project file and a Program.cs, and cd moves into it. Everything this example uses is part of .NET, so there's no package to add. Open the folder in VS Code, or FileDelete.csproj in Visual Studio.

Step 2: Write the Code​

Replace everything in Program.cs. First, a file to delete:

Program.cs (a file to delete)
// Create a file, so there's something to delete.
File.WriteAllText("draft.txt", "An old draft");
Console.WriteLine($"Before: draft.txt exists: {File.Exists("draft.txt")}");

File.WriteAllText creates draft.txt with a line of text in it, and File.Exists reports whether the file is there.

Then the delete:

Program.cs (delete it)
// File.Delete removes it.
File.Delete("draft.txt");
Console.WriteLine($"After: draft.txt exists: {File.Exists("draft.txt")}");

File.Delete("draft.txt") deletes the file from the current folder, and File.Exists checks again.

Then the same delete, once more:

Program.cs (delete it again)
// Deleting a file that isn't there does nothing.
File.Delete("draft.txt");
Console.WriteLine("Deleting it a second time threw no exception.");

This time there's no file to delete. The reference: If the file to be deleted does not exist, no exception is thrown.

Step 3: Run It​

Run it in VS Code's terminal, or press Ctrl+F5 in Visual Studio:

Terminal
dotnet run
Output
Before: draft.txt exists: True
After: draft.txt exists: False
Deleting it a second time threw no exception.

macOS-style terminal window titled Terminal — FileDelete showing dotnet run printing Before: draft.txt exists: True, After: draft.txt exists: False and Deleting it a second time threw no exception.

draft.txt was there before File.Delete, True, and gone after it, False. The second call found no file, did nothing, and the program went on to its last line.

The Complete File​

macOS-style code window titled Program.cs — FileDelete showing File.WriteAllText creating draft.txt, a File.Exists check, File.Delete followed by another File.Exists check, and a second File.Delete of the same file followed by a message that no exception was thrown

Program.cs
// Create a file, so there's something to delete.
File.WriteAllText("draft.txt", "An old draft");
Console.WriteLine($"Before: draft.txt exists: {File.Exists("draft.txt")}");

// File.Delete removes it.
File.Delete("draft.txt");
Console.WriteLine($"After: draft.txt exists: {File.Exists("draft.txt")}");

// Deleting a file that isn't there does nothing.
File.Delete("draft.txt");
Console.WriteLine("Deleting it a second time threw no exception.");

Common Mistakes​

MistakeWhat happensDo this instead
Using a wildcard, as in File.Delete("*.tmp")With a.tmp and b.tmp there, a check printed After File.Delete("*.tmp"): a.tmp True, b.tmp True. Nothing was deleted, and no exception said soFind the files first, and delete each one, as the FAQ below shows
Passing a folder's path, as in File.Delete("old") for a folder named oldThe app stopped with Unhandled exception. System.UnauthorizedAccessException: Access to the path '/Users/you/source/FileDelete/old' is denied. The reference lists that exception for when path is a directory.Use Directory.Delete for a folder. Deletes an empty directory from a specified path. A check with Directory.Delete("old") then printed old exists: False
Deleting a file in a folder that isn't there, as in File.Delete("archive/draft.txt")The app stopped with Unhandled exception. System.IO.DirectoryNotFoundException: Could not find a part of the path '/Users/you/source/FileDelete/archive/draft.txt'. A missing file is fine, but a missing folder isn'tMake sure the folder is there, for example with Directory.Exists, before you delete files in it

FAQ​

Do I need File.Exists before File.Delete?​

Not for a missing file: the example's second File.Delete threw no exception. A missing folder is a different case, as Common Mistakes shows.

How do I delete all the files that match a pattern?​

List them with Directory.GetFiles, which does take a pattern, and delete them one by one. The reference: Returns the names of files (including their paths) that match the specified search pattern in the specified directory.

Delete every .tmp file in the current folder
// Find the matching files first, then delete each one.
foreach (string file in Directory.GetFiles(".", "*.tmp"))
{
File.Delete(file);
}
Console.WriteLine($"After deleting each match: a.tmp {File.Exists("a.tmp")}, b.tmp {File.Exists("b.tmp")}");

With a.tmp and b.tmp there, this printed After deleting each match: a.tmp False, b.tmp False.

Conclusion​

File.Delete(path) deletes one file, and does nothing when the file isn't there. Give it the path of a file, not a folder and not a pattern, and make sure the folder in the path exists. To delete many files, list them with Directory.GetFiles and delete each one.

Additional Resources​