{"id":360,"date":"2026-11-07T17:45:00","date_gmt":"2026-11-07T17:45:00","guid":{"rendered":"https:\/\/backendside.com\/blog\/?p=360"},"modified":"2026-11-21T11:57:02","modified_gmt":"2026-11-21T11:57:02","slug":"repair-corrupted-sqlite-database","status":"publish","type":"post","link":"https:\/\/backendside.com\/blog\/2026\/11\/07\/repair-corrupted-sqlite-database\/","title":{"rendered":"Recovering and Repairing a Corrupted SQLite Database"},"content":{"rendered":"<p class=\"lead\">SQLite is famously robust: its transactions survive crashes and power cuts, and corruption is rare. Rare is not never, though, and when it happens the message is blunt &mdash; <code>database disk image is malformed<\/code>. The good news is that a corrupted SQLite file is very often partly or almost entirely recoverable, as long as you do not make it worse in the first five minutes. This guide walks through a calm, ordered approach: protect the evidence, diagnose, repair from the least to the most invasive method, verify, and prevent a repeat.<\/p>\n<h2>Step 0: stop and make a copy<\/h2>\n<p>Before you run a single repair command:<\/p>\n<ol>\n<li><strong>Close every program using the database<\/strong>, so nothing writes to it while you work.<\/li>\n<li><strong>Copy the database together with its companion files<\/strong> &mdash; <code>-wal<\/code> and <code>-shm<\/code> (WAL mode) or <code>-journal<\/code> (rollback mode) if they exist. A <code>-wal<\/code> file can hold committed transactions not yet written to the main file, and a hot <code>-journal<\/code> holds what SQLite needs to roll back an interrupted write. Deleting either can turn a healthy database into a corrupt one, or a minor problem into a major one.<\/li>\n<li><strong>Work only on the copy.<\/strong> Keep the original untouched so you can start again if a step goes wrong.<\/li>\n<\/ol>\n<h2>Recognising the problem<\/h2>\n<p>Not every scary error is corruption. Match the message before you reach for repair tools:<\/p>\n<table>\n<thead>\n<tr>\n<th>Error<\/th>\n<th>What it usually means<\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td><code>database disk image is malformed<\/code><\/td>\n<td>Real structural corruption (SQLITE_CORRUPT)<\/td>\n<\/tr>\n<tr>\n<td><code>file is not a database<\/code><\/td>\n<td>The header is unreadable: badly damaged, not SQLite at all, <strong>or an encrypted database opened without the right key<\/strong><\/td>\n<\/tr>\n<tr>\n<td><code>database is locked<\/code><\/td>\n<td>Another connection holds a lock &mdash; not corruption<\/td>\n<\/tr>\n<tr>\n<td><code>database or disk is full<\/code><\/td>\n<td>Out of disk space or a size limit; free space and retry<\/td>\n<\/tr>\n<tr>\n<td><code>row N missing from index<\/code> (from integrity_check)<\/td>\n<td>Index-only damage &mdash; usually the easiest fix<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>That second row matters: if the file came from an app that might use SQLCipher, try opening it with SQLCipher and the key before assuming the worst.<\/p>\n<h2>Step 1: diagnose with integrity_check<\/h2>\n<p><code>PRAGMA integrity_check<\/code> walks the entire file &mdash; every page, every b-tree, every index entry &mdash; and reports what is inconsistent. It returns a single row saying <code>ok<\/code> if the database is healthy.<\/p>\n<pre style=\"background:#1e1d1b;color:#eceae6;padding:1rem 1.15rem;border-radius:8px;overflow-x:auto;font-size:.85rem;line-height:1.5;\"><code>PRAGMA integrity_check;        -- full check; up to 100 problems by default\nPRAGMA integrity_check(1000);  -- report up to 1000 problems\nPRAGMA quick_check;            -- faster, skips index-vs-table cross checks\nPRAGMA foreign_key_check;      -- logical: rows pointing at missing parents<\/code><\/pre>\n<p>Read the output carefully. If every message mentions an <strong>index<\/strong> (&ldquo;row 1234 missing from index idx_orders_date&rdquo;, &ldquo;wrong # of entries in index&rdquo;), the table data is probably fine and only the indexes are damaged. Messages about pages, freelists or b-tree structure (&ldquo;Page 52 is never used&rdquo;, &ldquo;invalid page number&rdquo;, &ldquo;2nd reference to page&rdquo;) point to deeper damage. Note that <code>foreign_key_check<\/code> finds data problems, not file corruption &mdash; useful afterwards, not as a diagnosis.<\/p>\n<div style=\"border:1px solid #c5d3f8;background:linear-gradient(135deg,#eef2fd 0%,#ffffff 72%);border-radius:14px;padding:1.5rem 1.65rem;margin:2rem 0;\">\n<div style=\"font-size:.7rem;font-weight:700;letter-spacing:.08em;text-transform:uppercase;color:#2d5be3;margin-bottom:.55rem;\">&#128295; BackendSide Tool<\/div>\n<h4 style=\"margin:0 0 .45rem;font-size:1.15rem;color:#1a1916;font-weight:700;\">SQLiteDesk &mdash; Integrity Check and Maintenance from a Menu<\/h4>\n<p style=\"margin:0 0 1.05rem;color:#3d3c38;font-size:.92rem;line-height:1.65;\">No shell required for the first diagnosis. <strong>SQLiteDesk<\/strong> runs an <strong>integrity check<\/strong>, shows database stats, and runs VACUUM, ANALYZE and REINDEX straight from its Maintenance menu. When the check points at a damaged index, REINDEX is one click away.<\/p>\n<p>  <a href=\"https:\/\/backendside.com\/sqlitedesk.php\" style=\"display:inline-flex;align-items:center;gap:.4rem;background:#2d5be3;color:#ffffff;font-weight:600;font-size:.85rem;padding:.6rem 1.2rem;border-radius:6px;text-decoration:none;\">Explore SQLiteDesk &rarr;<\/a>\n<\/div>\n<h2>Step 2: the repair ladder<\/h2>\n<p>Work up from the gentlest method, re-running <code>integrity_check<\/code> after each one. Stop as soon as you get a clean result.<\/p>\n<h3>Rung 1: REINDEX (index-only damage)<\/h3>\n<p>If only indexes are affected, rebuilding them from the table data fixes it completely:<\/p>\n<pre style=\"background:#1e1d1b;color:#eceae6;padding:1rem 1.15rem;border-radius:8px;overflow-x:auto;font-size:.85rem;line-height:1.5;\"><code>REINDEX;                       -- rebuild every index\nREINDEX idx_orders_date;       -- or just the damaged one\nPRAGMA integrity_check;<\/code><\/pre>\n<h3>Rung 2: rebuild into a new file<\/h3>\n<p>If the tables can still be read, copying everything into a fresh file leaves the damaged structure behind. <code>VACUUM INTO<\/code> does it in one statement (SQLite 3.27+):<\/p>\n<pre style=\"background:#1e1d1b;color:#eceae6;padding:1rem 1.15rem;border-radius:8px;overflow-x:auto;font-size:.85rem;line-height:1.5;\"><code>VACUUM INTO 'C:\/recovery\/rebuilt.db';<\/code><\/pre>\n<p>A plain-text dump is the more forgiving alternative, because you can inspect and edit it. With the <code>sqlite3<\/code> command-line shell:<\/p>\n<pre style=\"background:#1e1d1b;color:#eceae6;padding:1rem 1.15rem;border-radius:8px;overflow-x:auto;font-size:.85rem;line-height:1.5;\"><code>sqlite3 damaged.db \".output dump.sql\" \".dump\"\nsqlite3 rebuilt.db \".read dump.sql\"<\/code><\/pre>\n<p>If <code>.dump<\/code> hits errors along the way, it ends the script with <code>ROLLBACK; -- due to errors<\/code> instead of <code>COMMIT;<\/code>. Change that last line to <code>COMMIT;<\/code> before loading, or the reload will discard everything it just inserted.<\/p>\n<div style=\"border:1px solid #c5d3f8;background:linear-gradient(135deg,#eef2fd 0%,#ffffff 72%);border-radius:14px;padding:1.5rem 1.65rem;margin:2rem 0;\">\n<div style=\"font-size:.7rem;font-weight:700;letter-spacing:.08em;text-transform:uppercase;color:#2d5be3;margin-bottom:.55rem;\">&#128295; BackendSide Tool<\/div>\n<h4 style=\"margin:0 0 .45rem;font-size:1.15rem;color:#1a1916;font-weight:700;\">SQLiteDesk &mdash; Dump to .sql and Rebuild<\/h4>\n<p style=\"margin:0 0 1.05rem;color:#3d3c38;font-size:.92rem;line-height:1.65;\">If the database is still readable, a dump-and-reload is often all it takes. <strong>SQLiteDesk<\/strong> can <strong>dump an entire database to .sql<\/strong>, create a fresh database, and run that <code>.sql<\/code> script into it. Dumps replay cleanly with generated columns, full-text search and AUTOINCREMENT counters. You can also export individual tables to CSV, JSON or SQL INSERT to rescue the data that matters most first.<\/p>\n<p>  <a href=\"https:\/\/backendside.com\/sqlitedesk.php\" style=\"display:inline-flex;align-items:center;gap:.4rem;background:#2d5be3;color:#ffffff;font-weight:600;font-size:.85rem;padding:.6rem 1.2rem;border-radius:6px;text-decoration:none;\">Explore SQLiteDesk &rarr;<\/a>\n<\/div>\n<h3>Rung 3: .recover (deep damage)<\/h3>\n<p>When <code>VACUUM INTO<\/code> and <code>.dump<\/code> fail partway, the <code>sqlite3<\/code> shell&rsquo;s <code>.recover<\/code> command (version 3.29 and later) is the strongest tool available. Instead of reading the database through its normal, now-broken structure, it scans the pages directly and salvages every row it can find:<\/p>\n<pre style=\"background:#1e1d1b;color:#eceae6;padding:1rem 1.15rem;border-radius:8px;overflow-x:auto;font-size:.85rem;line-height:1.5;\"><code>sqlite3 damaged.db \".output recovered.sql\" \".recover\"\nsqlite3 recovered.db \".read recovered.sql\"\n\n-- useful options:\n--   .recover --ignore-freelist        skip pages on the freelist (fewer old deleted rows)\n--   .recover --lost-and-found orphans name the table for unplaceable rows<\/code><\/pre>\n<p>Rows that <code>.recover<\/code> finds but cannot tie back to a known table are placed in a <code>lost_and_found<\/code> table, with columns describing where they came from and their values in <code>c0<\/code>, <code>c1<\/code> and so on. Look there before declaring data lost. Be aware that recovery can also resurrect rows that were deleted but whose pages were not yet overwritten &mdash; check the results rather than trusting them blindly.<\/p>\n<p>For a SQLCipher database, run the recovery with SQLCipher&rsquo;s own shell and supply the key first; a stock <code>sqlite3<\/code> cannot read the encrypted pages.<\/p>\n<h3>Rung 4: restore from backup<\/h3>\n<p>If recovery leaves too many gaps, the remaining option is the last good backup &mdash; possibly combined with whatever newer rows <code>.recover<\/code> salvaged.<\/p>\n<h2>Step 3: verify the result<\/h2>\n<p>A repaired file is not finished until you have checked it:<\/p>\n<pre style=\"background:#1e1d1b;color:#eceae6;padding:1rem 1.15rem;border-radius:8px;overflow-x:auto;font-size:.85rem;line-height:1.5;\"><code>PRAGMA integrity_check;        -- must return 'ok'\nPRAGMA foreign_key_check;      -- orphaned child rows after partial recovery\nSELECT count(*) FROM orders;   -- compare key tables against what you expect\nSELECT count(*) FROM lost_and_found;  -- if .recover created it<\/code><\/pre>\n<p>Also check settings that a rebuild may not carry over, such as <code>PRAGMA user_version<\/code> (many apps use it to track schema migrations) and <code>PRAGMA journal_mode<\/code>, and set them on the new file if needed. Only then swap the repaired file in place of the original.<\/p>\n<h2>Why it happened, and how to stop it happening again<\/h2>\n<p>SQLite&rsquo;s own documentation lists the realistic causes, and nearly all of them are environmental:<\/p>\n<table>\n<thead>\n<tr>\n<th>Cause<\/th>\n<th>Prevention<\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td>Copying the file while a transaction was in progress<\/td>\n<td>Use the online backup API, <code>.backup<\/code> or <code>VACUUM INTO<\/code><\/td>\n<\/tr>\n<tr>\n<td>Deleting or separating the <code>-wal<\/code> \/ <code>-journal<\/code> file<\/td>\n<td>Always move and back up the companion files together<\/td>\n<\/tr>\n<tr>\n<td>Database on a network share used by several machines<\/td>\n<td>Keep SQLite files on local disks; use a server database for shared access<\/td>\n<\/tr>\n<tr>\n<td>Power loss with <code>synchronous = OFF<\/code> or a drive that ignores sync requests<\/td>\n<td>Use <code>synchronous = FULL<\/code> (or <code>NORMAL<\/code> in WAL mode) and reliable storage<\/td>\n<\/tr>\n<tr>\n<td>USB drive removed during a write<\/td>\n<td>Close the app before unplugging<\/td>\n<\/tr>\n<tr>\n<td>Failing disk or memory<\/td>\n<td>Run hardware checks if corruption recurs<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>Add a scheduled <code>PRAGMA quick_check<\/code> to your app or maintenance routine, keep regular online backups on a different drive, and test a restore now and then. Corruption you catch early, with a recent backup at hand, is a minor inconvenience instead of a disaster.<\/p>\n<div style=\"border:1px solid #c5d3f8;background:linear-gradient(135deg,#eef2fd 0%,#ffffff 72%);border-radius:14px;padding:1.5rem 1.65rem;margin:2rem 0;\">\n<div style=\"font-size:.7rem;font-weight:700;letter-spacing:.08em;text-transform:uppercase;color:#2d5be3;margin-bottom:.55rem;\">&#128295; BackendSide Tool<\/div>\n<h4 style=\"margin:0 0 .45rem;font-size:1.15rem;color:#1a1916;font-weight:700;\">SQLiteDesk &mdash; The Backup You Will Be Glad You Made<\/h4>\n<p style=\"margin:0 0 1.05rem;color:#3d3c38;font-size:.92rem;line-height:1.65;\">The best repair is a restore. <strong>SQLiteDesk<\/strong> makes an <strong>online backup that is safe on a live database while writes are happening<\/strong>, and restores from a backup file when you need it &mdash; for encrypted databases too, with the option to re-encrypt. Backups keep the database&rsquo;s <code>user_version<\/code>, <code>application_id<\/code> and journal mode. Available on the Microsoft Store with a free trial.<\/p>\n<p>  <a href=\"https:\/\/backendside.com\/sqlitedesk.php\" style=\"display:inline-flex;align-items:center;gap:.4rem;background:#2d5be3;color:#ffffff;font-weight:600;font-size:.85rem;padding:.6rem 1.2rem;border-radius:6px;text-decoration:none;\">Explore SQLiteDesk &rarr;<\/a>\n<\/div>\n<h2>Key takeaways<\/h2>\n<ul>\n<li>First <strong>close everything and copy the database with its <code>-wal<\/code>, <code>-shm<\/code> or <code>-journal<\/code> files<\/strong>; never delete them.<\/li>\n<li>&ldquo;File is not a database&rdquo; may just mean an <strong>encrypted database without its key<\/strong>.<\/li>\n<li>Diagnose with <code>PRAGMA integrity_check<\/code>; index-only errors are fixed by <code>REINDEX<\/code>.<\/li>\n<li>For deeper damage, rebuild with <code>VACUUM INTO<\/code> or <code>.dump<\/code> (fix a trailing <code>ROLLBACK<\/code>), then escalate to <code>.recover<\/code> and check <code>lost_and_found<\/code>.<\/li>\n<li>Verify with <code>integrity_check<\/code>, <code>foreign_key_check<\/code> and row counts, and restore <code>user_version<\/code> if needed.<\/li>\n<li>Prevent repeats with <strong>online backups, local disks, sane <code>synchronous<\/code> settings<\/strong> and regular checks; <strong>SQLiteDesk<\/strong> puts the backups, integrity checks and REINDEX in one Windows app.<\/li>\n<\/ul>\n","protected":false},"excerpt":{"rendered":"<p>&#8220;Database disk image is malformed&#8221; is not always the end. Here is a calm, step-by-step approach: protect what you have, diagnose with integrity_check, then work up the repair ladder from REINDEX to a dump-and-reload to the sqlite3 .recover command, and finally make sure it does not happen again.<\/p>\n","protected":false},"author":1,"featured_media":429,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[6,8],"tags":[38,32,33],"class_list":["post-360","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-sqlite","category-tutorials","tag-data-recovery","tag-sqlite","tag-sqlitedesk"],"_links":{"self":[{"href":"https:\/\/backendside.com\/blog\/wp-json\/wp\/v2\/posts\/360","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/backendside.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/backendside.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/backendside.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/backendside.com\/blog\/wp-json\/wp\/v2\/comments?post=360"}],"version-history":[{"count":1,"href":"https:\/\/backendside.com\/blog\/wp-json\/wp\/v2\/posts\/360\/revisions"}],"predecessor-version":[{"id":430,"href":"https:\/\/backendside.com\/blog\/wp-json\/wp\/v2\/posts\/360\/revisions\/430"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/backendside.com\/blog\/wp-json\/wp\/v2\/media\/429"}],"wp:attachment":[{"href":"https:\/\/backendside.com\/blog\/wp-json\/wp\/v2\/media?parent=360"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/backendside.com\/blog\/wp-json\/wp\/v2\/categories?post=360"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/backendside.com\/blog\/wp-json\/wp\/v2\/tags?post=360"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}