diff --git a/src/configure.rs b/src/configure.rs index e622743b1..40eee7d1a 100644 --- a/src/configure.rs +++ b/src/configure.rs @@ -243,11 +243,6 @@ impl Context { Ok((id, add_timestamp)) }, )?; - transaction.execute("DELETE FROM imap WHERE transport_id=?", (transport_id,))?; - transaction.execute( - "DELETE FROM imap_sync WHERE transport_id=?", - (transport_id,), - )?; // Removal timestamp should not be lower than addition timestamp // to be accepted by other devices when synced. diff --git a/src/sql.rs b/src/sql.rs index 912a704a3..609358c69 100644 --- a/src/sql.rs +++ b/src/sql.rs @@ -883,6 +883,24 @@ pub async fn housekeeping(context: &Context) -> Result<()> { .log_err(context) .ok(); + // Cleanup `imap` and `imap_sync` entries for deleted transports. + // + // Transports may be deleted directly or via sync messages, + // so it is easier to cleanup orphaned entries in a single place. + context + .sql + .execute( + "DELETE FROM imap WHERE transport_id NOT IN (SELECT transports.id FROM transports)", + (), + ) + .await + .log_err(context) + .ok(); + context.sql.execute( + "DELETE FROM imap_sync WHERE transport_id NOT IN (SELECT transports.id FROM transports)", + (), + ).await.log_err(context).ok(); + // Delete POI locations // which don't have corresponding message. delete_orphaned_poi_locations(context)