Declutter a Complete CloudKit Container: Step‑by‑Step Guide
In this article
To declutter a complete CloudKit container you must delete unused records, prune custom zones, and rebuild indexes either via the CloudKit Dashboard or programmatically. Most people think removing a single record type is enough, but leftover zones and stale subscriptions also consume quota.
Identify Unused Record Types and Zones
Start by listing every record type that your app currently stores. Open the CloudKit Dashboard, select your container, and view the Data > Record Types table. Look for types with a low creation rate or a high delete‑to‑create ratio over the past month. A quick rule of thumb is to flag any type that averages fewer than 10 new records per day and has more than 70% of its records older than 90 days.
Next, examine custom zones. Zones appear under Data > Zones. If a zone was created for a feature that is no longer in use, it will show a low record count and no recent modifications. Record zones that contain fewer than 500 records and have not been touched in the last 60 days are prime candidates for removal.
Delete Unnecessary Records Safely
When you have identified candidates, delete them in batches to avoid hitting the operation limit. Use the CKModifyRecordsOperation class with a maximum batch size of 200 records. The operation returns a per‑record error array, so you can retry any failures without losing progress.
- Query the target record type with a predicate that selects only stale records, for example
CKQuery(recordType: "Note", predicate: NSPredicate(format: "modificationDate < %@", Date().addingTimeInterval(-90*24*60*60))). - Collect the record IDs returned by the query.
- Split the IDs into groups of 200.
- For each group, create a CKModifyRecordsOperation with
recordsToSave = nilandrecordIDsToDelete = group. - Set
operationConfigurationto.defaultand add a completion block that logs any errors. - Add the operation to the private database queue.
After the batch runs, verify the remaining count in the Dashboard. If the count drops from, say, 10,000 to 2,500, you have removed 75% of the clutter.
Prune Custom Zones and Reset Subscriptions
Custom zones that are no longer needed should be deleted to free storage and reduce sync traffic. Use CKModifyRecordZonesOperation with recordZoneIDsToDelete set to the IDs of the zones you identified.
Subscriptions tied to those zones also linger unless removed. List all subscriptions via CKFetchSubscriptionsOperation, filter those whose zoneID matches a deleted zone, and delete them with CKModifySubscriptionsOperation. This prevents the server from sending push notifications for a zone that no longer exists.
Rebuild Indexes and Optimize Queries
CloudKit automatically maintains indexes for fields you mark as searchable. However, after massive deletions, index fragmentation can slow queries. The simplest way to refresh indexes is to perform a “touch” update on a small sample of remaining records.
For each record type you kept, fetch 100 random records, modify a non‑essential field (e.g., add a temporary tag), and save them back. This forces the server to rewrite the index entries. After the operation, remove the temporary tag.
When you design future queries, prefer predicates that use indexed fields and limit the result set with CKQueryOperation.resultsLimit. A practical guideline is to keep the limit under 500 for most UI lists; larger limits should be paginated.
Verify Cleanup and Monitor Ongoing Growth
Once the deletion steps are complete, run a final audit in the Dashboard. Check the storage usage chart; a well‑decluttered container typically shows a drop of 30‑50% compared to the pre‑cleanup baseline.
Set up a monthly monitoring script that runs a lightweight query on each record type and logs the count. If any type exceeds the rule‑of‑thumb threshold of 5,000 active records, schedule a follow‑up prune.
Worked Example: Cleaning a “Note” Record Type
Assume your app stores 12,000 notes, but only 3,200 have been edited in the last 90 days. You decide to keep the recent notes and delete the rest.
- Run a query with
modificationDate < now‑90daysto fetch stale note IDs. - The query returns 8,800 IDs.
- Split the IDs into 44 batches of 200.
- Execute a CKModifyRecordsOperation for each batch, monitoring the completion handler for errors.
- After all batches finish, verify the Dashboard shows roughly 3,200 remaining notes.
- Fetch 100 of the remaining notes, add a temporary field
"indexRefresh": true, save them, then remove the field.
The result is a lean “Note” type, refreshed indexes, and a clear baseline for future growth.
Rule of Thumb for Ongoing Maintenance
- Delete records older than 90 days unless they are marked as “pinned”.
- Prune custom zones that hold fewer than 500 records and have no activity in 60 days.
- Refresh indexes after any bulk deletion larger than 1,000 records.
- Run a storage‑usage check at least once a month.
Following these habits keeps your CloudKit container responsive and prevents unexpected quota warnings.