You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Child records deleted locally when their parent's save failed with quotaExceeded #547
When a user's iCloud storage is full, saving parent records in CloudKit fails with quotaExceeded. SyncEngine ignores this, and that record is never sent again. The record is kept locally on device however.
Later, when a child that references that parent record is sent later, CloudKit rejects it this time with referenceViolation, rather than quotaExceeded. handleSentRecordZoneChanges treats all referenceViolation errors as the parent being deleted from another device, and with the child's ON DELETE CASCADE, that child is deleted from the local database.
This is currently causing issues for our users and we didn't find a reliably fix on our side to get around it.
Checklist
I wrote this in my own words, and aimed to be as succinct as possible, even if I used AI to help research its content.
I have determined whether this bug is also reproducible in a vanilla SwiftUI project.
I have determined whether this bug is also reproducible in a vanilla GRDB project.
If possible, I've reproduced the issue using the main branch of this package.
When iCloud storage is full, records are still saved locally, and uploaded again once storage is available
Actual behavior
Children of a parent that was never uploaded to CloudKit are deleted because of referenceViolation when they are sent later to CloudKit. We observed this behavior in our app and we were able to consistently reproduce it with an iCloud account that had no free storage left. Turning off iCloud made child record persist locally
We're hitting this in our app as well, on SQLiteData 1.6.0 (the handler is unchanged from 1.6.0 through 1.12.0.)
Also, it isn't specific to quotaExceeded. It looks like any parent whose save gets dropped produces the same outcome. I reproduced the child deletion against 1.12.0 by removing the parent's pending save before it was ever sent, with no quota involved - so the lastKnownServerRecord disambiguation in #548 looks worth having independently of the quota retry.
We hit the same on 1.12.0 (iOS/macOS 26), while measuring CloudKit storage for a finance app. Two observations that might help with #548:
Dropped records look synced.nextRecordZoneChangeBatch calls refreshLastKnownServerRecord(record) with the locally built record before sending. For a record that never reached the server, hasLastKnownServerRecord is therefore 1 although nothing was confirmed. In a 50,000-row upload with nearly full iCloud storage, exactly one batch of 250 records was rejected with quotaExceeded, and our upload counter (based on hasLastKnownServerRecord) still counted it as uploaded. Keep children of never-uploaded parents and retry quotaExceeded saves #548's hasBeenSavedToServer check via recordChangeTag avoids this for the reference-violation path; apps that show "pending uploads" may want the same signal publicly.
App-side workaround until Keep children of never-uploaded parents and retry quotaExceeded saves #548 lands: after sendChanges() we treat a record as confirmed only if the archived server record has a modificationDate and no newer local userModificationTime, and re-enqueue the rest with a no-op UPDATE on a non-primary-key column. We re-enqueue parents first and hold back children of never-saved parents, otherwise the re-sent child hits exactly the referenceViolation → local delete described here.
Thanks for #548. We'd be glad to see it merged, since without it a user who sets up the app with full iCloud storage can lose newly entered child rows.
We hit the same referenceViolation → local delete path on 1.6.x, and the handler is unchanged in 1.12.1 (Sources/SQLiteData/CloudKit/SyncEngine.swift:1705-1724). In our case storage quota wasn't involved. A child was sent while its parent wasn't on the server yet: during a bulk re-upload after a zone reset, and on a device's first sync. The .cascade branch deleted the child row. _isSynchronizingChanges is false there, so the delete is also recorded as a user delete, and a deleteRecord for the child gets queued to the server.
Re-check with the server instead of trusting local state. As a local workaround, in the .cascade case we insert the record into UnsyncedRecordID and drop the pending save instead of deleting the row. On the next cycle, the existing records(for:) refetch settles it. unknownItem keeps the local row, so the child is re-sent after its parent. success applies the server copy. If the parent really was deleted, its remote delete arrives and the local ON DELETE CASCADE removes the child the normal way. This doesn't depend on whether the parent's local metadata looks "saved". That matters after .encryptedDataReset: uploadRecords(in:) re-enqueues every record but keeps each record's old lastKnownServerRecord, so a hasBeenSavedToServer check may still treat the parent as saved.
Multi-FK tables can probably be left alone. Tables with more than one foreign key never get a CKRecord.parent (parentForeignKey is nil unless there's exactly one FK), so they shouldn't hit this path.
I'm happy to share the patch or add a test to #548 if that's useful.
Created #582 to reproduce this. To be clear up front, it doesn't fix anything. It just pins the bug down.
Seems like the reason nothing caught it before is MockCloudDatabase treated an over-quota batch as all-or-nothing. It fails every record with quotaExceeded. So it could never hand the sync engine what the real server does: quotaExceeded on the parent and referenceViolation on the child, same batch. The first commit makes the mock reject records individually instead.
The second commit adds a test. Send a list and its reminder in one batch, the list comes back quotaExceeded, the reminder comes back referenceViolation, and the handler fires ON DELETE CASCADE on a row the server never had - the data loss.
@Andrei-Kondrykau your case had no quota involved at all, and the test doesn't cover that path. The common thread looks like the handler never asking whether the parent actually made it to the server. It just acts on the error code.
Activity
sdvz commentedon Sep 27, 2026
We're hitting this in our app as well, on SQLiteData 1.6.0 (the handler is unchanged from 1.6.0 through 1.12.0.)
Also, it isn't specific to quotaExceeded. It looks like any parent whose save gets dropped produces the same outcome. I reproduced the child deletion against 1.12.0 by removing the parent's pending save before it was ever sent, with no quota involved - so the lastKnownServerRecord disambiguation in #548 looks worth having independently of the quota retry.
Keep children of never-uploaded parents (backport of upstream pointfr…
Belvedere82 commentedon Oct 6, 2026
We hit the same on 1.12.0 (iOS/macOS 26), while measuring CloudKit storage for a finance app. Two observations that might help with #548:
Dropped records look synced.
nextRecordZoneChangeBatchcallsrefreshLastKnownServerRecord(record)with the locally built record before sending. For a record that never reached the server,hasLastKnownServerRecordis therefore 1 although nothing was confirmed. In a 50,000-row upload with nearly full iCloud storage, exactly one batch of 250 records was rejected withquotaExceeded, and our upload counter (based onhasLastKnownServerRecord) still counted it as uploaded. Keep children of never-uploaded parents and retryquotaExceededsaves #548'shasBeenSavedToServercheck viarecordChangeTagavoids this for the reference-violation path; apps that show "pending uploads" may want the same signal publicly.App-side workaround until Keep children of never-uploaded parents and retry
quotaExceededsaves #548 lands: aftersendChanges()we treat a record as confirmed only if the archived server record has amodificationDateand no newer localuserModificationTime, and re-enqueue the rest with a no-opUPDATEon a non-primary-key column. We re-enqueue parents first and hold back children of never-saved parents, otherwise the re-sent child hits exactly thereferenceViolation→ local delete described here.Thanks for #548. We'd be glad to see it merged, since without it a user who sets up the app with full iCloud storage can lose newly entered child rows.
Andrei-Kondrykau commentedon Oct 12, 2026
We hit the same
referenceViolation→ local delete path on 1.6.x, and the handler is unchanged in 1.12.1 (Sources/SQLiteData/CloudKit/SyncEngine.swift:1705-1724). In our case storage quota wasn't involved. A child was sent while its parent wasn't on the server yet: during a bulk re-upload after a zone reset, and on a device's first sync. The.cascadebranch deleted the child row._isSynchronizingChangesisfalsethere, so the delete is also recorded as a user delete, and adeleteRecordfor the child gets queued to the server.Two thoughts that might help with #548:
.cascadecase we insert the record intoUnsyncedRecordIDand drop the pending save instead of deleting the row. On the next cycle, the existingrecords(for:)refetch settles it.unknownItemkeeps the local row, so the child is re-sent after its parent.successapplies the server copy. If the parent really was deleted, its remote delete arrives and the localON DELETE CASCADEremoves the child the normal way. This doesn't depend on whether the parent's local metadata looks "saved". That matters after.encryptedDataReset:uploadRecords(in:)re-enqueues every record but keeps each record's oldlastKnownServerRecord, so ahasBeenSavedToServercheck may still treat the parent as saved.CKRecord.parent(parentForeignKeyisnilunless there's exactly one FK), so they shouldn't hit this path.I'm happy to share the patch or add a test to #548 if that's useful.
Find deletes that never reached iCloud (Slice 5) (#364)
mbrandonw commentedon Nov 5, 2026
Hi @jalalawqati, @sdvz, @Belvedere82 and @Andrei-Kondrykau: we have merged #335 which should fix this problem. If y'all can give it a test and let us know how it works we would be much obliged :)
Add a failing test for a child deleted after its parent fails quota
Add a failing test for a child deleted after its parent fails quota
Add a failing test for a child deleted after its parent fails quota
sdvz commentedon Nov 12, 2026
Hey folks.
Created #582 to reproduce this. To be clear up front, it doesn't fix anything. It just pins the bug down.
Seems like the reason nothing caught it before is
MockCloudDatabasetreated an over-quota batch as all-or-nothing. It fails every record withquotaExceeded. So it could never hand the sync engine what the real server does:quotaExceededon the parent andreferenceViolationon the child, same batch. The first commit makes the mock reject records individually instead.The second commit adds a test. Send a list and its reminder in one batch, the list comes back
quotaExceeded, the reminder comes backreferenceViolation, and the handler firesON DELETE CASCADEon a row the server never had - the data loss.@Andrei-Kondrykau your case had no quota involved at all, and the test doesn't cover that path. The common thread looks like the handler never asking whether the parent actually made it to the server. It just acts on the error code.
SQLiteData 1.13.0 rebase: keep only the #547 patch (#526)