1

How I could completely clear out EVERYTHING in a Core Data model i.e remove all objects for all entities?

I will be using the code to clear out the saved history in a the model.

GEOCHET
  • 21,119
  • 15
  • 74
  • 98
max_
  • 24,076
  • 39
  • 122
  • 211

3 Answers3

10

Here is another way to do...

NSManagedObjectContext *context = [appDelegate managedObjectContext];
            NSFetchRequest * allMovies = [[NSFetchRequest alloc] init];
            [allMovies setEntity:[NSEntityDescription entityForName:@"Movies" inManagedObjectContext:context]];
            [allMovies setIncludesPropertyValues:NO]; //only fetch the managedObjectID

            NSError * error = nil;
            NSArray * movies = [context executeFetchRequest:allMovies error:&error];
            //error handling goes here
            for (NSManagedObject * movie in movies) {
                [context deleteObject:movie];
            }
            NSError *saveError = nil;
            [context save:&saveError];

It will clear all the objects.

Mak083
  • 1,160
  • 1
  • 13
  • 33
2

Delete the .sqlite DB Core Data creates (it's probably stored on your /Documents directory) and get your app to re-create it.

Jamie Chapman
  • 4,229
  • 5
  • 29
  • 47
  • You specify the URL for the persistent store when you create the persistent store, so you should already know where the file is. You can use NSFileManager's -removeItemAtPath:error: method to delete the file. – Caleb Apr 02 '11 at 02:25
0

Here is a good answer to this question.

Delete/Reset all entries in Core Data?

Then just make sure to re-create the store.

Community
  • 1
  • 1
Mike A
  • 2,501
  • 2
  • 23
  • 30