C# File Delete() Method

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

Definition

In C#, File.Delete() removes a file from disk. The file is deleted for good: it does not go to the Recycle Bin.

If the file does not exist, the call does nothing and does not throw. That makes it safe to call as a cleanup step. An error does happen when the folder in the path does not exist.

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

Syntax

Pass the path of the file to remove.

C#
string path = Path.Combine(Path.GetTempPath(), "codetofun-delete.txt");
File.WriteAllText(path, "temporary");

File.Delete(path);

Console.WriteLine(File.Exists(path));
CallWhat it does
File.Delete(path)Removes the file. Quiet if the file is already missing.
if (File.Exists(p)) File.Delete(p);The check is optional, because Delete already ignores a missing file.
File.Delete(folderPath)Not for folders. It throws UnauthorizedAccessException; use Directory.Delete.

File.Delete works with a path. FileInfo.Delete removes the file for an object you already hold, and Directory.Delete removes folders.

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

Parameters

ParameterDescription
pathThe file to delete. It may be absolute or relative. It must not be a folder.

Return Value

Return ValueDescription
voidNothing is returned. After the call the file is gone, or it was never there.

C# Examples

Example 1 — Create a File, Delete It, and Confirm

The simplest cleanup: make a file, remove it, and ask whether it is still there.

C#
using System;
using System.IO;

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

        string path = Path.Combine(root, "temp.txt");
        File.WriteAllText(path, "temporary");
        Console.WriteLine(File.Exists(path));

        File.Delete(path);
        Console.WriteLine(File.Exists(path));

        Directory.Delete(root, true);
    }
}

How It Works

  1. A file to remove. WriteAllText creates temp.txt in the demo folder, and the first check prints True.
  2. One call removes it. File.Delete(path) takes the file off the disk right away. There is no return value.
  3. The second check proves it. Exists now prints False. The demo folder is removed at the end so nothing is left in the temp directory.

Example 2 — Deleting a File That Is Not There

Call Delete twice in a row and see that the second call is quiet.

C#
using System;
using System.IO;

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

        string path = Path.Combine(root, "once.txt");
        File.WriteAllText(path, "x");

        File.Delete(path);
        File.Delete(path);
        Console.WriteLine("Second delete did not throw");

        try {
            File.Delete(Path.Combine(root, "no-such-folder", "a.txt"));
        } catch (DirectoryNotFoundException) {
            Console.WriteLine("Missing folder is an error");
        }

        Directory.Delete(root, true);
    }
}

How It Works

  1. Delete twice. The first call removes once.txt. The second call finds nothing to remove and simply returns.
  2. No guard needed. This is why cleanup code can call File.Delete without checking File.Exists first.
  3. A missing folder is different. The path points into no-such-folder, which does not exist, so the call throws DirectoryNotFoundException and the catch block runs.

Example 3 — Delete All .tmp Files in a Folder

List the files that match a pattern and delete only those.

C#
using System;
using System.IO;

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

        File.WriteAllText(Path.Combine(root, "a.tmp"), "1");
        File.WriteAllText(Path.Combine(root, "b.tmp"), "2");
        File.WriteAllText(Path.Combine(root, "keep.txt"), "3");

        int removed = 0;
        foreach (string file in Directory.GetFiles(root, "*.tmp")) {
            File.Delete(file);
            removed++;
        }

        Console.WriteLine("Removed: " + removed);
        foreach (string left in Directory.GetFiles(root)) {
            Console.WriteLine(Path.GetFileName(left));
        }

        Directory.Delete(root, true);
    }
}

How It Works

  1. Three files, two to remove. The folder holds a.tmp, b.tmp, and keep.txt. Only the two .tmp files should go.
  2. Filter first, then delete. GetFiles(root, "*.tmp") returns an array, so the folder is not being changed while the loop reads the list.
  3. Count the work. The counter ends at 2, and the program prints it.
  4. Only the keeper is left. A second listing shows just keep.txt. Always test a pattern on a temp folder before using it on real data.

Frequently Asked Questions

No. A missing file is ignored. It does throw DirectoryNotFoundException when the folder in the path does not exist.
No. The file is removed permanently and cannot be restored by your program. Double-check the path before you call it.
The path may be a folder, you may lack permission, or on Windows the file may be read-only. Clear the read-only flag with File.SetAttributes(path, FileAttributes.Normal) and try again.
Usually the file is open in your own program or another program. Close the stream (use a using block) or wait until the other program is done.
Use Directory.Delete(path) for an empty folder, or Directory.Delete(path, true) to remove the folder with everything inside it. File.Delete only works on files.

Next: Encrypt()

Encrypt a file with NTFS EFS so only the current Windows account can read it.

Encrypt() 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.

14 people found this page helpful