-
Notifications
You must be signed in to change notification settings - Fork 4.2k
docs(sqlite): clarify WAL sidecar cleanup behavior after close #27915
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -133,6 +133,25 @@ db.close(true); | |
| has no effect after the first. | ||
| </Note> | ||
|
|
||
| <Important title="WAL sidecar files"> | ||
| When using WAL (Write-Ahead Logging) mode with a file-based database, SQLite creates additional sidecar files: | ||
| `-wal` (write-ahead log) and `-shm` (shared memory). The cleanup behavior of these files after calling `.close()` | ||
| varies by platform and SQLite configuration: | ||
|
|
||
| - **Linux**: Sidecar files are typically cleaned up after close | ||
| - **macOS**: Sidecar files may persist after close due to platform-specific SQLite behavior | ||
| - **Windows**: Behavior varies by Windows version and SQLite configuration | ||
|
|
||
| If you need to ensure sidecar files are cleaned up, consider manually removing them after closing the database: | ||
| ```ts | ||
| import { rmSync } from "node:fs"; | ||
|
|
||
| db.close(); | ||
| rmSync("mydb.sqlite-wal"); | ||
| rmSync("mydb.sqlite-shm"); | ||
| ``` | ||
| </Important> | ||
|
Comment on lines
+136
to
+153
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Cross-reference the The linked issue ( Consider restructuring to recommend the 📝 Suggested documentation improvement If you need to ensure sidecar files are cleaned up, consider manually removing them after closing the database:
+ If you need deterministic cleanup of sidecar files, use the `fileControl` API before closing:
+ ```ts
+ import { Database, constants } from "bun:sqlite";
+
+ const db = new Database("mydb.sqlite");
+ db.run("PRAGMA journal_mode = WAL;");
+ // Disable WAL persistence - files will be cleaned up on close
+ db.fileControl(constants.SQLITE_FCNTL_PERSIST_WAL, 0);
+ // ... use database ...
+ db.close();
+ ```
+
+ Alternatively, you can manually remove the files after closing:
```ts
- import { rmSync } from "node:fs";
+ import { rmSync } from "node:fs";
db.close();
- rmSync("mydb.sqlite-wal");
- rmSync("mydb.sqlite-shm");
+ rmSync("mydb.sqlite-wal", { force: true });
+ rmSync("mydb.sqlite-shm", { force: true });🤖 Prompt for AI Agents |
||
|
|
||
| ### `using` statement | ||
|
|
||
| You can use the `using` statement to ensure that a database connection is closed when the `using` block is exited. | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Add
{ force: true }to prevent errors when files don't exist.The
rmSynccalls will throwENOENTif the sidecar files don't exist (e.g., on Linux where they may already be cleaned up, or if WAL mode wasn't actually used). Adding{ force: true }makes the cleanup idempotent and safe across platforms.🛠️ Proposed fix
```ts import { rmSync } from "node:fs"; db.close(); - rmSync("mydb.sqlite-wal"); - rmSync("mydb.sqlite-shm"); + rmSync("mydb.sqlite-wal", { force: true }); + rmSync("mydb.sqlite-shm", { force: true });🤖 Prompt for AI Agents