Click here to Skip to main content
13,199,685 members (28,888 online)
Click here to Skip to main content
Add your own
alternative version

Tagged as


2 bookmarked
Posted 20 May 2011

Super Delete that file!

, 20 May 2011
Rate this:
Please Sign up or sign in to vote.
--- UPDATED with better console stuff and now using the crypto random number generator.I know that some of you have found yourself in that situation that requires you to delete something from your hard drive permanently, thus rendering your data unrecoverable.Well, there isn't a good way to do
--- UPDATED with better console stuff and now using the crypto random number generator.

I know that some of you have found yourself in that situation that requires you to delete something from your hard drive permanently, thus rendering your data unrecoverable.

Well, there isn't a good way to do that through C# without some tomfoolery... so I hereby present you with SuperDelete. This code will permanently delete whatever file you pass in, overwriting the existing data with cryptographically random numbers a specified number of times before deleting the file. This renders the previously existing data pretty much useless to anyone.

As long as data is being flushed to the drive after each file write (hence the foreach over the FileStream), and your system doesn't have some sort of advanced write-caching in place that will cache data over multiple file opens and closes, your data will be as good as dead once this code is finished with it.

The recommended overwrite of 10 times is to cover the nominal servo-positioning errors that occur in all hard drives, however you can push this up to whatever value you want to make yourself feel secure.

I also recommend that you read this article on data storage and recovery:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Security.Cryptography;

namespace SuperDelete
class Program
// For large files this is the best performing buffer size
static byte[] buffer = new byte[32768];

/// <span class="code-SummaryComment"><summary>
</span>/// Main application entry point
/// Usage: SuperDelete.exe filename number_of_wipes
/// Example: SuperDelete.exe \"C:\\mySecrets.txt\" 10"
/// <span class="code-SummaryComment"></summary>
</span>static void Main(string[] args)
Console.WriteLine("Usage: SuperDelete.exe <filename> <number of wipes>");
Console.WriteLine("Example: SuperDelete.exe \"C:\\mySecrets.txt\" 10");
Console.WriteLine("Are you sure you want to SuperDelete the file {0}?  (Y/N)", args[0]);

if (Console.ReadKey().Key == ConsoleKey.Y)
SuperDeleteFile(args[0], Convert.ToInt32(args[1]));
catch (Exception e)
Console.WriteLine("Failed to super delete :(");

/// <span class="code-SummaryComment"><summary>
</span>/// Super delete that file!
/// <span class="code-SummaryComment"></summary>
</span>/// <span class="code-SummaryComment"><param name="fileName">Name of the file to delete</param>
</span>/// <span class="code-SummaryComment"><param name="passes">Number of passes to use in wiping the file.  Recommended is 10</param>
</span>public static void SuperDeleteFile(string fileName, int passes)

int cursorTop = Console.CursorTop;
int cursorLeft = Console.CursorLeft;            

for (int i = 0; i < passes; i++)
using (FileStream fileToOverwrite = File.Open(fileName, FileMode.Open, FileAccess.Write, FileShare.None))

// 24 is the RSA-AES CSP
RNGCryptoServiceProvider rng = new RNGCryptoServiceProvider(new CspParameters(24));


// Writing more than the file size (unless it comes out to be the exact size of the buffer *n) will mask the original filesize
long bytesToWrite = fileToOverwrite.Length;
long originalFileSize = fileToOverwrite.Length;

while (bytesToWrite > 0)
Console.CursorLeft = cursorLeft;
Console.CursorTop = cursorTop;

string messageOne = string.Format("Pass {0}, wiping bytes {1} - {2} of {3}. ", i, originalFileSize - bytesToWrite, (originalFileSize - bytesToWrite) + buffer.Length, originalFileSize);
string messageTwo = string.Format("This pass has %{0} left to complete.", Convert.ToInt64((Convert.ToDouble(bytesToWrite) / Convert.ToDouble(originalFileSize)) * 100));

Console.WriteLine(PadToLength(messageOne, Console.BufferWidth));
Console.WriteLine(PadToLength(messageTwo, Console.BufferWidth));

fileToOverwrite.Write(buffer, 0, buffer.Length);

bytesToWrite -= buffer.Length;


static string PadToLength(string original, int length)
if (original.Length >= length)
return original;

StringBuilder sb = new StringBuilder(original);

for (int i = original.Length +1; i < length; i++)
sb.Append(" ");

return sb.ToString();

For the complete program, including the disabling of write-caching, please purchase my Super Delete application.


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Aron Weiler
Technical Lead CareFusion
United States United States
I just looooove software.

Check out my technical blog here: The Fyslexic Duck. You can find most of what I've put on CodeProject there, plus some additional technical articles.

You may also be interested in...

Comments and Discussions

GeneralNot effective on all storage devices Pin
supercat920-May-11 16:09
membersupercat920-May-11 16:09 
On some hard drives, if the controller detects that a sector is getting 'weak', it can be remapped to another part of the disk. If that occurs, a copy of the data may remain on the original abandoned sector and no amount of rewriting the block will erase the old copy.

Also, on many flash devices, data may only be erased in groups of 32 or more sectors. To work around this, a command to rewrite a sector will cause the controller to remap the sector to a new blank spot on the disk and mark the old copy as abandoned. If one has a drive with 32 gigs of empty space, one might rewrite a one-meg file a thousand times without physically erasing the data.
GeneralRe: Not effective on all storage devices Pin
AspDotNetDev20-May-11 16:25
mvpAspDotNetDev20-May-11 16:25 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web03 | 2.8.171020.1 | Last Updated 20 May 2011
Article Copyright 2011 by Aron Weiler
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid