Why a Seized Phone's Deleted Texts Often Survive in SQLite
Delete a text message on most phones and the words are very likely still on the flash chip. The messages app keeps them in a SQLite database, and by default SQLite does not wipe the bytes of a deleted row. It marks the space as reusable and moves on.
That is why examiners can often pull "deleted" messages out of a seized handset without any exotic attack: the data was never removed. Here is how to watch it happen on your own machine.
See it in thirty seconds
You need the sqlite3 command line tool and strings. Create a tiny messages table, delete one row, then look at the raw file rather than asking SQLite.
sqlite3 sms.db "CREATE TABLE msg(id INTEGER PRIMARY KEY, body TEXT);
INSERT INTO msg(body) VALUES
('meet at the pier at nine'),
('burn this message after reading'),
('see you tomorrow');"
sqlite3 sms.db "DELETE FROM msg WHERE id = 2;"
sqlite3 sms.db "SELECT id, body FROM msg;"
strings sms.db
The SELECT shows two rows. The strings output still shows all three messages, including the one you deleted. The row is gone from the table, not from the file.
What SQLite actually does on DELETE
A SQLite file is split into fixed-size pages, commonly 4096 bytes. A table lives in B-tree pages, and each page holds a small header, an array of cell pointers, and the cells themselves (your rows), packed from the end of the page backwards.
When you delete a row, SQLite does three things:
- removes that row's pointer from the pointer array;
- turns the cell's space into a "freeblock", chained into a list of free space inside the page;
- writes a four-byte freeblock header (next offset and size) over the start of the old cell.
Everything after those four bytes is untouched. The message text sits well past the start of the cell, so it survives intact. The format is documented in the SQLite file format specification, and the behaviour is deliberate: overwriting is extra I/O that most applications do not want.
Whole pages are even worse
Delete enough rows that a page becomes empty and SQLite does not zero it either. The page is added to the database's freelist, a chain of trunk pages that record which other pages are free for reuse. The header of the file stores where the freelist starts and how many pages are on it.
A page on the freelist keeps its old contents until something reuses it. So a conversation thread deleted in one go can leave whole pages of messages sitting in the file, readable as plain text, with their row structure largely intact.
Quick detour, because this is the clever bit: the freelist trunk page only stores page numbers, so it is the leaf pages it points at that hold the old data. Nothing needs to be "recovered" in the sense of rebuilding it. An examiner just walks the freelist and reads those pages as if they were live B-tree pages.
Where else the old data hides
Free pages are only one place. A phone database usually leaks in several layers at once:
- The WAL file. In write-ahead log mode, changes go to a
-walfile first, and older versions of pages can sit there until a checkpoint overwrites them. See the SQLite WAL documentation. - Rollback journals. In the older journal mode, the original pages are copied out before a change and may linger after the file is deleted.
- Partially overwritten cells. New rows can reuse part of a freeblock, leaving the tail of an older message next to the new one.
- Backups and sync copies. Cloud and local backups are snapshots, with their own copy of the database.
- The flash itself. Even a clean rewrite at the SQLite level may leave old blocks on the storage chip until it is erased by the controller.
The last point is why "I overwrote it" is a weaker claim on a phone than on a spinning disk. The flash controller remaps blocks for wear levelling, so what the filesystem sees and what the chip holds can differ.
What an examiner does with it
Forensic tools such as commercial extraction suites and open-source carvers parse the page structure directly. They read the live rows, then walk freeblocks and freelist pages, and try to decode what looks like cell data. The SQLite shell's own .recover command does a rough version of this for corrupted files.
Recovered fragments are not always clean. The rowid and the start of the record header are often the bytes that were overwritten, so the examiner sees the message text but may have to infer the timestamp or sender from neighbouring structure. That uncertainty matters if the material is later relied on as evidence, so such a result should be described as a "recovered fragment" rather than a complete record.
How to make a delete actually delete
SQLite has a pragma for this. With secure_delete on, deleted content is overwritten with zeros. The pragma documentation also describes a "fast" setting that zeroes content only where it would not cost extra I/O. Some builds of SQLite ship with it enabled by default, so it is worth checking rather than assuming.
sqlite3 sms2.db "PRAGMA secure_delete = ON;
CREATE TABLE msg(id INTEGER PRIMARY KEY, body TEXT);
INSERT INTO msg(body) VALUES ('burn this message after reading');
DELETE FROM msg;"
strings sms2.db | grep burn || echo "not found"
The pragma is per connection, so an application has to set it every time it opens the database. If you only run it from the shell on a copy, you protect nothing on the phone.
For data that is already deleted the ordinary way, VACUUM rebuilds the whole file from the live rows only, which drops freelist pages and freeblock contents. Two caveats: it writes a new file, so the old blocks may still exist on flash, and a WAL or backup can still hold earlier versions.
What this means if you build or use the app
If you write an app that stores sensitive messages in SQLite, decide up front what "delete" must mean. Turn on secure_delete, consider VACUUM after bulk deletions, and check the WAL is checkpointed. Better still, encrypt the database at rest so a copy of the file reveals nothing without the key, which is what SQLCipher-style approaches are for.
If you are a user, deleting a message in the app removes it from view, not from the file. Full-device encryption and a strong lock screen protect more than any delete button does, because they decide whether anyone can read those free pages in the first place.