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.
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.
iOS9+, Swift 2
Delete all objects in all entities
Thanks for the post. I followed it and it worked for me. But I had another issue that was not mentioned in any of the replies. So I am not sure if it was just me.
Anyway, thought I would post here the problem and my way that solved it.
I had a few records in the database, I wanted to purge everything clean before write new data to the db, so I did everything including
and then used
managedObjectContext
to access the database (supposed to be empty by now), somehow the data was still there. After a while of troubleshooting, I found that I need to resetmanagedObjectContext
,managedObject
,managedObjectModel
andpersistentStoreCoordinator
, before I usemanagedObjectContext
to access the dabase. Now I have a clean database to write to.The accepted answer is correct with removing URL by NSFileManager is correct, but as stated in iOS 5+ edit, the persistent store is not represented only by one file. For SQLite store it's *.sqlite, *.sqlite-shm and *.sqlite-wal ... fortunately since iOS 7+ we can use method
[NSPersistentStoreCoordinator +removeUbiquitousContentAndPersistentStoreAtURL:options:error:]
to take care of removal, so the code should be something like this:
Use this
If you want to delete all objects and do not want to delete the backing files, you can use following methods:
Beware that it may be very slow (depends on how many objects are in your object graph).
Delete sqlite from your fileURLPath and then build.