After a retention thread that had been failing for twenty days was repaired, its first run deleted 436,672 expired rows. Then the residue was measured: 85,702 rows reaching back 60.8 hours. The rule said 48. Nothing was locked any more, and the thread was still leaving a day's worth of rows it should have removed.

Two formats of the same instant

Rows were stamped by Python's datetime, which renders as the date, the letter T, then the time. The deletion threshold was computed by SQLite's datetime function, which renders the same instant as the date, a space, then the time. Both are ISO-ish. Both are readable. Compared as strings, which is what SQLite does with text columns, they diverge at the eleventh character: T is 0x54 and space is 0x20, so every row stamped on the boundary date sorts after the threshold and survives the delete forever.

Figure 1 · Compared as text

An illustrative pair on the boundary date. The first ten characters match. At the eleventh, the row has the letter T and the threshold has a space; T sorts higher, so a row stamped twelve hours before the threshold compares as if it came after it, and the delete skips it.

The check was direct. At the same moment, with the same 48-hour offset, a threshold built with datetime selected 0 rows for deletion and a threshold built with strftime using the T separator selected 18,044.

A second offset was hiding under the first. SQLite's now is UTC. The stored stamps were local time, nine hours ahead. Without the localtime modifier the effective retention would have been 57 hours rather than 48, an error that happens to fall in the same direction and would have been easy to misattribute to the separator alone.

Fixing the threshold, not the data

The data stayed as written. The threshold was rebuilt with strftime, an explicit format string carrying the T, and the localtime modifier. The next run deleted 18,047 more rows and the measured retention came to 48.0 hours. The file, after vacuum, went from 288MB to 36MB.

Figure 2 · Before and after the threshold fix

Threshold from datetime()

Rows the delete selected
0
Measured retention (rule: 48 h)
60.8 hours
Database file
288 MB

Threshold from strftime() with T

Rows the delete selected
18,044
Measured retention (rule: 48 h)
48.0 hours
Database file, after vacuum
36 MB
Rows selected: the same moment and the same 48-hour offset, with only the format of the threshold changed. Retention and file size: before the fix, and after the next run. All numbers are the ones measured in this record; bars are drawn to scale within each row.

Both findings went into a list of traps for the next person: the lock variant that no timeout retries, the separator that breaks string comparison, the UTC default, and the fact that a thread having been started says nothing about whether it has ever succeeded. The retention code had been correct in intent for twenty days and wrong in two characters.