Infra13 min read
How to wipe a hard drive or SSD before selling it
Why deleted files can be recovered from HDDs, SSDs and USB drives, how a small Rust tool finds them, and how to really wipe a drive before you sell it.

You're about to sell your old laptop, give away an external drive or pass on that USB stick from the drawer. You delete everything, empty the trash, maybe even format it. Done, right?
Not quite. rscovery is a fork I made of a tool in Rust with Tauri and React that reads a drive block by block and looks for files the system already considers deleted. And if you want to see this happen in a real case, this video (in Portuguese) explains everything you need to know.
This post is about what I learned working on it: why a deleted file is still there, how a simple tool finds it, and above all how to actually protect yourself before the drive leaves your hands.
TL;DR#
- Deleting a file only removes it from the index. The bytes stay on the drive until something writes over them.
- A quick format doesn't erase the data either. It recreates an empty index and leaves the rest as it was.
- You can find files with no index at all: every format has a start signature and many have an end one. That's what rscovery looks for.
- On an HDD, one pass of zeros does the job. An SSD is a different story: TRIM helps, but overwriting doesn't reach everything.
- The protection that works is encryption from day one. When it's time to sell, erasing becomes throwing away the key.
What happens when you delete a file#
A drive is split into blocks, and the file system keeps an index: which file lives in which blocks, with its name, size and date. When you delete a file, the system doesn't go around zeroing block after block. It just removes the index entry and marks those blocks as free.
That's why deleting is instant, even for a 10 GB video. The PhotoRec documentation, one of the best-known recovery tools, explains it the same way: the information about where the file was goes away, but the contents stay on the drive until they're overwritten.
A quick format does something similar on a larger scale. The format command documentation says /Q only deletes the file table and the root directory. The actual data stays intact.
How rscovery finds a file with no index#
If the index is gone, how do you know where a photo starts? By its signature, also called magic bytes: almost every file format starts with a fixed sequence of bytes, and many end with another.
- JPEG starts with
FF D8and ends withFF D9(the SOI and EOI markers). - PNG starts with
89 50 4E 47 0D 0A 1A 0Aand ends with theIENDchunk, which the PNG specification requires to be the last one. - PDF starts with
%PDF-and ends with%%EOF. - ZIP starts with
PK\x03\x04and ends with the end of central directory record,PK\x05\x06(ZIP format APPNOTE).
This technique has a name: file carving. PhotoRec does it far more thoroughly. rscovery is the teaching version, good for understanding the mechanism from the inside.
The Rust code#
In rscovery, each format is just a struct with the two signatures and a maximum size. This is the PNG one:
#[tauri::command]
pub async fn find_png(app_handle: tauri::AppHandle, path: &str) -> Result<(), String> {
MagicByte {
signature: &[0x89, 0x50, 0x4E, 0x47, 0x0D, 0x0A, 0x1A, 0x0A],
end: &[0x49, 0x45, 0x4E, 0x44, 0xAE, 0x42, 0x60, 0x82],
extension: "png",
max_size: 200 * 1024 * 1024,
name: "PNG",
is_image: true,
}
.extract(app_handle, path, 300)
}extract opens the drive like any other file, reads it in 32 MB chunks and walks byte by byte through a tiny state machine: either it's looking for the start, or it's collecting bytes until it finds the end. The comments are mine, the rest is the code from the repository:
for &b in buffer[..bytes_read].iter() {
if searching_file {
file_buffer.push(b);
if file_buffer.len() > self.max_size {
searching_file = false;
file_buffer.clear();
continue;
}
// found the end signature: the file is complete
if file_buffer.len() >= self.end.len()
&& file_buffer[file_buffer.len() - self.end.len()..] == *self.end
{
// images are kept only if they decode, deduped by sha256 (omitted)
searching_file = false;
file_buffer.clear();
sig_match_index = 0;
}
continue;
}
// still looking for the start signature, one byte at a time
if b == self.signature[sig_match_index] {
sig_match_index += 1;
if sig_match_index == self.signature.len() {
searching_file = true;
file_buffer.clear();
file_buffer.extend_from_slice(self.signature);
sig_match_index = 0;
}
} else {
sig_match_index = 0;
}
}Two things keep a lot of junk out of the results. The JPEG signature is only two bytes, so FF D8 shows up by chance all the time. That's why, for image formats, rscovery tries to decode with the image crate and only shows what opens. It also keeps the SHA-256 of every hit, so the same photo doesn't show up twice.
Block map and loose text#
The tool has two more screens. View Blocks paints a map of the drive: every 32 MB chunk with at least one non-zero byte shows up in color. It's the fastest way to see that an "empty" drive isn't empty.
let valid_bytes = buffer[..bytes_read].iter().filter(|&&b| b != 0x00).count();
if valid_bytes > 0 {
non_empty.push(iteration);
}And the Text screen looks for readable text runs of at least 32 characters that contain any word from your list. It's the scariest part: a .txt full of passwords you deleted years ago shows up whole.
All of this runs on the Rust side. React just listens to the events Tauri emits (scan-progress, file-found, text-found) and builds the screen, with images arriving as base64.
I tested it on a fake USB stick#
I wasn't going to run this on my computer's drive. I created a 40 MB disk image formatted as FAT32, like a USB stick, copied four test files to it (a photo, a document as PNG, a PDF and a .txt with fake passwords), deleted all four and unmounted it.
Then I ran the same loops from rscovery over the image file, without the UI. The result:
blocks of 1024 KB: 40 total, 1 non-empty
jpeg found at byte 663040 (17367 bytes) -> recovered/jpeg_0.jpeg
png found at byte 684544 (12679 bytes) -> recovered/png_0.png
pdf found at byte 706560 (192 bytes) -> recovered/pdf_0.pdf
TEXT: lista de compras e senhas antigas (arquivo de teste): wifi de casa = exemplo123, ...All four came back. The photo and the PNG were byte-for-byte identical to the originals. The PDF came back one byte short, the line break after %%EOF, because the scanner stops right at the end signature.
Then I tested the other side: on a copy of the same image, I filled the free space with a file full of zeros, deleted that file and scanned again. Zero files. Once it's overwritten, it's gone.
What rscovery doesn't do#
Because it's simple, it has limits worth knowing:
- Fragmented files don't come back whole. It collects consecutive bytes from start to end. If the file was scattered across the drive, it comes out corrupted or not at all.
- Accented text gets split. The text search only accepts ASCII characters, so a Portuguese word like "não" cuts the sentence at the "ã".
- It stops at 300 hits per search.
- ZIP comes out truncated. The ZIP end signature is the start of the last record, which still has at least 18 bytes after it. Since the scanner stops at the signature, those bytes are left out.
- It unmounts the drive before reading. On macOS, it runs
diskutil unmounton the selected device. Only use it on your own drive, ideally on a copy.
HDDs and SSDs don't forget the same way#
Everything I've shown so far applies fully to HDDs and to USB sticks without any special care. With an SSD, TRIM comes in.
On an HDD#
The data stays on the platter until something writes over it. The good news is that writing once is enough: the 2014 edition of NIST SP 800-88, the US reference guide for media sanitization, says a single pass with a fixed pattern such as zeros typically prevents recovery even with lab techniques. Those dozens of passes are a thing of the past.
On Windows, a full format (with "Quick" unchecked) has written zeros to the whole volume since Vista, as Microsoft documents.
On an SSD#
On an SSD, when you delete a file, the system sends the TRIM command saying which blocks are no longer valid (Samsung explains it well). The controller cleans those blocks in the background, on its own schedule. On many current SSDs, reading a block that went through TRIM returns zeros, but that depends on the model.
So is an SSD safer? For everyday files, it usually is. For erasing on purpose, it's trickier:
- Overwriting doesn't reach everything. The controller spreads writes around (wear leveling) and keeps spare blocks the system can't even see. NIST SP 800-88 Rev. 2, from 2025, recommends avoiding multi-pass overwrites on these drives, because they offer very little protection.
- macOS itself warns you.
man diskutilsays thesecureEraseoptions are no longer considered safe because of wear leveling, and recommends encryption. Disk Utility doesn't even show those options for SSDs. - USB sticks and enclosures are a gamble. TRIM depends on the adapter passing the command along. I couldn't find an official source on which ones do, so I treat every USB stick like an HDD: the data stays until it's overwritten.
How to actually protect yourself#
NIST splits sanitization into three levels: clear (against someone using a regular computer to recover data), purge (against a lab, with the drive still usable) and destroy. And it lists cryptographic erase as a way to purge: if the whole drive is encrypted, destroying the key makes the data unreadable.
The golden rule: encrypt from day one#
It's the only protection that works even in the worst case: the drive that died, the stolen laptop, the SSD you can't properly erase.
- Mac: on a Mac with Apple silicon or a T2 chip, storage is already encrypted even with FileVault off. Turning FileVault on ties the key to your password.
- Windows: Device Encryption is automatic BitLocker, even on Home, but it only turns on by itself if you sign in with a Microsoft or work account. With a local account, check it.
- USB sticks and external drives: if they hold anything sensitive, use an encrypted volume (BitLocker To Go, or an encrypted volume from Disk Utility on a Mac).
Before selling#
- Mac: use Erase All Content and Settings (macOS Monterey or later, with Apple silicon or a T2 chip). Apple explains that erasing destroys the keys, and without them the data is inaccessible.
- Windows: reset the PC with Remove everything and the Clean data option turned on, which is what Microsoft recommends if you're selling or recycling it. Microsoft itself warns this doesn't meet government standards, so together with encryption it's much better.
- USB sticks and HDDs: a full format, or one pass of zeros. On Windows,
formatwith/Pwrites zeros to every sector. - SSD: prefer the manufacturer's tool, which triggers the controller's own Secure Erase and wipes every cell. If the SSD was always encrypted, erasing the key already does it.
- Phone: sign out of your accounts, check that encryption is on and do a factory reset.
- Won't turn on anymore: don't sell the drive with it. Take it out of the device or really destroy it.
What I learned#
Deleting and destroying are different things. The operating system is optimized to be fast, not to forget. "Delete" is just a promise that the space can be reused.
Even a simple tool is scary. rscovery has a little over 700 lines of Rust, and it recovered everything I deleted from the test USB stick. Professional tools do far more.
Rust is a good fit for this kind of tool. Reading gigabytes byte by byte needs to be fast, and Tauri kept the React UI light: Rust does the heavy lifting and just sends events to the screen.
On an SSD, the answer is encryption, not brute force. I came in thinking "overwrite it several times". I learned that, on flash memory, that gives a false sense of security. Erasing the key is faster and more reliable.
Precautions#
- Only run this kind of tool on your own drives. Reading someone else's drive without permission is an invasion of privacy, and may be a crime.
- Work on a copy. To recover something of yours, make an image of the drive and scan the image. Every write to the original drive can overwrite exactly what you want to save.
- Don't trust "format". A quick format doesn't erase the data, and a full one on an SSD doesn't reach everything.
- Bought a used drive? Format it before using it, and remember the seller may not have read this post.
References#
- YuriRDev: Você está VENDENDO seus DADOS sem saber! (a real case, explained from start to finish, in Portuguese)
- rscovery on GitHub
- NIST SP 800-88 Rev. 2 (2025): Guidelines for Media Sanitization and Rev. 1 (2014)
- PhotoRec (CGSecurity)
- Microsoft: format writes zeros since Vista and the format command
- Microsoft: reset your PC and Device Encryption
- Apple: Erase All Content and Settings, FileVault and volume encryption and Disk Utility
- Samsung: TRIM and Secure Erase
- PNG specification (W3C) and ZIP format APPNOTE
- Elcomsoft: TRIM, DRAT and DZAT in SSD forensics
Read next

Infra15 min read
How to save tokens in Claude Code with rtk and graphify

Back-end18 min read
What is RAG? Indexing, hybrid search and a real case

Front-end16 min read
Multilingual SEO in React: hreflang, prerendering, sitemap