How to fix SQLITE_CORRUPT: The database disk image is malformed in SQLite
Database corruption prevents all queries and operations. Use PRAGMA integrity_check to diagnose the issue, then recover data using the .recover command or dump-and-rebuild method to restore functionality.
Quick fix
Fixes SQLITE_CORRUPT: The database disk image is malformed
sqlite3 mydb.sqlite3 "PRAGMA integrity_check;"- Check if the database is actually corrupt
- Back up the corrupted file immediately
- Attempt recovery using the .recover command (fastest method)
- If .recover fails, rebuild using dump and SQL import
sqlite3 mydb.sqlite3 "PRAGMA integrity_check;"SQLITE_CORRUPT: The database disk image is malformed
What this error means
This error (SQLite error code 11) indicates that the physical structure of the SQLite database file has been corrupted. SQLite detected inconsistencies in the database file format, internal page structures, or table/index organization during file verification. This can occur due to hardware failures, improper shutdowns, concurrent write conflicts, or application bugs writing to closed file descriptors.
Typical symptoms
- All queries fail with "database disk image is malformed" error
- Unable to read any table data from the database file
- Applications crash or hang when trying to access the database
- PRAGMA integrity_check returns corruption errors instead of "ok"
- The database file exists but appears inaccessible or incomplete
- Error occurs after unexpected system shutdown or power loss
Common causes
- Hardware failure or SSD/disk corruption affecting the database file
- Sudden system crash or forced application termination during an active write operation
- Concurrent processes accessing the database without proper locking coordination
- Writing to a closed file descriptor, overwriting database file content
- Network filesystem (NFS) locking bugs when running on remote storage
- Improper backup procedure where database was copied while in use
- Journal file (*.journal) separated from or deleted while recovery was needed
- Application bug corrupting SQLite internal data structures in memory
How to fix "SQLITE_CORRUPT: The database disk image is malformed"
First, verify the corruption is real by running the integrity check command:
sqlite3 mydb.sqlite3 "PRAGMA integrity_check;"A healthy database returns "ok". A corrupt database lists specific errors like:
- corruption detected in the index "sqlite_autoindex_table_name"
- missing pages referenced by index
- malformed database record
If the database opens and returns "ok", the problem may be transient or application-specific.
Never attempt recovery without a backup. The original file may help diagnose the issue:
cp mydb.sqlite3 mydb.sqlite3.backupKeep this backup safe in case recovery goes wrong or you need to investigate further.
SQLite 3.38.0+ includes a dedicated .recover tool for damaged databases:
sqlite3 mydb.sqlite3 ".recover --ignore-freelist" | sqlite3 mydb_recovered.sqlite3Or for older SQLite versions, use .dump:
echo ".dump" | sqlite3 mydb.sqlite3 > dump.sqlThis exports all recoverable data to SQL statements. The .recover method is preferred as it skips unusable free space and doesn't try to restore invalid transaction states.
After the command completes, check the new database:
sqlite3 mydb_recovered.sqlite3 "PRAGMA integrity_check;"If "ok" is returned, your data is recovered. Replace the original:
mv mydb.sqlite3 mydb.sqlite3.corrupted
mv mydb_recovered.sqlite3 mydb.sqlite3If the .recover method produces an incomplete or unusable database:
# Export what can be salvaged
sqlite3 mydb.sqlite3 ".mode insert" ".dump" > dump.sql
# Manually edit dump.sql to remove transaction statements if needed:
# - Remove lines starting with "BEGIN TRANSACTION;" and "COMMIT;"
# - Some dumps include invalid data; comment out problematic INSERT statements
# Create fresh database from cleaned dump
sqlite3 mydb_rebuilt.sqlite3 < dump.sql
# Verify integrity
sqlite3 mydb_rebuilt.sqlite3 "PRAGMA integrity_check;"This method may lose some data but recovers what is structurally valid. Review the SQL dump before importing to catch issues.
Corruption does not happen randomly. Determine the cause to prevent recurrence:
Hardware Issues:
# Check disk health (Linux)
sudo smartctl -a /dev/sda | grep -i "health|fail|error"
# On macOS
diskutil verifyVolume /Volumes/YourDriveIf the disk is failing, replace it and restore from backup.
Concurrent Access Issues:
# Find processes accessing the database
lsof | grep mydb.sqlite3
# Verify only one application has the file open at a timeApplication Bugs:
- Review recent code changes that modify the database
- Check application logs for crashes during write operations
- Ensure proper error handling and transaction rollback
Backup/Restore Issues:
- Never backup a database file while it's open in the application
- Stop the application, backup, then restart
- Include the journal file in backups (if using WAL mode)
If you have a recent uncorrupted backup:
cp /path/to/backup/mydb.sqlite3 mydb.sqlite3
sqlite3 mydb.sqlite3 "PRAGMA integrity_check;"This is the most reliable recovery method. Regular backups are essential for SQLite.
Advanced notes
Note that even after successful recovery, constraints (FOREIGN KEY, UNIQUE, CHECK) may be violated in the recovered database. Verify data integrity in your application and run PRAGMA foreign_key_check to find violations. If using write-ahead logging (WAL mode), corruption is less likely; standard rollback journal mode is more vulnerable to corruption during crashes. Some corruption cannot be recoveredโthe .recover command attempts to salvage what is structurally intact but may lose recent transactions or partial records. For mission-critical databases, implement regular automated backups and consider using a server-based database (PostgreSQL, MySQL) with built-in replication and recovery features.
Frequently asked questions
- What does "SQLITE_CORRUPT: The database disk image is malformed" mean?
- This error (SQLite error code 11) indicates that the physical structure of the SQLite database file has been corrupted. SQLite detected inconsistencies in the database file format, internal page structures, or table/index organization during file verification. This can occur due to hardware failures, improper shutdowns, concurrent write conflicts, or application bugs writing to closed file descriptors.
- How do I fix "SQLITE_CORRUPT: The database disk image is malformed"?
- Database corruption prevents all queries and operations. Use PRAGMA integrity_check to diagnose the issue, then recover data using the .recover command or dump-and-rebuild method to restore functionality.
- What causes "SQLITE_CORRUPT: The database disk image is malformed"?
- Hardware failure or SSD/disk corruption affecting the database file Sudden system crash or forced application termination during an active write operation Concurrent processes accessing the database without proper locking coordination
- Is the "SQLITE_CORRUPT: The database disk image is malformed" error serious?
- Yes. This is a critical-severity error that usually blocks work until it is fixed, but the steps above resolve it.
Official resources & further reading
- ๐SQLite Official Documentation
- ๐SQLite Docs - Recovering Data From A Corrupt SQLite Database(Official Docs)
- ๐SQLite Docs - Result and Error Codes(Official Docs)
- ๐SQLite Docs - How To Corrupt An SQLite Database(Official Docs)
- ๐