You need to enable JavaScript to run this app.
最新活动
大模型
产品
解决方案
定价
生态与合作
支持与服务
开发者
了解我们

如何彻底删除文件使其不可恢复?当前file.delete()存在安全风险

Properly Deleting Files Permanently (Preventing Recovery)

Great question—this is a super common pitfall with standard file deletion methods. The problem with file.delete() (regardless of the language you're using) is that it only removes the file system's pointer to the file. The actual binary data still lives on your storage device until it's overwritten by new data, which is why recovery tools can dig it up.

Here are the most reliable ways to fix this:

1. Overwrite the File Before Deletion

The core approach here is to overwrite the file's content multiple times with random data (or zeros) before deleting it. This makes the original data effectively unrecoverable for most practical purposes.

Java Example (since you mentioned file.delete())

Here's a robust implementation you can use:

import java.io.File;
import java.io.RandomAccessFile;
import java.io.IOException;
import java.util.Random;

public class SecureFileDeleter {
    public static void secureDelete(File file) throws IOException {
        if (!file.exists() || !file.isFile()) {
            return;
        }

        RandomAccessFile raf = new RandomAccessFile(file, "rw");
        long fileLength = raf.length();
        byte[] buffer = new byte[4096]; // Use a reasonable buffer size
        Random random = new Random();

        // First pass: overwrite with random bytes
        for (long i = 0; i < fileLength; i += buffer.length) {
            raf.seek(i);
            random.nextBytes(buffer);
            int bytesToWrite = (int) Math.min(buffer.length, fileLength - i);
            raf.write(buffer, 0, bytesToWrite);
        }

        // Optional second pass: overwrite with zeros to hide shredding traces
        raf.seek(0);
        byte[] zeros = new byte[4096];
        for (long i = 0; i < fileLength; i += zeros.length) {
            raf.seek(i);
            int bytesToWrite = (int) Math.min(zeros.length, fileLength - i);
            raf.write(zeros, 0, bytesToWrite);
        }

        raf.close();
        // Now delete the file as usual
        if (!file.delete()) {
            throw new IOException("Failed to delete file after secure overwrite");
        }
    }
}

Python Example

If you're working in Python, here's a similar function:

import os
import random

def secure_delete(file_path, num_passes=3):
    if not os.path.isfile(file_path):
        return

    file_size = os.path.getsize(file_path)

    with open(file_path, 'wb') as f:
        for _ in range(num_passes):
            f.seek(0)
            # Write random bytes to fill the file
            f.write(random.randbytes(file_size))
            # Flush and sync to ensure data is written to disk (not just cached)
            f.flush()
            os.fsync(f.fileno())

    # Delete the file once overwriting is done
    os.remove(file_path)

2. Use OS-Specific Tools

For even stronger security, leverage built-in OS tools designed for secure deletion:

  • Windows: Use sdelete (from Microsoft Sysinternals) with the command sdelete -p 3 -z filename.txt ( -p 3 for 3 overwrite passes, -z to finish with zeros). Alternatively, the cipher tool can overwrite free space if you've already deleted the file, but overwriting first is better.
  • Linux/macOS: Use the shred command:
    shred -u -z -n 3 my-sensitive-file.txt
    
    Breakdown:
    • -n 3: Overwrite 3 times with random data
    • -z: Final pass with zeros to obscure that the file was shredded
    • -u: Delete the file after overwriting

Critical Notes for SSDs

If you're dealing with Solid-State Drives (SSDs), standard overwriting might not work as well due to wear leveling. Instead, use the drive's built-in secure erase feature:

  • On Linux, tools like hdparm can trigger this (check your drive's documentation first)
  • On Windows, use manufacturer-specific utilities (like Samsung Magician, Crucial Storage Executive)
  • This tells the SSD controller to mark all blocks as unused, making recovery impossible without specialized (and expensive) hardware.

Final Tips

  • Adjust the number of overwrites based on your security needs: 1-3 passes is enough for most cases (the old 7-pass DoD standard is overkill for modern drives)
  • Make sure your application has write permissions for the file to overwrite it, and delete permissions to remove it
  • For extremely sensitive data, consider using encryption before storing the file—then deleting the encryption key renders the data unreadable even if recovered

Content of the question originates from Stack Exchange, question author Ankit Shukla

火山引擎 最新活动