How to reset your iCloud Ubiquitous Container

In this screencast I’ll show you how to reset your iCloud Ubiquity Container from an iOS device. This will clear up anything you’ve saved in iCloud Documents and iCloud Core Data and also simulates “first installation”.

Sometimes when you test the same app over and over the container can get corrupted, error messages start flooding in and ultimately your app doesn’t behave as expected – making you doubt your sanity, alienating your friends and family and giving you more grey hair then you need.

If the above still doesn’t work, turn your devices off and on again, restart Xcode and reboot your Mac – a useful tip often forgotten with pesky bugs that don’t make sense (thanks to Eric for reminding me of this).

In a nutshell:

  • remove your app from all devices – this is important because running apps, or existing apps launched will assume there’s data in iCloud and they will try to communicate with your (defunct) container
  • remote all data from the container as described in the video (on iOS, head over to Settings – iCloud – Manage Storage – find your app and select it, hit edit and delete all data)
  • now re-deploy the app to each device via Xcode

For Core Data: the first app that’s launched checks to see if data exists in iCloud, and if not will create the iCloud store. Subsequent apps will see this has been done and read the changes from those mysterious log files.

Removing the container in code

You can also call a method on the NSPersistentStoreCoordinator class to remove the entire ubiquity container. Here’s how to do that:

- (void)deleteCloudContainer {
    
    // remove this store's iCloud Content
    NSPersistentStore *currentStore = self.managedObjectContext.persistentStoreCoordinator.persistentStores.lastObject;
    NSError *error = nil;
    [self.managedObjectContext reset];
    [self.managedObjectContext.persistentStoreCoordinator removePersistentStore:currentStore error:nil];
    
    if (![NSPersistentStoreCoordinator removeUbiquitousContentAndPersistentStoreAtURL:currentStore.URL options:currentStore.options error:&error]) {
        NSLog(@"Could not remove iCloud Container. Reason: %@", error.localizedFailureReason);
    }
}

This does pretty much the same thing as the manual method, although it may take a moment. Works in iOS 7, but I’ve not had much luck in iOS 8 Beta 5 (in fact, I’ve not had much luck with anything in iOS 8 Beta 5).





4 thoughts on “How to reset your iCloud Ubiquitous Container

  1. Hi, I’m working on a core data app and iCloud. I’m trying to sync both devices and also to have the existing data relaunched after iCloud has been enabled.

    What advices can you give me. I’m having a lot of trouble with the sql file. Should it be in the bundle or should I copy it from inside?

    Thanks

    1. Hi Chrystian,

      iCloud with Core Data is the holy grail – and it’s complex. I won’t be able to answer this with a comment. The best advice I can give you is to take a look at this article which explains how to setup iCloud Core Data:

      I’ve recorded a screencast series on iCloud, the last part of which covers iCloud with Core Data. It’s been helpful to many people, check it out here:

  2. Not working here with IOS 8 beta 5 – I can do like in the video, but in the end the container is still there. If i trash the containers on my mac at ~/Library/Mobile Documents, the second after they ‘re back.
    Oh well…

    1. Hi Fred, that may be a bug indeed – not a lot is working in the current Beta of iOS 8 and Xcode 6 – slightly shocking considering it’s Beta 5 already. At least Apple are happy, never mind us users ey…

      You can remove the iCloud Container in code too – I’ve added how to do it above. Works fine in iOS 7, but not quite yet in iOS 8 Beta 5.

Leave a Reply