Skip to content

YC (S26) | Open Computer History | Continuously record your company computer work, map your workflows, help you find work worth automating, and power your agents' context

fix: split delete into fast batch + one-time orphan cleanup

louis0301951e808047c0e2
diff --git a/crates/screenpipe-db/src/db.rs b/crates/screenpipe-db/src/db.rs
index c6c5f7c0c..ce7aa12e7 100644
--- a/crates/screenpipe-db/src/db.rs
+++ b/crates/screenpipe-db/src/db.rs
@@ -4654,6 +4654,222 @@ impl DatabaseManager {
})
}
+ /// Fast batch delete: only deletes time-range-bounded rows (ocr_text,
+ /// elements, frames, audio_transcriptions, ui_events). Skips the expensive
+ /// orphan cleanup (video_chunks, audio_chunks) which requires full-table
+ /// NOT IN scans. Call `cleanup_orphaned_chunks` once after all batches.
+ ///
+ /// Returns file paths and row counts. video_chunks_deleted and
+ /// audio_chunks_deleted will always be 0 — orphans are cleaned separately.
+ pub async fn delete_time_range_batch(
+ &self,
+ start: DateTime<Utc>,
+ end: DateTime<Utc>,
+ collect_all_files: bool,
+ ) -> Result<DeleteTimeRangeResult, sqlx::Error> {
+ let mut tx = self.begin_immediate_with_retry().await?;
+
+ let start_str = start.to_rfc3339();
+ let end_str = end.to_rfc3339();
+
+ // Collect snapshot files
+ let snapshot_files: Vec<String> = sqlx::query_scalar(
+ r#"SELECT snapshot_path FROM frames
+ WHERE timestamp BETWEEN ?1 AND ?2
+ AND snapshot_path IS NOT NULL"#,
+ )
+ .bind(&start_str)
+ .bind(&end_str)
+ .fetch_all(&mut **tx.conn())
+ .await?;
+
+ // Collect video files that are fully within this batch (all frames in chunk are in range)
+ let video_query = if collect_all_files {
+ // Local retention: collect all files regardless of cloud status
+ r#"SELECT file_path FROM video_chunks
+ WHERE id IN (SELECT DISTINCT video_chunk_id FROM frames WHERE timestamp BETWEEN ?1 AND ?2)
+ AND id NOT IN (SELECT DISTINCT video_chunk_id FROM frames WHERE timestamp NOT BETWEEN ?1 AND ?2)
+ AND file_path NOT LIKE 'cloud://%'"#
+ } else {
+ // Archive: only collect cloud-uploaded files
+ r#"SELECT file_path FROM video_chunks
+ WHERE id IN (SELECT DISTINCT video_chunk_id FROM frames WHERE timestamp BETWEEN ?1 AND ?2)
+ AND id NOT IN (SELECT DISTINCT video_chunk_id FROM frames WHERE timestamp NOT BETWEEN ?1 AND ?2)
+ AND (cloud_blob_id IS NOT NULL OR file_path LIKE 'cloud://%')"#
+ };
+ let video_files: Vec<String> = sqlx::query_scalar(video_query)
+ .bind(&start_str)
+ .bind(&end_str)
+ .fetch_all(&mut **tx.conn())
+ .await?;
+
+ // Collect audio files
+ let audio_files: Vec<String> = sqlx::query_scalar(
+ r#"SELECT file_path FROM audio_chunks
+ WHERE id IN (SELECT DISTINCT audio_chunk_id FROM audio_transcriptions WHERE timestamp BETWEEN ?1 AND ?2)
+ AND id NOT IN (SELECT DISTINCT audio_chunk_id FROM audio_transcriptions WHERE timestamp NOT BETWEEN ?1 AND ?2)
+ AND file_path NOT LIKE 'cloud://%'"#,
+ )
+ .bind(&start_str)
+ .bind(&end_str)
+ .fetch_all(&mut **tx.conn())
+ .await?;
+
+ // Delete ocr_text
+ let ocr_result = sqlx::query(
+ "DELETE FROM ocr_text WHERE frame_id IN (SELECT id FROM frames WHERE timestamp BETWEEN ?1 AND ?2)",
+ )
+ .bind(&start_str)
+ .bind(&end_str)
+ .execute(&mut **tx.conn())
+ .await?;
+ let ocr_deleted = ocr_result.rows_affected();
+
+ // Migrate elements from anchor frames
+ let anchor_ids: Vec<i64> = sqlx::query_scalar(
+ r#"SELECT DISTINCT f.id FROM frames f
+ WHERE f.timestamp BETWEEN ?1 AND ?2
+ AND EXISTS (
+ SELECT 1 FROM frames ref
+ WHERE ref.elements_ref_frame_id = f.id
+ AND ref.timestamp NOT BETWEEN ?1 AND ?2
+ )"#,
+ )
+ .bind(&start_str)
+ .bind(&end_str)
+ .fetch_all(&mut **tx.conn())
+ .await?;
+
+ for anchor_id in &anchor_ids {
+ let new_anchor_id: Option<i64> = sqlx::query_scalar(
+ r#"SELECT MIN(id) FROM frames
+ WHERE elements_ref_frame_id = ?1
+ AND timestamp NOT BETWEEN ?2 AND ?3"#,
+ )
+ .bind(anchor_id)
+ .bind(&start_str)
+ .bind(&end_str)
+ .fetch_optional(&mut **tx.conn())
+ .await?
+ .flatten();
+
+ if let Some(new_id) = new_anchor_id {
+ sqlx::query("UPDATE elements SET frame_id = ?1 WHERE frame_id = ?2")
+ .bind(new_id)
+ .bind(anchor_id)
+ .execute(&mut **tx.conn())
+ .await?;
+ sqlx::query(
+ "UPDATE frames SET elements_ref_frame_id = ?1 WHERE elements_ref_frame_id = ?2",
+ )
+ .bind(new_id)
+ .bind(anchor_id)
+ .execute(&mut **tx.conn())
+ .await?;
+ sqlx::query("UPDATE frames SET elements_ref_frame_id = NULL WHERE id = ?1")
+ .bind(new_id)
+ .execute(&mut **tx.conn())
+ .await?;
+ }
+ }
+
+ // Delete elements
+ sqlx::query(
+ "DELETE FROM elements WHERE frame_id IN (SELECT id FROM frames WHERE timestamp BETWEEN ?1 AND ?2)",
+ )
+ .bind(&start_str)
+ .bind(&end_str)
+ .execute(&mut **tx.conn())
+ .await?;
+
+ // Delete frames
+ let frames_result = sqlx::query("DELETE FROM frames WHERE timestamp BETWEEN ?1 AND ?2")
+ .bind(&start_str)
+ .bind(&end_str)
+ .execute(&mut **tx.conn())
+ .await?;
+ let frames_deleted = frames_result.rows_affected();
+
+ // NO orphan video_chunks cleanup here — done separately
+
+ // Delete audio_transcriptions
+ let audio_transcriptions_result =
+ sqlx::query("DELETE FROM audio_transcriptions WHERE timestamp BETWEEN ?1 AND ?2")
+ .bind(&start_str)
+ .bind(&end_str)
+ .execute(&mut **tx.conn())
+ .await?;
+ let audio_transcriptions_deleted = audio_transcriptions_result.rows_affected();
+
+ // NO orphan audio_chunks cleanup here — done separately
+
+ // Delete ui_events
+ let ui_events_result =
+ sqlx::query("DELETE FROM ui_events WHERE timestamp BETWEEN ?1 AND ?2")
+ .bind(&start_str)
+ .bind(&end_str)
+ .execute(&mut **tx.conn())
+ .await?;
+ let ui_events_deleted = ui_events_result.rows_affected();
+
+ tx.commit().await.map_err(|e| {
+ error!("failed to commit delete_time_range_batch transaction: {}", e);
+ e
+ })?;
+
+ debug!(
+ "delete_time_range_batch committed: frames={}, ocr={}, audio_transcriptions={}, ui_events={}",
+ frames_deleted, ocr_deleted, audio_transcriptions_deleted, ui_events_deleted
+ );
+
+ Ok(DeleteTimeRangeResult {
+ frames_deleted,
+ ocr_deleted,
+ audio_transcriptions_deleted,
+ audio_chunks_deleted: 0,
+ video_chunks_deleted: 0,
+ accessibility_deleted: 0,
+ ui_events_deleted,
+ video_files,
+ audio_files,
+ snapshot_files,
+ })
+ }
+
+ /// Clean up orphaned video_chunks and audio_chunks that no longer have
+ /// any referencing frames/transcriptions. This is the expensive operation
+ /// (full-table NOT IN scan) that should only run once after all batch
+ /// deletes are complete.
+ pub async fn cleanup_orphaned_chunks(&self) -> Result<(u64, u64), sqlx::Error> {
+ let mut tx = self.begin_immediate_with_retry().await?;
+
+ let video_chunks_result = sqlx::query(
+ "DELETE FROM video_chunks WHERE id NOT IN (SELECT DISTINCT video_chunk_id FROM frames)",
+ )
+ .execute(&mut **tx.conn())
+ .await?;
+ let video_chunks_deleted = video_chunks_result.rows_affected();
+
+ let audio_chunks_result = sqlx::query(
+ "DELETE FROM audio_chunks WHERE id NOT IN (SELECT DISTINCT audio_chunk_id FROM audio_transcriptions)",
+ )
+ .execute(&mut **tx.conn())
+ .await?;
+ let audio_chunks_deleted = audio_chunks_result.rows_affected();
+
+ tx.commit().await.map_err(|e| {
+ error!("failed to commit cleanup_orphaned_chunks transaction: {}", e);
+ e
+ })?;
+
+ debug!(
+ "cleanup_orphaned_chunks committed: video_chunks={}, audio_chunks={}",
+ video_chunks_deleted, audio_chunks_deleted
+ );
+
+ Ok((video_chunks_deleted, audio_chunks_deleted))
+ }
+
/// Returns the oldest timestamp across frames and audio_transcriptions.
/// Used by retention to avoid scanning from epoch.
pub async fn get_oldest_timestamp(&self) -> Result<Option<DateTime<Utc>>, sqlx::Error> {
diff --git a/crates/screenpipe-engine/src/archive.rs b/crates/screenpipe-engine/src/archive.rs
index 8285c65ca..1bc19234c 100644
--- a/crates/screenpipe-engine/src/archive.rs
+++ b/crates/screenpipe-engine/src/archive.rs
@@ -917,9 +917,9 @@ async fn count_records_in_range(
/// Only deletes video/snapshot files that have been uploaded to cloud
/// (cloud_blob_id IS NOT NULL).
///
-/// Batches deletes into 1-hour chunks to avoid holding the write semaphore
-/// for minutes. A single massive transaction blocks ALL other DB writes
-/// (vision capture, audio, etc.) causing PoolTimedOut crashes.
+/// Batches deletes into 1-hour chunks. Each batch only deletes time-bounded
+/// rows (fast). The expensive orphan cleanup (full-table NOT IN scans) runs
+/// once at the end.
async fn do_cleanup(db: &Arc<DatabaseManager>, cutoff: DateTime<Utc>) -> anyhow::Result<()> {
let batch_size = Duration::hours(1);
@@ -933,11 +933,12 @@ async fn do_cleanup(db: &Arc<DatabaseManager>, cutoff: DateTime<Utc>) -> anyhow:
};
let mut batch_start = oldest;
+ let mut any_deleted = false;
while batch_start < cutoff {
let batch_end = (batch_start + batch_size).min(cutoff);
- match db.delete_time_range(batch_start, batch_end).await {
+ match db.delete_time_range_batch(batch_start, batch_end, false).await {
Ok(result) => {
let batch_total = result.frames_deleted
+ result.ocr_deleted
@@ -945,13 +946,13 @@ async fn do_cleanup(db: &Arc<DatabaseManager>, cutoff: DateTime<Utc>) -> anyhow:
+ result.ui_events_deleted;
if batch_total > 0 {
+ any_deleted = true;
info!(
- "archive: batch deleted frames={} ocr={} audio={} accessibility={} ui_events={} \
+ "archive: batch deleted frames={} ocr={} audio={} ui_events={} \
(video_files={} snapshot_files={} audio_files={})",
result.frames_deleted,
result.ocr_deleted,
result.audio_transcriptions_deleted,
- result.accessibility_deleted,
result.ui_events_deleted,
result.video_files.len(),
result.snapshot_files.len(),
@@ -959,7 +960,7 @@ async fn do_cleanup(db: &Arc<DatabaseManager>, cutoff: DateTime<Utc>) -> anyhow:
);
}
- // Delete orphan media files from disk (only those confirmed uploaded)
+ // Delete media files from disk
for path in result
.video_files
.iter()
@@ -982,6 +983,13 @@ async fn do_cleanup(db: &Arc<DatabaseManager>, cutoff: DateTime<Utc>) -> anyhow:
tokio::time::sleep(std::time::Duration::from_millis(500)).await;
}
+ // One-time orphan cleanup after all batches
+ if any_deleted {
+ if let Err(e) = db.cleanup_orphaned_chunks().await {
+ warn!("archive: orphan chunk cleanup failed: {}", e);
+ }
+ }
+
Ok(())
}
diff --git a/crates/screenpipe-engine/src/retention.rs b/crates/screenpipe-engine/src/retention.rs
index 739000e6f..1869b294f 100644
--- a/crates/screenpipe-engine/src/retention.rs
+++ b/crates/screenpipe-engine/src/retention.rs
@@ -301,17 +301,15 @@ fn spawn_retention_loop(
}
async fn do_local_cleanup(db: &Arc<DatabaseManager>, cutoff: DateTime<Utc>) -> anyhow::Result<u64> {
- // Delete in 1-hour batches to avoid holding the write semaphore for minutes.
- // The write semaphore has a 10s timeout — a single massive transaction would
- // block ALL other DB writes (vision capture, audio, etc.) causing PoolTimedOut
- // crashes. By batching, each transaction holds the semaphore for only seconds.
+ // Delete in 1-hour batches. Each batch only deletes time-bounded rows
+ // (fast, uses indexed timestamp). The expensive orphan cleanup (full-table
+ // NOT IN scans on video_chunks/audio_chunks) runs once at the end.
let batch_size = Duration::hours(1);
let mut total: u64 = 0;
- // Find the oldest data timestamp to avoid scanning from epoch
let oldest = match db.get_oldest_timestamp().await {
Ok(Some(ts)) => ts,
- Ok(None) => return Ok(0), // empty DB
+ Ok(None) => return Ok(0),
Err(e) => {
warn!("retention: failed to get oldest timestamp: {}", e);
return Ok(0);
@@ -319,11 +317,12 @@ async fn do_local_cleanup(db: &Arc<DatabaseManager>, cutoff: DateTime<Utc>) -> a
};
let mut batch_start = oldest;
+ let mut any_deleted = false;
while batch_start < cutoff {
let batch_end = (batch_start + batch_size).min(cutoff);
- match db.delete_time_range_local(batch_start, batch_end).await {
+ match db.delete_time_range_batch(batch_start, batch_end, true).await {
Ok(result) => {
let batch_total = result.frames_deleted
+ result.ocr_deleted
@@ -331,6 +330,7 @@ async fn do_local_cleanup(db: &Arc<DatabaseManager>, cutoff: DateTime<Utc>) -> a
+ result.ui_events_deleted;
if batch_total > 0 {
+ any_deleted = true;
info!(
"retention: batch deleted frames={} ocr={} audio={} ui_events={} \
(video_files={} snapshot_files={} audio_files={})",
@@ -346,7 +346,6 @@ async fn do_local_cleanup(db: &Arc<DatabaseManager>, cutoff: DateTime<Utc>) -> a
total += batch_total;
- // Delete media files from disk AFTER successful DB commit
for path in result
.video_files
.iter()
@@ -360,7 +359,6 @@ async fn do_local_cleanup(db: &Arc<DatabaseManager>, cutoff: DateTime<Utc>) -> a
}
Err(e) => {
warn!("retention: batch delete failed for range {} to {}: {}", batch_start, batch_end, e);
- // Continue with next batch instead of aborting entire cleanup
}
}
@@ -370,5 +368,12 @@ async fn do_local_cleanup(db: &Arc<DatabaseManager>, cutoff: DateTime<Utc>) -> a
tokio::time::sleep(std::time::Duration::from_millis(500)).await;
}
+ // One-time orphan cleanup after all batches
+ if any_deleted {
+ if let Err(e) = db.cleanup_orphaned_chunks().await {
+ warn!("retention: orphan chunk cleanup failed: {}", e);
+ }
+ }
+
Ok(total)
}