Securely delete files beyond recovery using overwriting techniques, ensuring data privacy and compliance with data protection regulations
Permanently delete files beyond recovery using secure deletion methods including overwriting, DoD standards, and verification.
Activate this skill when the user:
# Basic secure deletion (3 passes)
shred -vfz -n 3 sensitive_file.txt
# DoD 5220.22-M standard (7 passes)
shred -vfz -n 7 file.pdf
# Gutmann method (35 passes)
shred -vfz -n 35 top_secret.doc
# Delete and remove file
shred -vfzu -n 3 file.txt
# Secure delete entire directory
find /path/to/dir -type f -exec shred -vfzu -n 3 {} \;
# Install srm
sudo apt-get install secure-delete
# Simple deletion (7 passes)
srm file.txt
# Fast deletion (1 pass)
srm -f file.txt
# Recursive directory deletion
srm -r /path/to/directory
# DoD compliant
srm -D file.txt
# Wipe file with default passes
wipe file.txt
# Quick wipe (4 passes)
wipe -q file.txt
# Recursive wipe
wipe -r directory/
# Force wipe (no confirmation)
wipe -f file.txt
# Overwrite file with zeros
dd if=/dev/zero of=file.txt bs=1M count=10
# Overwrite with random data
dd if=/dev/urandom of=file.txt bs=1M count=10
# Wipe entire disk/partition (BE CAREFUL!)
sudo dd if=/dev/zero of=/dev/sdX bs=1M status=progress
# Wipe free space on partition
sfill -f /mount/point
# Quick free space wipe
sfill -I /mount/point
# Wipe swap space
sudo swapoff -a
sudo dd if=/dev/zero of=/dev/swap_partition
sudo mkswap /dev/swap_partition
sudo swapon -a
| Level | Method | Passes | Speed | Use Case |
|---|---|---|---|---|
| Low | Single zero pass | 1 | Fast | Non-sensitive data |
| Medium | DoD 5220.22-M | 3-7 | Medium | Business documents |
| High | Gutmann | 35 | Slow | Top secret data |
| SSD | Trim + Encrypt | N/A | Fast | Solid state drives |
Problem: Traditional overwriting doesn't work on SSDs due to wear leveling.
Solutions:
# TRIM (marks blocks for deletion)
sudo fstrim -v /mount/point
# Secure Erase (ATA command)
sudo hdparm --user-master u --security-set-pass password /dev/sdX
sudo hdparm --user-master u --security-erase password /dev/sdX
# Encryption before deletion
cryptsetup luksFormat /dev/sdX
# Then delete encryption key
import os
import random
import hashlib
class SecureDelete:
def secure_delete_file(self, filepath, passes=3):
"""Securely delete file with multiple overwrites"""
file_size = os.path.getsize(filepath)
with open(filepath, "ba+", buffering=0) as f:
for pass_num in range(passes):
f.seek(0)
if pass_num == 0:
# First pass: zeros
f.write(b'\x00' * file_size)
elif pass_num == 1:
# Second pass: ones
f.write(b'\xFF' * file_size)
else:
# Subsequent passes: random data
f.write(os.urandom(file_size))
f.flush()
os.fsync(f.fileno())
# Delete the file
os.remove(filepath)
print(f"Securely deleted: {filepath} ({passes} passes)")
def verify_deletion(self, filepath):
"""Verify file no longer exists"""
return not os.path.exists(filepath)
def secure_delete_directory(self, dirpath, passes=3):
"""Recursively secure delete directory"""
for root, dirs, files in os.walk(dirpath, topdown=False):
for name in files:
filepath = os.path.join(root, name)
self.secure_delete_file(filepath, passes)
for name in dirs:
os.rmdir(os.path.join(root, name))
os.rmdir(dirpath)
Always provide: