---
title: "How can you securely delete a file (overwrite before delete)?"  
description: "How can you securely delete a file (overwrite before delete)?"  
author: "ICSM Computer"  
published: 2025-05-18  
updated: 2025-05-18  
canonical: https://www.mindstick.com/interview/34134/how-can-you-securely-delete-a-file-overwrite-before-delete  
category: "c#"  
tags: ["c#", "file handling"]  
reading_time: 3 minutes  

---

# How can you securely delete a file (overwrite before delete)?

To **securely delete a file** in C#, you should overwrite its content with random or zeroed data **before deleting it**. This prevents file recovery tools from retrieving the original content from disk.

## Step-by-Step: Secure File Deletion

1. **Overwrite the file content**
2. **Flush to disk**
3. **Delete the file**

### Example: Overwrite with Random Data, Then Delete

```cs
using System;
using System.IO;
using System.Security.Cryptography;

public static class SecureFileDelete
{
    public static void SecureDelete(string filePath, int overwritePasses = 1)
    {
        if (!File.Exists(filePath))
            return;

        FileInfo fileInfo = new FileInfo(filePath);
        long fileLength = fileInfo.Length;

        using (FileStream fs = new FileStream(filePath, FileMode.Open, FileAccess.Write))
        {
            byte[] buffer = new byte[4096];
            RandomNumberGenerator rng = RandomNumberGenerator.Create();

            for (int pass = 0; pass < overwritePasses; pass++)
            {
                fs.Position = 0;
                long totalWritten = 0;

                while (totalWritten < fileLength)
                {
                    rng.GetBytes(buffer);
                    int bytesToWrite = (int)Math.Min(buffer.Length, fileLength - totalWritten);
                    fs.Write(buffer, 0, bytesToWrite);
                    totalWritten += bytesToWrite;
                }

                fs.Flush(true); // flush to disk
            }
        }

        // Optionally truncate and delete
        File.Delete(filePath);
    }
}
```

## Parameters

1. `overwritePasses`: Number of times to overwrite the file (1 is usually enough; 3 for higher security).
2. Uses `RandomNumberGenerator` instead of `Random` for cryptographic quality.

## Notes

1. This works best on traditional hard drives.
2. On SSDs, the OS and drive firmware may ignore overwrite attempts due to wear leveling and deferred writes — **use full-disk encryption** or OS-level secure delete for SSDs.
3. Truncating the file to zero bytes after overwrite is optional, but often unnecessary.

## Answers

### Answer by ICSM Computer

To **securely delete a file** in C#, you should overwrite its content with random or zeroed data **before deleting it**. This prevents file recovery tools from retrieving the original content from disk.

## Step-by-Step: Secure File Deletion

1. **Overwrite the file content**
2. **Flush to disk**
3. **Delete the file**

### Example: Overwrite with Random Data, Then Delete

```cs
using System;
using System.IO;
using System.Security.Cryptography;

public static class SecureFileDelete
{
    public static void SecureDelete(string filePath, int overwritePasses = 1)
    {
        if (!File.Exists(filePath))
            return;

        FileInfo fileInfo = new FileInfo(filePath);
        long fileLength = fileInfo.Length;

        using (FileStream fs = new FileStream(filePath, FileMode.Open, FileAccess.Write))
        {
            byte[] buffer = new byte[4096];
            RandomNumberGenerator rng = RandomNumberGenerator.Create();

            for (int pass = 0; pass < overwritePasses; pass++)
            {
                fs.Position = 0;
                long totalWritten = 0;

                while (totalWritten < fileLength)
                {
                    rng.GetBytes(buffer);
                    int bytesToWrite = (int)Math.Min(buffer.Length, fileLength - totalWritten);
                    fs.Write(buffer, 0, bytesToWrite);
                    totalWritten += bytesToWrite;
                }

                fs.Flush(true); // flush to disk
            }
        }

        // Optionally truncate and delete
        File.Delete(filePath);
    }
}
```

## Parameters

1. `overwritePasses`: Number of times to overwrite the file (1 is usually enough; 3 for higher security).
2. Uses `RandomNumberGenerator` instead of `Random` for cryptographic quality.

## Notes

1. This works best on traditional hard drives.
2. On SSDs, the OS and drive firmware may ignore overwrite attempts due to wear leveling and deferred writes — **use full-disk encryption** or OS-level secure delete for SSDs.
3. Truncating the file to zero bytes after overwrite is optional, but often unnecessary.


---

Original Source: https://www.mindstick.com/interview/34134/how-can-you-securely-delete-a-file-overwrite-before-delete

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
