I am a little confused as to how to delete all core data in swift. I have created a button with an IBAction
linked. On the click of the button I have the follow
In Swift 3.0
func deleteAllRecords() {
//delete all data
let context = appDelegate.persistentContainer.viewContext
let deleteFetch = NSFetchRequest<NSFetchRequestResult>(entityName: "YourClassName")
let deleteRequest = NSBatchDeleteRequest(fetchRequest: deleteFetch)
do {
try context.execute(deleteRequest)
try context.save()
} catch {
print ("There was an error")
}
}
For Swift 3.0
func DeleteAllData(){
let appDelegate = UIApplication.shared.delegate as! AppDelegate
let managedContext = appDelegate.persistentContainer.viewContext
let DelAllReqVar = NSBatchDeleteRequest(fetchRequest: NSFetchRequest<NSFetchRequestResult>(entityName: "Entity"))
do {
try managedContext.execute(DelAllReqVar)
}
catch {
print(error)
}
}
Similar to above but with AppDelegte call taken out and UIView variable used
var context: NSManagedObjectContext?
//deleting Message
func deleteMessages(entity: String) {
do {
let request = NSFetchRequest(entityName: entity)
print(request)
if let result = try context!.executeFetchRequest(request) as? [your class of NSManagedObject] {
for message in result {
context!.deleteObject(message)
try context!.save()
print(message)
self.tableView.reloadData()
}
}
} catch {
print("miss")
}
}
To use call function
self.deleteMessages("TimeMaster")
From iOS 9, there is the possibility of erasing persistent storage. For better maintenance – create NSPersistentStoreCoordinator extension with abstract function.
extension NSPersistentStoreCoordinator {
func destroyPersistentStore(type: String) -> NSPersistentStore? {
guard
let store = persistentStores.first(where: { $0.type == type }),
let storeURL = store.url
else {
return nil
}
try? destroyPersistentStore(at: storeURL, ofType: store.type, options: nil)
return store
}
}
Then destroying SQLite persistent storage looks pretty simple:
let coordinator = persistentContainer.persistentStoreCoordinator
let store = coordinator.destroyPersistentStore(type: NSSQLiteStoreType)
Try this simple solution:
func deleteAllData(entity: String)
{
let appDelegate = UIApplication.sharedApplication().delegate as! AppDelegate
let managedContext = appDelegate.managedObjectContext
let fetchRequest = NSFetchRequest(entityName: entity)
fetchRequest.returnsObjectsAsFaults = false
do
{
let results = try managedContext.executeFetchRequest(fetchRequest)
for managedObject in results
{
let managedObjectData:NSManagedObject = managedObject as! NSManagedObject
managedContext.deleteObject(managedObjectData)
}
} catch let error as NSError {
print("Detele all data in \(entity) error : \(error) \(error.userInfo)")
}
}
Swift 4
func deleteAllData(_ entity:String) {
let fetchRequest = NSFetchRequest<NSFetchRequestResult>(entityName: entity)
fetchRequest.returnsObjectsAsFaults = false
do {
let results = try dataController.viewContext.fetch(fetchRequest)
for object in results {
guard let objectData = object as? NSManagedObject else {continue}
dataController.viewContext.delete(objectData)
}
} catch let error {
print("Detele all data in \(entity) error :", error)
}
}
Implementation:
self.deleteAllData("your_entityName")
I use this one for delete all the core data entities in Swift3
func deleteAllCD(){
for entityName in ["EntityName", "AnotherEntityName"]{
let request = NSFetchRequest<NSFetchRequestResult>(entityName: entityName)
let delAllReqVar = NSBatchDeleteRequest(fetchRequest: request)
do { try persistentContainer.viewContext.execute(delAllReqVar) }
catch { print(error) }
}
}