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 freelist_count tells you how much a SQLite VACUUM would give back

Sqlite
~/tips/tip-sqlite-freelist-count-before-vacuum

sqlite3 -header app.db "SELECT * FROM pragma_page_count, pragma_freelist_count, pragma_page_size;"

Checks how many empty pages a SQLite file holds before you rebuild it.

Deleting rows from SQLite does not make the file smaller. The freed pages go on a free list and wait to be reused. Before a full VACUUM, I’d check whether it is worth it.

sqlite3 -header app.db "SELECT * FROM pragma_page_count, pragma_freelist_count, pragma_page_size;"
page_count|freelist_count|page_size
1058|526|4096

Pragmas that return a value can be used as table-valued functions with a pragma_ prefix, so one query gives all three numbers. Here half of the 1058 pages are free, and with 4096 bytes per page that’s about 2 MB a VACUUM would give back. On a 4 MB file that’s worth it. When the free list is a few pages, leave the file alone.

Deleting half of the rows does not always free half of the pages, by the way. A page only goes on the free list when it is completely empty, so scattered deletes leave many half-full pages and a small free list. In that case VACUUM still helps, because it repacks the rows, but this number underestimates the gain.

VACUUM itself needs care. It rewrites the whole file, needs up to twice the size in free disk space while it runs, and holds a write lock the whole time. Plan it for a quiet moment.

Further reading: PRAGMA freelist_count, SQLite VACUUM

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