When changing the data model, you can simply check which model has the database file. If it is not new, delete the file specified in StoreCoordinator using NSFileManager and run StoreCoordinater and NSManagedContext again to create a new one.
Something like this (untested code):
var error: NSError var applicationDocumentsDirectory: NSURL = NSFileManager.defaultManager().URLsForDirectory(NSDocumentDirectory, inDomains:NSUserDomainMask).lastObject let storeURL: NSURL = applicationDocumentsDirectory.URLByAppendingPathComponent("Database.sqlite") NSFileManager.defaultManager().removeItemAtPath(storeURL.path, error)
If the model has not changed, you need to save the update information anywhere. A text file in the database itself or in UserDefaults. You just need a flag to check if the database has been updated / cleared.
Then you can also delete the database as described above, or get all the objects and delete them.
source share