Skip to content

Instantly share code, notes, and snippets.

@promto-c
Last active April 22, 2025 21:00
Show Gist options
  • Save promto-c/531e3d3321f1c2fa66487054b2e040c2 to your computer and use it in GitHub Desktop.
Save promto-c/531e3d3321f1c2fa66487054b2e040c2 to your computer and use it in GitHub Desktop.
A comprehensive guide to SQLite's journal modes, including WAL, DELETE, TRUNCATE, PERSIST, MEMORY, and OFF. Understand the differences, use cases, and how to switch modes to optimize your SQLite database for performance, concurrency, and reliability.

SQLite Journal Modes Explained

SQLite supports several journal modes, each offering advantages in performance, concurrency, and the way changes are logged and applied. This guide explains the journal modes available in SQLite so you can choose the right one for your application.

1. WAL (Write‑Ahead Logging)

PRAGMA journal_mode = WAL;
  • Description
    • Writes all changes first into a separate -wal file. Readers continue to see a consistent snapshot of the main database while writers append to the WAL. On checkpoint or close, changes are merged back.
  • Use Cases
    • High‑concurrency apps with many readers and occasional writers
    • Situations where write bursts must not block reads

2. DELETE (Rollback Journal)

PRAGMA journal_mode = DELETE;
  • Description
    • The default mode. Before each transaction, modified pages are saved in a rollback journal file. On commit, changes are applied to the main file and the journal is deleted.
  • Use Cases
    • Simple single‑user applications
    • Environments with low concurrency

3. TRUNCATE

PRAGMA journal_mode = TRUNCATE;
  • Description
    • Like DELETE, except the journal file is truncated to zero length instead of deleted. Fewer filesystem operations can yield a small speed boost.
  • Use Cases
    • Similar to DELETE, but on filesystems where deleting is expensive

4. PERSIST

PRAGMA journal_mode = PERSIST;
  • Description
    • Keeps the journal file around but zeroes its header to mark it “empty.” Avoids delete/truncate overhead while still providing rollback safety.
  • Use Cases
    • High‑write workloads where reducing file‑create/delete churn matters

5. MEMORY

PRAGMA journal_mode = MEMORY;
  • Description
    • Stores the rollback journal entirely in RAM. Eliminates all journal‑related disk I/O—fastest transactions, but if the process crashes, uncommitted changes and the journal itself are lost.
  • Use Cases
    • In‑memory or temporary caches where speed is crucial and data persistence is not a concern.

6. OFF

PRAGMA journal_mode = OFF;
  • Description
    • Turns off rollback journaling completely. Writes go straight to the main database file without any atomic rollback, risking corruption on failures.
  • Use Cases
    • Read‑only media (e.g., CD/DVD)
    • Transient data where speed beats reliability

Quick Tip

  • WAL is the only journal mode that stays set permanently across connections and restarts.
  • All other modes (DELETE, TRUNCATE, PERSIST, MEMORY, OFF) will revert to DELETE when you reopen the database.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment