Skip to content

Child records deleted locally when their parent's save failed with quotaExceeded #547

Open

Description

@jalalawqati

Description

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.
  • This issue hasn't been addressed in an existing GitHub issue or discussion.

Expected behavior

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

Reproducing project

https://github.com/jalalawqati/ReferenceViolationReproduce

SQLiteData version information

1.12.0

Sharing version information

2.10.1

GRDB version information

7.11.1

Destination operating system

iOS 26.6

Xcode version information

Xcode 26.6

Swift Compiler version information

swift-driver version: 1.148.6 Apple Swift version 6.3.3 (swiftlang-6.3.3.1.1 clang-2100.1.1.101)
Target: arm64-apple-macosx26.0

Activity

  1. added
    bug
    on Sep 15, 2026
  2. sdvz commented on Sep 27, 2026

    @sdvz

    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.

  3. added a commit that references this issue on Sep 29, 2026
    17d8843
  4. Belvedere82 commented on Oct 6, 2026

    @Belvedere82

    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:

    1. 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.

    2. 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.

  5. Andrei-Kondrykau commented on Oct 12, 2026

    @Andrei-Kondrykau

    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.

    Two thoughts that might help with #548:

    1. 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.
    2. 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.

  6. mbrandonw commented on Nov 5, 2026

    @mbrandonw
    Member

    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 :)

  7. sdvz commented on Nov 12, 2026

    @sdvz

    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 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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    bug

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions