From 4a40cef2a641399a80cb8dfc67783f4df4295e9a Mon Sep 17 00:00:00 2001 From: Brent Simmons Date: Wed, 25 Mar 2026 21:24:49 -0700 Subject: [PATCH] =?UTF-8?q?Add=20deleteStaleRecords=20parameter=20?= =?UTF-8?q?=E2=80=94=20delete=20only=20if=20true.=20Right=20never=20it?= =?UTF-8?q?=E2=80=99s=20never=20true.=20We=E2=80=99ll=20get=20back=20to=20?= =?UTF-8?q?this.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Account/CloudKit/CloudKitAccountDelegate.swift | 4 ++-- .../Sources/Account/CloudKit/CloudKitArticlesZone.swift | 8 ++++---- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/Modules/Account/Sources/Account/CloudKit/CloudKitAccountDelegate.swift b/Modules/Account/Sources/Account/CloudKit/CloudKitAccountDelegate.swift index 713aa7b12..ae5430d30 100644 --- a/Modules/Account/Sources/Account/CloudKit/CloudKitAccountDelegate.swift +++ b/Modules/Account/Sources/Account/CloudKit/CloudKitAccountDelegate.swift @@ -533,7 +533,7 @@ enum CloudKitAccountDelegateError: LocalizedError, Sendable { } let syncUnreadContent = Self.syncArticleContentForUnreadArticles do { - try await articlesZone.cleanUpRecordsUsingCache(account: account, syncUnreadContent: syncUnreadContent, dryRun: dryRun, progress: progress) + try await articlesZone.cleanUpRecordsUsingCache(account: account, syncUnreadContent: syncUnreadContent, dryRun: dryRun, deleteStaleRecords: false, progress: progress) } catch { Self.logger.error("CloudKitAccountDelegate: cleanUpCloudKit error: \(error)") postSyncError(error, account: account, operation: "Cleaning up iCloud records") @@ -909,7 +909,7 @@ private extension CloudKitAccountDelegate { Self.logger.info("CloudKitAccountDelegate: running weekly record cleanup") do { let syncUnreadContent = Self.syncArticleContentForUnreadArticles - let deleted = try await articlesZone.cleanUpRecords(account: account, syncUnreadContent: syncUnreadContent, dryRun: false) + let deleted = try await articlesZone.cleanUpRecords(account: account, syncUnreadContent: syncUnreadContent, dryRun: false, deleteStaleRecords: false) Self.logger.info("CloudKitAccountDelegate: weekly cleanup deleted \(deleted, privacy: .public) records") } catch { Self.logger.error("CloudKitAccountDelegate: weekly cleanup error: \(error.localizedDescription, privacy: .public)") diff --git a/Modules/Account/Sources/Account/CloudKit/CloudKitArticlesZone.swift b/Modules/Account/Sources/Account/CloudKit/CloudKitArticlesZone.swift index db3294c25..70e577f25 100644 --- a/Modules/Account/Sources/Account/CloudKit/CloudKitArticlesZone.swift +++ b/Modules/Account/Sources/Account/CloudKit/CloudKitArticlesZone.swift @@ -213,7 +213,7 @@ final class CloudKitArticlesZone: CloudKitZone { /// Periodic cleanup path. Scans content records incrementally, stopping when /// the limit is hit. No `ScanCache` awareness — the periodic cleanup runs on /// launch before the user would open the stats window. - func cleanUpRecords(account: Account, syncUnreadContent: Bool, dryRun: Bool, limit: Int = CloudKitArticlesZone.cleanUpLimit) async throws -> Int { + func cleanUpRecords(account: Account, syncUnreadContent: Bool, dryRun: Bool, deleteStaleRecords: Bool, limit: Int = CloudKitArticlesZone.cleanUpLimit) async throws -> Int { guard let database else { return 0 } @@ -224,7 +224,7 @@ final class CloudKitArticlesZone: CloudKitZone { var deleteRecordIDs = try await scanContentRecordsIncrementally(database: database, statusByRecordID: statusByRecordID, syncUnreadContent: syncUnreadContent, limit: limit) Self.logger.info("CloudKitArticlesZone: cleanUpRecords: \(deleteRecordIDs.count, privacy: .public) content records to delete") - if deleteRecordIDs.count < limit { + if deleteStaleRecords && deleteRecordIDs.count < limit { let statusIDs = staleStatusRecordIDsToDelete(from: statusByRecordID, limit: limit - deleteRecordIDs.count) Self.logger.info("CloudKitArticlesZone: cleanUpRecords: \(statusIDs.count, privacy: .public) status records to delete") deleteRecordIDs.append(contentsOf: statusIDs) @@ -236,7 +236,7 @@ final class CloudKitArticlesZone: CloudKitZone { /// Cache-aware cleanup with per-category progress reporting. /// Deletes records in separate batches by category so the caller /// can update progress after each category completes. - func cleanUpRecordsUsingCache(account: Account, syncUnreadContent: Bool, dryRun: Bool, progress: @escaping @MainActor @Sendable (CloudKitCleanUpProgress) -> Void) async throws { + func cleanUpRecordsUsingCache(account: Account, syncUnreadContent: Bool, dryRun: Bool, deleteStaleRecords: Bool, progress: @escaping @MainActor @Sendable (CloudKitCleanUpProgress) -> Void) async throws { guard database != nil else { return } @@ -285,7 +285,7 @@ final class CloudKitArticlesZone: CloudKitZone { } // Delete stale status records - if !staleStatusIDs.isEmpty { + if deleteStaleRecords && !staleStatusIDs.isEmpty { reportProgress(.deletingStaleStatus) Self.logger.info("CloudKitArticlesZone: cleanUpRecordsUsingCache(progress:): \(dryRun ? "DRY RUN" : "deleting", privacy: .public) \(staleStatusIDs.count, privacy: .public) stale status records") for batch in staleStatusIDs.chunked(into: Self.cleanUpLimit) {