views:

6843

answers:

5

Do you know of any way to delete all of the entries stored in Core Data? My schema should stay the same; I just want to reset it to blank.


Edit

I'm looking to do this programmatically so that a user can essentially hit a reset button.

A: 

Delete the persistent store file and setup a new persistent store coordinator?

Hunter
and do a build>clean.
John Ballinger
Doing a clean will not remove the persistent store files, thankfully. That would be a recipe for disaster if true.
Hunter
+10  A: 

You can delete the sqllite 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.

Grouchal
Great solution. Thanks.What's DLog()?
Michael Grinich
Ah yes - sorry that is a special function I use that only does an NSLog when the build is a DEBUG build - just replace with NSLog.
Grouchal
You can see an implementation of DLog here: http://www.cimgf.com/2009/01/24/dropping-nslog-in-release-builds/
Matt Long
is there any way to delete data of a particular attribute?
Warrior
This works nicely for me. But to make it go faster, is there a way to delete all the objects of a certain entity with one command? Like in SQL you could do something like, DROP TABLE entity_name. I don't want to delete the whole SQL file because I only want to delete all objects of a specific entity, not other entities.
MattDiPasquale
A: 

Delete sqlite from your fileURLPath and then build.

Jai
+18  A: 

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.

groundhog
+1  A: 

I've written a clearStores method that goes through every store and delete it both from the coordinator and the filesystem (error handling left aside):

NSArray *stores = [persistentStoreCoordinator persistentStores];

for(NSPersistentStore *store in stores) {
    [persistentStoreCoordinator removePersistentStore:store error:nil];
    [[NSFileManager defaultManager] removeItemAtPath:store.URL.path error:nil];
}

[persistentStoreCoordinator release], persistentStoreCoordinator = nil;

This method is inside a coreDataHelper class that takes care of (among other things) creating the persistentStore when it's nil.

Sam V