Secure deletion of data on a drive (attempt 1)

Last Update: 26.02.2009. By kerim in python

A while ago i came upon an article about secure deletion of files. An interesting topic as i remember myself wondering why it would be needed to overwrite a file up to 30 times in order to be sure it can’t be restored somehow.

There are many “secure deletion” tools out there.

And my tries to find a solution (in java or python) that would overwrite the exact same sectors a files used so far were fruitless. This is a “features” that is very dependent on the os and i guess you need some more os dependend apis (and programming languages) for such a thing.

Of course there is one alternative, although it is not really reasonable if every now and then you want to delete one single file of a few kbytes. Instead of overwriting the sectors you might (after deleting the files you want) just as well simply fill the drive up to its limit with random data. That way you should easily overwrite ANY sector. I use it after every few GBytes of new/deleted/replaced files.

import os, random, sys  
usage="""
run using PATH TO PYTHON securedelete.py DRIVE
e.g. "c:\python25\python securedelete.py d:\\
"""

if len (sys.argv)!=2:
  print usage
else:
  path=os.path.join(sys.argv[1],'temp.dmp')
  try:
    filler=open(path,'wb')
    random.seed()
    while(True):
      filler.write(1000*str(random.random()))
  except:
     filler.close()
  finally:
     os.remove(path)

Not very performant and somewhat shooting at flies with a pumpgun … but it should work and it is done in 5 minutes: Let’s see if we can find a better solution.