Manually delete CloudKit records when the app is deleted

I am thinking to use iCloud kit in my app. I made some tests and it looks great. Straightforward and easy to implement. I will use private databases in order to not exceed the public database quota.


However, if the user deletes my app I need to offer a clean way to delete all the records associated. Something like when we use Ubiquitous containers (Settings -> iCloud -> Storage -> Manage storage -> Documents & Data).


Do you guys know if it is possible to do that CloudKit? Otherwise I need rethink its usage because this is an important detail.


Thanks

Accepted Reply

No, it's not possible to do cleanup like this upon a delete of an app. As MendelK mentions, no code is run on app deltion. But beyond that, how would you know whether the user has a another device or not? What if they do an erase of their device? The app is no longer on their device, but they didn't explicitly delete it. What if they get a new device, set that one up, and now want to delete the app off the second device?

Replies

Currently, I do not think this is possible because you cannot execute any code when your app is removed.

No, it's not possible to do cleanup like this upon a delete of an app. As MendelK mentions, no code is run on app deltion. But beyond that, how would you know whether the user has a another device or not? What if they do an erase of their device? The app is no longer on their device, but they didn't explicitly delete it. What if they get a new device, set that one up, and now want to delete the app off the second device?

Why do you care? You don't get charged for using the private database - the user does. I am sure the user has some way of deleting the files if Apple sends them a bill for storage and they are no longer using the app.


A different question relates to the public database. If the user stores lots of stuff in the public database on your nickel and then deletes the app you keep getting charged. An app can update a field in every record that it owns each time the user opens the app. It can also do a query for all records in the public database that have not been so updated in the past, say 30 days and delete them. A bit harsh but effective. I suspect you can also send a notification to the app and, if it is on the device, cause it to update the record in the background before this procedure.

If you go to Settings -> iCloud -> Storage -> Manage storage -> Documents & Data and delete your app there, private database in CloudKit will be automatically deleted as well.


Just one small remark though: it might apply to custom zones only. My app uses a custom zone in the private database and deleting app records from iCloud settings purges this zone every time.

Deleting Settings -> iCloud -> Storage -> Manage storage -> Documents & Data has no effect on CloudKit databases. And that option only appears if iCloud Documents is enabled in the capabilities, which is probably unlikely when using CloudKit.


There doesn't appear to be a way for the user to delete their private CloudKit database, despite there being a CKError code that suggests there is.


    CKErrorUserDeletedZone                = 28, / The user deleted this zone through the settings UI. Your client should either remove its local data or prompt the user before attempting to re-upload any data to this zone. */

(iOS 11 SDK)

cloud kit app