---
title: "Checkpoint a SQLite WAL in C#"
url: https://recursiveai.org/onelite/cookbook/checkpoint-sqlite-wal/
description: "Run PRAGMA wal_checkpoint from C#, copy committed SQLite WAL frames into the database, and read the busy flag and both frame counts."
version: "1.0.0"
---

# Checkpoint a SQLite WAL in C#

**OneLite** is a free, open-source SQLite-compatible database for .NET from Recursive AI. MIT or Apache-2.0, no native dependencies.

[NuGet](https://www.nuget.org/packages/RecursiveAI.OneLite) · [GitHub](https://github.com/recursive-ai-oss/onelite-csharp) · [Docs](https://recursiveai.org/onelite/docs/) · [Download example (.zip)](https://recursiveai.org/onelite/cookbook/checkpoint-sqlite-wal/checkpoint-sqlite-wal.zip)

A checkpoint copies committed WAL frames back into the main `.db` file.

```shell
dotnet add package RecursiveAI.OneLite
```

Requires [.NET 10](https://dotnet.microsoft.com/download/dotnet/10.0) (net10.0); .NET 9 and earlier aren’t supported.

```csharp
using RecursiveAI.OneLite;
var file = Path.GetRandomFileName();
using var db = Database.OpenDefault(file);
db.Execute($"CREATE TABLE notes(id)");
using var conn = db.Connect();
using var cmd = conn.Prepare(
    "PRAGMA wal_checkpoint(FULL)");
if (cmd.Step()) Console.Write(cmd.GetInt64(2));
```

Unzip [the example](https://recursiveai.org/onelite/cookbook/checkpoint-sqlite-wal/checkpoint-sqlite-wal.zip), then `dotnet run`.

```text
2
```

The snippet prints how many page versions it copied from the write-ahead log (WAL): 2 in this scratch database.

`db.Execute` takes an interpolated string (`$"..."`); any values inserted with `{...}` become SQL parameters.

## The full program

```csharp
using RecursiveAI.OneLite;

string path = Path.GetRandomFileName();
using (var db = Database.OpenDefault(path))
using (var conn = db.Connect())
{
    db.Execute($"CREATE TABLE notes(body TEXT)");
    using var cmd = conn.Prepare(
        "PRAGMA wal_checkpoint(FULL)");
    if (!cmd.Step()) throw new InvalidOperationException();
    Console.WriteLine($"busy={cmd.GetInt64(0)}");
    Console.WriteLine($"log={cmd.GetInt64(1)}");
    Console.WriteLine($"copied={cmd.GetInt64(2)}");
}
File.Delete(path);
```

```text
busy=0
log=2
copied=2
```

The program creates and deletes a scratch database. `OpenDefault` enables WAL. The `FULL` checkpoint copied both frames (page versions) in the log; a reader or writer can prevent it from completing, in which case the first result column can be `1`.

## Variations

**Empty the WAL file after copying its frames.** Replace `FULL` with `TRUNCATE` in the pragma. After a successful truncate, the counters are zero; the log no longer contains any frames.

```sql
PRAGMA wal_checkpoint(TRUNCATE)
```

```text
busy=0
log=0
copied=0
```

## Related recipes

[Open an existing SQLite database](https://recursiveai.org/onelite/cookbook/open-existing-sqlite-database-csharp/) · [Use SQLite transactions](https://recursiveai.org/onelite/cookbook/sqlite-transactions-csharp/) · [Back up a SQLite database](https://recursiveai.org/onelite/cookbook/backup-sqlite-database/)

---

## The short answer

Run `PRAGMA wal_checkpoint(FULL)` on a file-backed database in WAL mode. Read its result row: the first column is the busy flag, the second is the number of frames in the log, and the third is the number already copied back into the database by the end of the checkpoint. These are counts of pages in the log, not SQL rows. If the busy flag is `1`, don’t treat the requested checkpoint as complete.

The same `PRAGMA` works through `Microsoft.Data.Sqlite`, including on EF Core’s SQLite connection when configured with the [OneLite adapter](https://recursiveai.org/onelite/docs/adapter/).

## What is a SQLite WAL file?

In [WAL mode](https://sqlite.org/wal.html), SQLite records committed changes in a separate `-wal` file. A reader may need that file to see the committed state even when the latest pages are not yet in the main database file. A checkpoint copies those pages back. It is not a commit, and a successful commit doesn’t mean that a checkpoint has already happened. OneLite’s automatic checkpoint is passive and defaults to a threshold of 1,000 frames. You can request a checkpoint before that threshold, as the example does.

## Why can a SQLite WAL checkpoint be blocked?

A reader can still be using an older snapshot, so the checkpoint can’t overwrite main-file pages that reader needs. `PASSIVE` copies whatever it can without waiting, so compare its counts rather than treating a zero busy flag as proof of a complete copy. `FULL` can wait according to the busy policy. If an operation still needs the log, let the reader finish or retry the checkpoint later. See the [SQLite pragma reference](https://sqlite.org/pragma.html#pragma_wal_checkpoint) for the result columns and when the counts are `-1`.

A successful `FULL` checkpoint doesn’t promise a zero-byte `-wal` file. `RESTART` also waits until readers are done with the log so the next writer can restart it. `TRUNCATE` goes one step further and truncates the log immediately on success. Don’t delete `-wal` to make it smaller: committed changes might still be there.
