Delete/Reset all entries in Core Data? Delete/Reset all entries in Core Data? ios ios

Delete/Reset all entries in Core Data?


You can still delete the file programmatically, using the NSFileManager:removeItemAtPath:: method.

NSPersistentStore *store = ...;NSError *error;NSURL *storeURL = store.URL;NSPersistentStoreCoordinator *storeCoordinator = ...;[storeCoordinator removePersistentStore:store error:&error];[[NSFileManager defaultManager] removeItemAtPath:storeURL.path error:&error];

Then, just add the persistent store back to ensure it is recreated properly.

The programmatic way for iterating through each entity is both slower and prone to error. The use for doing it that way is if you want to delete some entities and not others. However you still need to make sure you retain referential integrity or you won't be able to persist your changes.

Just removing the store and recreating it is both fast and safe, and can certainly be done programatically at runtime.

Update for iOS5+

With the introduction of external binary storage (allowsExternalBinaryDataStorage or Store in External Record File) in iOS 5 and OS X 10.7, simply deleting files pointed by storeURLs is not enough. You'll leave the external record files behind. Since the naming scheme of these external record files is not public, I don't have a universal solution yet. – an0 May 8 '12 at 23:00


You can delete the SQLite file - but I choose to do it by purging the tables individually with a functions:

- (void) deleteAllObjects: (NSString *) entityDescription  {    NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];    NSEntityDescription *entity = [NSEntityDescription entityForName:entityDescription inManagedObjectContext:_managedObjectContext];    [fetchRequest setEntity:entity];    NSError *error;    NSArray *items = [_managedObjectContext executeFetchRequest:fetchRequest error:&error];    [fetchRequest release];    for (NSManagedObject *managedObject in items) {        [_managedObjectContext deleteObject:managedObject];        DLog(@"%@ object deleted",entityDescription);    }    if (![_managedObjectContext save:&error]) {        DLog(@"Error deleting %@ - error:%@",entityDescription,error);    }}

The reason I chose to do it table by table is that it makes me confirm as I am doing the programming that deleting the contents of the table is sensible and there is not data that I would rather keep.

Doing it this will is much slower than just deleting the file and I will change to a file delete if I this method takes too long.


Updated Solution for iOS 10+

Use NSBatchDeleteRequest to delete all objects in the entity without having to load them into memory or iterate through them.

// create the delete request for the specified entitylet fetchRequest: NSFetchRequest<NSFetchRequestResult> = MyEntity.fetchRequest()let deleteRequest = NSBatchDeleteRequest(fetchRequest: fetchRequest)// get reference to the persistent containerlet persistentContainer = (UIApplication.shared.delegate as! AppDelegate).persistentContainer// perform the deletedo {    try persistentContainer.viewContext.execute(deleteRequest)} catch let error as NSError {    print(error)}

This code has been updated for iOS 10 and Swift 3. If you need to support iOS 9, see this question.

Sources: