docs(sqlite): clarify WAL sidecar cleanup behavior after close#27915
docs(sqlite): clarify WAL sidecar cleanup behavior after close#27915ssing2 wants to merge 1 commit intooven-sh:mainfrom
Conversation
Fixes oven-sh#27481 Documents that WAL sidecar files (.db-wal, .db-shm) cleanup behavior varies by platform and SQLite configuration after db.close(): - Linux: sidecar files typically cleaned up after close - macOS: sidecar files may persist after close - Windows: behavior varies by version and configuration Provides guidance for manual cleanup when needed.
WalkthroughDocumentation update to Changes
🚥 Pre-merge checks | ✅ 3 | ❌ 1❌ Failed checks (1 inconclusive)
✅ Passed checks (3 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
There was a problem hiding this comment.
Actionable comments posted: 2
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.
Inline comments:
In `@docs/runtime/sqlite.mdx`:
- Around line 146-152: The rmSync calls can throw ENOENT if the WAL/SHM sidecar
files are absent; update the cleanup to call rmSync("mydb.sqlite-wal", { force:
true }) and rmSync("mydb.sqlite-shm", { force: true }) after db.close() so
removal is idempotent and safe across platforms — locate the calls to rmSync in
the snippet and add the { force: true } option.
- Around line 136-153: Update the "WAL sidecar files" Important note to
recommend using the deterministic SQLite API before suggesting manual deletion:
explain and show using Database and
db.fileControl(constants.SQLITE_FCNTL_PERSIST_WAL, 0) after enabling WAL (e.g.,
after db.run("PRAGMA journal_mode = WAL;")) so SQLite will clean up sidecar
files on db.close(), then present manual rmSync deletion as a fallback and use
rmSync("mydb.sqlite-wal", { force: true }) and rmSync("mydb.sqlite-shm", {
force: true }) to avoid errors when removing files.
ℹ️ Review info
⚙️ Run configuration
Configuration used: Path: .coderabbit.yaml
Review profile: ASSERTIVE
Plan: Pro
Run ID: 5d2b3b62-95a3-4eca-8695-7df976ca7695
📒 Files selected for processing (1)
docs/runtime/sqlite.mdx
| <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> |
There was a problem hiding this comment.
Cross-reference the fileControl API for deterministic cleanup.
The linked issue (#27481) specifically requests documenting db.fileControl(constants.SQLITE_FCNTL_PERSIST_WAL, 0) as the deterministic cleanup option. This API is already documented at lines 647-658, but this Important note doesn't mention it. The fileControl approach is preferable to manual file deletion as it instructs SQLite itself to clean up the files on close.
Consider restructuring to recommend the fileControl approach first, with manual deletion as a fallback:
📝 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 });</details>
<!-- suggestion_start -->
<details>
<summary>📝 Committable suggestion</summary>
> ‼️ **IMPORTANT**
> Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.
```suggestion
<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 deterministic cleanup of sidecar files, use the `fileControl` API before closing:
🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.
In `@docs/runtime/sqlite.mdx` around lines 136 - 153, Update the "WAL sidecar
files" Important note to recommend using the deterministic SQLite API before
suggesting manual deletion: explain and show using Database and
db.fileControl(constants.SQLITE_FCNTL_PERSIST_WAL, 0) after enabling WAL (e.g.,
after db.run("PRAGMA journal_mode = WAL;")) so SQLite will clean up sidecar
files on db.close(), then present manual rmSync deletion as a fallback and use
rmSync("mydb.sqlite-wal", { force: true }) and rmSync("mydb.sqlite-shm", {
force: true }) to avoid errors when removing files.
| ```ts | ||
| import { rmSync } from "node:fs"; | ||
|
|
||
| db.close(); | ||
| rmSync("mydb.sqlite-wal"); | ||
| rmSync("mydb.sqlite-shm"); | ||
| ``` |
There was a problem hiding this comment.
Add { force: true } to prevent errors when files don't exist.
The rmSync calls will throw ENOENT if 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 });</details>
<!-- suggestion_start -->
<details>
<summary>📝 Committable suggestion</summary>
> ‼️ **IMPORTANT**
> Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.
```suggestion
🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.
In `@docs/runtime/sqlite.mdx` around lines 146 - 152, The rmSync calls can throw
ENOENT if the WAL/SHM sidecar files are absent; update the cleanup to call
rmSync("mydb.sqlite-wal", { force: true }) and rmSync("mydb.sqlite-shm", {
force: true }) after db.close() so removal is idempotent and safe across
platforms — locate the calls to rmSync in the snippet and add the { force: true
} option.
Fixes #27481
Documents that WAL sidecar files (.db-wal, .db-shm) cleanup behavior
varies by platform and SQLite configuration after db.close():
Provides guidance for manual cleanup when needed.
Changes