skip to content

After deleting most rows from a SQLite database, why does the file stay the same size, and how do VACUUM and auto_vacuum reclaim the space?

level: middleimportance: should knowfreq 45%

answer

  1. freed pages are kept
  2. the freelist
  3. rebuild into a temporary copy
  4. up to twice the disk space
  5. auto_vacuum chosen before tables

basics

~20 s

By default, freed pages go onto SQLite's freelist and are reused by later inserts, so the file does not shrink. VACUUM rebuilds it compactly; auto_vacuum FULL or INCREMENTAL truncates freelist pages, but must be set before tables exist or applied by VACUUM.

solid answer

~50 s

With the default `auto_vacuum=NONE`, pages emptied by a delete go onto the **freelist** and are reused by later inserts; the file keeps its size, as `PRAGMA freelist_count` shows. `VACUUM` copies the content into a temporary database and writes it back over the original, so the file shrinks, tables and indexes become largely contiguous, partly filled pages may be repacked and deleted content is purged. It needs up to twice the file's size in free disk space, it is a write that fails if another connection holds a lock that prevents writing, and it may change the rowids of tables without an explicit `INTEGER PRIMARY KEY`. `auto_vacuum=FULL` moves freelist pages to the end and truncates them at every commit; `INCREMENTAL` keeps the same bookkeeping and frees pages only when you run `PRAGMA incremental_vacuum(N)`. Neither repacks pages, and switching from NONE needs a new database or a VACUUM.

code

sql · 4 lines
sql
PRAGMA page_size;
PRAGMA page_count;
PRAGMA freelist_count;
VACUUM;

go deeper

for a junior

Recall that deleted pages go on a freelist and get reused, so the file only shrinks after a VACUUM or with auto-vacuum enabled.

for a middle

Explain how VACUUM rebuilds through a temporary copy and what it costs, and how FULL and INCREMENTAL auto-vacuum differ and when each can be switched on.

for a senior

Show operational care: measure freelist_count first, check free disk and locks before a VACUUM, and watch for rowid changes in tables without an INTEGER PRIMARY KEY.

for a principal

Set the default for new databases a team ships: whether files are created with INCREMENTAL auto-vacuum and who runs the reclaim, since the setting is hard to change once tables exist.

## Where deleted space goes A SQLite database file is a sequence of fixed-size pages. When a `DELETE` or a `DROP TABLE` empties pages, SQLite does not hand them back to the filesystem under the default setting, `auto_vacuum=NONE`. It adds them to the **freelist**, a linked list of unused pages whose first trunk page and total count are recorded in the database header, and later inserts reuse those pages before the file grows. No space is lost, but the file does not shrink. Two pragmas show the situation: - `PRAGMA page_count;` returns the total number of pages in the file; - `PRAGMA freelist_count;` returns how many of them are unused. Multiply either by `PRAGMA page_size` to get bytes. A file of 50,000 pages with 45,000 on the freelist is mostly empty space. Deleted content is not erased by default either: the bytes stay in free space where forensic tools can find them, unless `PRAGMA secure_delete` is on, which overwrites deleted content with zeros. ## What VACUUM does `VACUUM` rebuilds the database file in two steps: 1. it copies the contents of the database into a temporary database file; 2. it overwrites the original with that content, using a rollback journal or the WAL exactly as any other transaction would. The result has no freelist pages, stores each table and index largely contiguously, may have fewer partly filled pages, and holds no traces of deleted content. The costs: - **disk space**: as much as twice the size of the original file may be needed in free space while it runs; - **locking**: `VACUUM` is a write, so it fails if another connection holds a lock that prevents writes, and it fails on a connection that has a transaction open; an unfinalized statement typically holds a read transaction open, so it can make `VACUUM` fail too; - **rowids**: it may change the rowids of tables that have no explicit `INTEGER PRIMARY KEY`, so anything that stored those rowids outside the table can break; - **time**: it rewrites the whole file, however little space it gains. `VACUUM INTO 'file'` performs the same rebuild into a new file and leaves the original untouched, so it is a way to make a compact backup rather than a way to shrink the live file. ## auto_vacuum: FULL and INCREMENTAL Auto-vacuum lets the file shrink without a full rebuild. It needs extra bookkeeping inside the file, **pointer-map pages** that let SQLite trace each page back to the page that refers to it, so that it can move pages around safely. | Setting | What happens to freed pages | When it happens | |---|---|---| | `NONE` (0, default) | Kept on the freelist and reused | Never on its own; only `VACUUM` shrinks the file | | `FULL` (1) | Moved to the end of the file and truncated away | At every transaction commit | | `INCREMENTAL` (2) | Bookkeeping kept, nothing freed automatically | When the application runs `PRAGMA incremental_vacuum(N)`: up to N pages, or the whole freelist if N is omitted or below 1 | Neither mode replaces `VACUUM`. Both only truncate freelist pages; they do not defragment the file or repack partly filled pages, and because they move pages around they can make fragmentation worse. ## Changing these settings on an existing file Auto-vacuum is normally chosen before the first table is created. Switching between `FULL` and `INCREMENTAL` works at any time, but going from `NONE` to either one needs a brand-new database or a `VACUUM` after setting the pragma, and going back to `NONE` always requires a `VACUUM`, even on an empty database. The same rebuild is how the page size of an existing database changes, with one restriction: in WAL mode, `VACUUM` can change the auto-vacuum setting but not the page size. ```sql PRAGMA auto_vacuum = INCREMENTAL; VACUUM; -- rebuilds the file with the new setting PRAGMA incremental_vacuum(500); -- later: free up to 500 pages ``` ## Choosing a policy - **Deletes in bursts** (expiring a cache, purging old messages) and a need to give storage back without a long rebuild: create the database with `auto_vacuum=INCREMENTAL` and run `PRAGMA incremental_vacuum` at an idle moment. - **Steady growth, rare deletes**: leave `NONE`; freed pages are reused and nothing needs doing. - **A one-off large purge, or years of churn**: schedule a `VACUUM` when the app is idle, after checking that up to twice the file's size is free and that nothing outside the database stores rowids of tables without an `INTEGER PRIMARY KEY`.

  • Why can SQLite's VACUUM fail even though no other process is writing to the database?
    VACUUM fails if its own connection has a transaction open, and unfinalized statements on that connection typically hold a read transaction open, so finish or finalize them first. It also fails if another connection holds a lock that prevents writes, since VACUUM is a write, and it can run out of disk because it may need up to twice the file's size.
  • When is SQLite's auto_vacuum=INCREMENTAL a better choice than FULL?
    FULL does its truncation work at every commit, adding cost to ordinary writes. INCREMENTAL keeps the same bookkeeping but frees pages only when the application runs PRAGMA incremental_vacuum(N), so an app that deletes in bursts can return space at an idle moment, in bounded chunks. Both still fragment the file and neither repacks pages the way VACUUM does.

  • A DELETE in SQLite returns freed space to the filesystem straight away.
  • auto_vacuum=FULL leaves the file as compact and defragmented as VACUUM does.
  • Running PRAGMA auto_vacuum=FULL alone enables auto-vacuum on an existing database with tables.
  • VACUUM works in place and needs no extra disk space.
  • VACUUM never changes any rowids.