Delete/Reset all entries in Core Data?
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.
Introduction
Deleting all entries in Core Data can be done with NSBatchDeleteRequest (fastest, bypasses object graph), fetch-and-delete loops (safe with relationships), or destroying the entire persistent store (nuclear reset). The right approach depends on your use case: batch delete for clearing specific entities, fetch-and-delete when cascade rules matter, and store destruction for a full factory reset. This article covers all three methods with Swift examples.
Method 1: NSBatchDeleteRequest (Recommended)
NSBatchDeleteRequest operates directly on the persistent store, bypassing the managed object context. This makes it fast but requires merging changes back into the context to keep in-memory objects in sync.
Method 2: Delete All Entities in the Model
Iterating through all entities in the managed object model ensures everything is cleared without hardcoding entity names.
Method 3: Fetch and Delete (Relationship-Safe)
Fetch-and-delete loads objects into memory and deletes them one by one. This is slower than batch delete but respects cascade delete rules and triggers NSManagedObjectContextObjectsDidChange notifications.
Method 4: Destroy the Persistent Store (Full Reset)
This deletes the SQLite file and recreates an empty one. It is the fastest way to reset everything but loses all data including metadata.
Method 5: Delete the SQLite File Directly
SQLite uses WAL (Write-Ahead Logging) files (-wal and -shm). Delete all three files to ensure a clean reset.
With Predicate (Conditional Delete)
Common Pitfalls
- Batch delete skips validation and cascade rules:
NSBatchDeleteRequestoperates on the SQLite store directly. It does not trigger cascade delete rules, validation, orwillSave/didSavenotifications. Use fetch-and-delete when relationships with cascade rules must be honored. - Stale objects in memory after batch delete: Objects loaded into the context before a batch delete become stale. Always merge changes with
NSManagedObjectContext.mergeChanges(fromRemoteContextSave:into:)or callcontext.reset(). - Forgetting WAL files: Deleting only the
.sqlitefile leaves-waland-shmfiles, which can recreate the database with old data on next launch. Delete all three files. - Deleting on the wrong context: Perform deletes on a background context to avoid blocking the UI. Use
container.newBackgroundContext()for heavy deletion operations. - Not saving after fetch-and-delete:
context.delete(object)marks the object for deletion but does not persist the change. Callcontext.save()after all deletions to write changes to the store.
Summary
NSBatchDeleteRequest— fastest, operates on the store directly, requires manual context sync- Fetch-and-delete — slower but respects cascade rules and triggers notifications
destroyPersistentStore— nuclear option for full database reset- Delete with predicates for conditional clearing of specific records
- Always merge batch delete changes into the context or call
context.reset() - Delete all SQLite-related files (
.sqlite,-wal,-shm) for a clean file-level reset
Related reading
- Design for Facebook authentication in an iOS app that also accesses a secured web service
- Detect a finger swipe through JavaScript on the iPhone and Android
- Detect a Null value in NSDictionary
- Detect application heap size in Android
- Detect backspace in empty UITextField
- Detect current device with UI_USER_INTERFACE_IDIOM in Swift
- Detect current device with UI_USER_INTERFACE_IDIOM in Swift
- Detect if device is iOS
.png&w=3840&q=75)
Tackling System Design Interview Problems
A short course that equips you with the skills to approach system design interviews methodically.
Start the free courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.