brewedbits.dev

Personal blog with guides and news on selfhosting, Linux and AI, from the Netherlands

nextDash nextdash --aboutYour bookmarks. Your terminal. checks your links knows your Docker host one container, no cloudLive demo →nextdash.cc ↗

Tip: PRAGMA wal_checkpoint(TRUNCATE) shrinks a SQLite -wal file that keeps growing

Sqlite
~/tips/tip-sqlite-wal-checkpoint-truncate

sqlite3 app.db "PRAGMA wal_checkpoint(TRUNCATE);"

Moves everything from the write-ahead log into the main file and resets the log to zero bytes.

In WAL mode, SQLite copies the log back into the main file now and then. If some reader always has the file open, that copy can never finish and the -wal file keeps growing.

sqlite3 app.db "PRAGMA wal_checkpoint(TRUNCATE);"
0|0|0

The three numbers are: whether it was blocked (0 means no), how many pages the log held, and how many of them made it into the database. TRUNCATE waits for the writer, copies everything, and then cuts the -wal file back to zero bytes. A plain PRAGMA wal_checkpoint; is the gentler PASSIVE mode: it copies what it can without waiting and leaves the file at its size.

When the first number is 1, a reader or writer was in the way. Find the process that keeps a read transaction open, which is often a long-running script or a backup tool, and the problem goes away.

It is worth running before a plain file copy of a quiet database, so the main file holds everything. For a running app it is still a stopgap. If the log grows again every day, the real fix is in the app: close read transactions quickly. PRAGMA journal_size_limit can also cap how big the file stays after a checkpoint, which I find a cleaner fix than calling TRUNCATE from cron.

Further reading: PRAGMA wal_checkpoint, SQLite write-ahead logging

More Sqlite tips SELECT unixepoch('2026-01-01 00:00:00'); SELECT name, iif(qty > 0, 'in stock', 'sold out') AS state sqlite3 -bail app.db < migrate.sql CREATE INDEX 'orders_user_id' ON 'orders'('user_id'); –> users(id) CREATE INDEX users_active_email ON users(email) WHERE active = 1; All Sqlite tips (27) All topics

Link