Deleted the wrong rows in SQLite? What to do in the first 10 minutes
We have all been there. The DELETE query was missing the WHERE clause, or a housekeeping job ran on the wrong database, or the test run was carried out in production instead of the development database. Our heart cries out in anguish, and we spend the next ten minutes or so trying to find out if the deleted rows can be recovered.
The first and most important step is to refrain from writing any new data to the database. Once SQLite has deleted some data, it will overwrite the relevant space with new data to help it run faster.
Here's what I would try to do right away, all of which are free methods:
Terminate the program or script that was using the database.
Copy the .db file and any associated -wal or -journal files to a different location, and work with the copies.
Try to retrieve the data from a backup, a file history copy, or a synchronized cloud folder.
If the file is damaged beyond repair, try using the .recover command in the sqlite3 shell (this requires a recent version of SQLite). This will bring you a list of all orphaned rows. You can then try to import them into the recovered database from the lostandfound table.
Check if the application used to access the SQLite database can provide an export or log of the database data.
The following mistakes would prevent successful recovery of the deleted rows in SQLite:
Calling the VACUUM command, which compacts the database, overwriting the deleted data.
Letting the application continue to write data to the database, thus overwriting the deleted data.
Testing a recovery approach on the original database file.
Having the securedelete or autovacuum features enabled, which may cause the deleted data to be lost permanently.
Manually deleting the -wal or -journal files, which can also result in permanent data loss.
If the free methods do not work, the Kernel SQLite Database Recovery tool can recover deleted records, tables, views, triggers and indexes from DB and DB3 files, and display table data, deleted data and table properties in a preview window before saving them. The tool's free trial version extracts 25 records from each table, so you can test it before you buy.
Has anyone here recovered rows from SQLite after deleting them?
Source: https://www.nucleustechnologies.com/sqlite-database-recovery/