From 357c684a21d18be81e45ca02625a254d20e4f876 Mon Sep 17 00:00:00 2001 From: zarzet Date: Tue, 14 Jul 2026 07:14:25 +0700 Subject: [PATCH] perf(db): enable incremental auto-vacuum so cleared data shrinks the file --- lib/services/history_database.dart | 5 +++++ lib/services/sqlite_helpers.dart | 4 ++++ 2 files changed, 9 insertions(+) diff --git a/lib/services/history_database.dart b/lib/services/history_database.dart index f5c49199..cde69b50 100644 --- a/lib/services/history_database.dart +++ b/lib/services/history_database.dart @@ -796,6 +796,11 @@ class HistoryDatabase { await txn.delete('history_path_keys'); await txn.delete('history'); }); + // Return freed pages to the OS (no-op unless the file was created with + // auto_vacuum enabled). + try { + await db.execute('PRAGMA incremental_vacuum'); + } catch (_) {} _log.i('Cleared all history'); } diff --git a/lib/services/sqlite_helpers.dart b/lib/services/sqlite_helpers.dart index 829b1abf..03213d44 100644 --- a/lib/services/sqlite_helpers.dart +++ b/lib/services/sqlite_helpers.dart @@ -28,6 +28,10 @@ Future openAppDatabase( if (foreignKeys) { await db.execute('PRAGMA foreign_keys = ON'); } + // Without auto_vacuum the file never shrinks after deletes — its size + // is a permanent high-water mark. Only takes effect on newly created + // databases; existing files keep their mode until a manual VACUUM. + await db.execute('PRAGMA auto_vacuum = INCREMENTAL'); await db.rawQuery('PRAGMA journal_mode = WAL'); await db.execute('PRAGMA synchronous = NORMAL'); },