Delete a file in C#

In C#, you can use the File.Delete() method from the System.IO namespace to delete files from the file system. This method permanently removes the specified file if it exists.

Syntax

Following is the syntax for deleting a file −

File.Delete(string path);

Parameters

  • path − A string specifying the path of the file to be deleted. This can be a relative or absolute path.

Using File.Delete() Method

The File.Delete() method removes the file at the specified path. If the file does not exist, the method does not throw an exception −

Example

using System;
using System.IO;

public class Program {
    public static void Main() {
        string myPath = @"C:\temp\example.txt";
        
        // Create a file first for demonstration
        File.WriteAllText(myPath, "This is a test file.");
        Console.WriteLine("File created: " + File.Exists(myPath));
        
        // Delete the file
        Console.WriteLine("Deleting file...");
        File.Delete(myPath);
        
        // Check if file still exists
        Console.WriteLine("File exists after deletion: " + File.Exists(myPath));
    }
}

The output of the above code is −

File created: True
Deleting file...
File exists after deletion: False

Safe File Deletion with Exception Handling

When deleting files, it's good practice to handle potential exceptions such as file access permissions or file locks −

Example

using System;
using System.IO;

public class Program {
    public static void Main() {
        string filePath = @"C:\temp\protected_file.txt";
        
        try {
            // Create a test file
            File.WriteAllText(filePath, "Protected content");
            Console.WriteLine("File created successfully");
            
            // Check if file exists before deletion
            if (File.Exists(filePath)) {
                File.Delete(filePath);
                Console.WriteLine("File deleted successfully");
            } else {
                Console.WriteLine("File does not exist");
            }
        }
        catch (UnauthorizedAccessException ex) {
            Console.WriteLine("Access denied: " + ex.Message);
        }
        catch (IOException ex) {
            Console.WriteLine("IO error occurred: " + ex.Message);
        }
        catch (Exception ex) {
            Console.WriteLine("An error occurred: " + ex.Message);
        }
    }
}

The output of the above code is −

File created successfully
File deleted successfully

Deleting Multiple Files

You can delete multiple files by using patterns with Directory.GetFiles() and File.Delete()

Example

using System;
using System.IO;

public class Program {
    public static void Main() {
        string directoryPath = @"C:\temp";
        
        // Create some test files
        for (int i = 1; i <= 3; i++) {
            string fileName = Path.Combine(directoryPath, $"test{i}.txt");
            File.WriteAllText(fileName, $"Content of file {i}");
        }
        
        Console.WriteLine("Created 3 test files");
        
        // Delete all .txt files in the directory
        string[] txtFiles = Directory.GetFiles(directoryPath, "*.txt");
        
        foreach (string file in txtFiles) {
            File.Delete(file);
            Console.WriteLine($"Deleted: {Path.GetFileName(file)}");
        }
        
        Console.WriteLine($"Total files deleted: {txtFiles.Length}");
    }
}

The output of the above code is −

Created 3 test files
Deleted: test1.txt
Deleted: test2.txt
Deleted: test3.txt
Total files deleted: 3

Key Rules

  • The File.Delete() method does not throw an exception if the file does not exist.

  • The file is permanently deleted and not moved to the Recycle Bin.

  • If the file is read-only, an UnauthorizedAccessException will be thrown.

  • If the file is in use by another process, an IOException will be thrown.

Conclusion

The File.Delete() method in C# provides a simple way to permanently remove files from the file system. Always use proper exception handling when deleting files to handle access permissions and file locks gracefully.

Updated on: 2026-03-17T07:04:35+05:30

1K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements