Deleting all data in a Core Data entity in Swift 3

You’re thinking of NSBatchDeleteRequest, which was added in iOS 9. Create one like this:

let fetch = NSFetchRequest<NSFetchRequestResult>(entityName: "Employee")
let request = NSBatchDeleteRequest(fetchRequest: fetch)

You can also add a predicate if you only wanted to delete instances that match the predicate. To run the request:

let result = try managedObjectContext.executeRequest(request)

Note that batch requests don’t update any of your current app state. If you have managed objects in memory that would be affected by the delete, you need to stop using them immediately.

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)