Demystifying SQLite Database Files: The Core Relationship Between .sqlite, -wal, and -shm
Content
When developing or maintaining applications (such as the local storage module of `wiki.lib00.com`), if you check the directory where your SQLite database is located, you will often see a group of files like this:
```bash
-rw-r--r--@ 1 lee staff 204800 Apr 20 02:13 state_5.sqlite
-rw-r--r--@ 1 lee staff 32768 Apr 20 02:19 state_5.sqlite-shm
-rw-r--r--@ 1 lee staff 222512 Apr 20 02:21 state_5.sqlite-wal
```
The presence of these files indicates that the SQLite database is currently operating in **WAL (Write-Ahead Logging) mode**. This article will explain the specific functions and relationships of these three files in detail.
## 1. `.sqlite` (The Main Database File)
* **Function**: This is the core file of the database, storing metadata, table structures, indexes, and committed, archived data.
* **Status**: In WAL mode, this file is **not necessarily up-to-date**. The latest data modifications might still be temporarily stored in the `-wal` file waiting to be synchronized.
---
## 2. `-wal` (The Write-Ahead Log File)
* **Function**: Stores new transactions and modifications that have not yet been synchronized (Checkpointed) to the main database file.
* **Relationship**: When a write operation occurs, SQLite appends the data to this `-wal` file instead of directly modifying the `.sqlite` file. This mechanism allows for "non-blocking read and write" (reads access the main file and parts of the WAL, while writes append to the WAL), significantly improving concurrent performance.
* **Warning**: This file contains the latest data. **Never delete it manually**, or you will lose recent transactions and potentially corrupt the database.
---
## 3. `-shm` (The Shared Memory File)
* **Function**: Acts as an index for the `-wal` file.
* **Relationship**: It stores metadata pointing to the contents of the WAL file, allowing multiple processes to quickly locate data blocks within the WAL, thus coordinating concurrent access.
* **Note**: It is a temporary file and does not contain persistent data. It is usually deleted automatically when all database connections are closed normally.
---
## Bonus: Enabling WAL Mode and the Checkpoint Mechanism
As a bonus tip, DP@lib00 recommends actively enabling WAL mode in most high-concurrency scenarios. You can enable it by executing the following SQL statement:
```sql
PRAGMA journal_mode=WAL;
```
**Cleanup Mechanism (Checkpointing)**:
When the last database connection is closed normally, or when the WAL file reaches a certain size (default 1000 pages), SQLite triggers a Checkpoint operation. At this point, the data in the `-wal` file is merged back into the main `.sqlite` file. Once the merge is complete, the `-wal` and `-shm` files will usually disappear or be truncated for reuse.
**Core Summary**: During runtime, these three files together constitute a single, complete logical database. While the database is running or hasn't been closed properly, **never manually delete or move** any of these files.
Related Contents
The Ultimate Guide to MySQL Partitioning: From Creation and Automation to Avoiding Pitfalls
Duration: 00:00 | DP | 2025-12-01 08:00:00Optimizing Million-Scale PV Log Tables: The Elegant Shift from VARCHAR to TINYINT
Duration: 00:00 | DP | 2025-12-30 23:18:20The Hidden Cost of Speed: How Much Space Do MySQL InnoDB Indexes Really Consume?
Duration: 00:00 | DP | 2026-02-01 08:38:42Unlocking MySQL Integer Types: SMALLINT vs. MEDIUMINT Range and Best Practices
Duration: 00:00 | DP | 2026-03-03 19:25:01Recommended
How to Choose Your pgvector Docker Image Version: PG16, 17, & 18 Explained with Best Practices
00:00 | 51Confused by pgvector Docker image tags like pg16, ...
Linux Command-Line Mystery: Why `ll` Hides Files like `.idea` & The Ultimate `ls` vs. `ll` Showdown
00:00 | 124Ever wondered why the `ll` command doesn't show hi...
Bootstrap 5.3: The Ultimate Guide to Creating Flawless Help Icon Tooltips
00:00 | 97Learn the best practice for creating help icon too...
The Ultimate 'Connection Refused' Guide: A PHP PDO & Docker Debugging Saga of a Forgotten Port
00:00 | 122A deep dive into a tricky PHP PDO `SQLSTATE[HY000]...