Run VACUUM INTO 'compact.db' on your existing notes.db to write a compacted copy without changing the original. Choose a fresh output path.
dotnet add package RecursiveAI.OneLiteRequires .NET 10 (net10.0); .NET 9 and earlier aren’t supported.
using RecursiveAI.OneLite; await using var db = await Database.OpenReadOnlyAsync( "notes.db"); string output = "compact.db"; await db.ExecuteAsync( $"VACUUM INTO {output}"); Console.WriteLine($"Wrote {output}");
OneLite binds {output} in $"VACUUM INTO {output}" as a SQL parameter rather than inserting it into the SQL, so you don’t need quotes around it.
Unzip the example, then dotnet run.
Wrote compact.dbThe full program
Run with no arguments to make a sample in a fresh temporary directory. It inserts 100 notes with padded text, deletes all but three, then counts the surviving rows in the compacted copy. To compact your own database, use dotnet run -- notes.db compact.db.
using RecursiveAI.OneLite; if (args.Length == 2) { await Compact(args[0], args[1]); return; } var demo = Directory.CreateTempSubdirectory("onelite-vacuum-"); string source = Path.Combine(demo.FullName, "notes.db"); string output = Path.Combine(demo.FullName, "compact.db"); await using (var setup = await Database.OpenAsync(source, new DatabaseOptions())) { await setup.ExecuteAsync($"CREATE TABLE notes(id INTEGER PRIMARY KEY, body TEXT)"); await setup.ExecuteAsync($""" INSERT INTO notes(body) SELECT 'Note ' || value || hex(zeroblob(900)) FROM generate_series(1, 100) """); await setup.ExecuteAsync($"DELETE FROM notes WHERE id > 3"); } await Compact(source, output); await using var copy = await Database.OpenReadOnlyAsync(output); Console.WriteLine($"Source: {new FileInfo(source).Length} bytes"); Console.WriteLine($"Copy: {new FileInfo(output).Length} bytes"); Console.WriteLine($"Rows in copy: {await copy.ScalarAsync<long>($"SELECT count(*) FROM notes")}"); static async Task Compact(string source, string output) { await using var db = await Database.OpenReadOnlyAsync(source); await db.ExecuteAsync($"VACUUM INTO {output}"); Console.WriteLine($"Wrote {Path.GetFileName(output)}"); }
Wrote compact.db Source: 212992 bytes Copy: 16384 bytes Rows in copy: 3
If the destination already contains data, OneLite throws OneLiteException with output file already exists.
Variations
Name the source schema explicitly. Replace the ExecuteAsync call and run with a fresh output path.
await db.ExecuteAsync( $"VACUUM main INTO {output}");
Related recipes
The short answer
To copy and compact a SQLite database from C#, execute VACUUM INTO on the source connection. You can open the source with OpenReadOnlyAsync; only the destination needs to be writable. OneLite opens and writes standard SQLite files; see the SQLite compatibility notes for details.
What does it mean to vacuum a database?
A delete removes a row logically, but the database can keep freed pages for future inserts. The file may stay the same size even after you delete most of its rows. VACUUM INTO reads the remaining table and index entries and packs them into a separate file. The original retains its existing allocation. If you need space for future inserts in the original, the free pages already serve that purpose; if you need a smaller file to hand off, use the new copy.
OneLite’s VACUUM INTO documentation says the copy preserves the source’s page size, text encoding, auto-vacuum mode, user_version, and application_id. The copy has an empty freelist, the list of pages available for reuse. It uses the rollback-journal file format even if the source uses WAL (write-ahead logging).
What’s the difference between VACUUM and VACUUM INTO?
In SQLite’s VACUUM syntax, plain VACUUM rebuilds the original file. VACUUM INTO builds the new image at the destination instead of overwriting the original. Unlike an incremental backup, compaction is a single SQL statement, not a series of page-copy steps. Budget disk space for both files while the statement runs.
Why does VACUUM INTO say the output file already exists?
The destination may be absent or an empty file. VACUUM INTO won’t replace a nonempty destination, including a previous copy. For periodic snapshots, use a new output name on each run. A relative output path is resolved from the process’s working directory, not the source file’s directory.
Run the statement outside an explicit transaction. If the source connection is already inside one, commit or roll it back first; otherwise OneLite reports cannot VACUUM from within a transaction. OneLite writes the destination with journaling off and removes the partial output if the statement fails. Treat the output as a finished snapshot only after the statement returns successfully.