clearAllData method
Clear all data (database and index files) and reset the engine.
This is a destructive operation that:
- Closes the database connection and the vector store
- Deletes the SQLite database file and its sidecars
- Deletes the vector store and the HNSW indexes of every collection
- Re-initializes the database and service
Implementation
Future<void> clearAllData() async {
debugPrint('[RagEngine] clearAllData: Starting...');
// A deferred warmup may still be reading from SQLite. Let it leave the
// shared database before closing the pool and deleting its files.
try {
await _ragService.warmupFuture;
} catch (error) {
debugPrint(
'[RagEngine] clearAllData: Previous warmup failed; continuing reset: $error',
);
}
// 1. Close DB pool
debugPrint('[RagEngine] clearAllData: Closing DB pool...');
await closeDbPool();
// Unmap the loaded HNSW index too: Windows refuses to delete a file that
// is still memory-mapped, so its `.hnsw` would survive the sweep below.
await clearHnswIndex();
debugPrint('[RagEngine] clearAllData: DB pool closed.');
// 2-3. Delete the database and everything derived from it: SQLite
// sidecars, the mmap vector store (the only copy of quantized embeddings),
// and the HNSW indexes and dirty markers of every collection. A stale
// index left behind could pass the node-count parity check against the
// new database and map old vectors onto reused chunk ids.
final dbFile = File(dbPath);
final dbFileName = dbFile.uri.pathSegments.last;
var deleted = 0;
if (await dbFile.parent.exists()) {
await for (final entity in dbFile.parent.list(followLinks: false)) {
if (entity is! File) continue;
final name = entity.uri.pathSegments.last;
if (!isDatabaseArtifact(dbFileName, name)) continue;
await entity.delete();
deleted++;
}
}
debugPrint(
'[RagEngine] clearAllData: Deleted $deleted database artifact(s).',
);
// 4. Re-initialize DB pool
debugPrint('[RagEngine] clearAllData: Re-initializing DB pool...');
await initDbPool(dbPath: dbPath, maxSize: 4);
debugPrint('[RagEngine] clearAllData: DB pool initialized.');
// 5. Re-initialize service
debugPrint('[RagEngine] clearAllData: Re-initializing service...');
RagEmbeddingFingerprintLock? resetLock;
await _ragService.initForEngine(
// A destructive reset is complete only when the replacement indexes are
// ready. Returning with another background warmup would let an immediate
// ingest race the same database that clearAllData just recreated.
deferIndexWarmup: false,
afterDatabaseInitialized: () async {
resetLock = await _resolveFingerprintGate(currentEmbeddingFingerprint);
},
);
_fingerprintLock = resetLock;
_collectionServices
..clear()
..[SourceRagService.defaultCollectionId] = _ragService;
_initializedCollections
..clear()
..add(SourceRagService.defaultCollectionId);
_collectionInitInFlight.clear();
debugPrint('[RagEngine] clearAllData: Service initialized. Done.');
}