How can I easily delete all objects in a Realm
System Design practice on Codemia
Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.
Introduction
If you want to wipe a Realm database, the normal answer is deleteAll() inside a write transaction. Realm keeps writes transactional, so even a full reset must happen in the same controlled write path as any other modification.
Delete everything in one transaction
In Swift, the common pattern looks like this:
deleteAll() removes every object stored in that Realm file. That makes it useful for logout flows, test setup, and local cache resets.
The important part is the write transaction. Realm does not allow you to mutate persisted objects outside one, even when the mutation is "delete everything."
Delete one type versus delete the whole Realm
Sometimes "all objects" actually means all objects of one model class. In that case, delete just the query result instead of wiping the entire Realm.
This matters when the Realm file stores both user data and unrelated local configuration. deleteAll() is powerful, so use it only when you truly mean the full reset.
Threading and lifecycle considerations
Realm objects and query results are thread-confined. That means you should perform the deletion on a thread that owns the Realm instance being used for the write. Do not grab objects from one thread and try to delete them from another Realm instance casually.
If the UI is displaying live Realm results, a full delete can trigger broad updates. That is usually correct behavior, but it is one more reason to make resets deliberate rather than accidental.
It is also worth remembering that deleteAll() clears the contents of the current Realm file. It is not the same as deleting every possible Realm file your app could open under different configurations.
When deleting the file is a different solution
Some developers ask about deleting the Realm file from disk instead of calling deleteAll(). That is a different operation with different constraints. It requires making sure no Realm instance is open and can be appropriate for full cache replacement or test teardown, but it is not the standard answer for ordinary data clearing.
For application code, deleteAll() is usually safer because it stays inside Realm's transaction model and keeps the database lifecycle predictable.
Be careful with relationships
A full delete removes the stored objects themselves, which naturally clears links to them because the target objects no longer exist. But the business meaning still matters. If a logout flow wipes everything, that may be fine. If a migration or partial reset needs to preserve reference data, deleting the whole Realm may be far too aggressive.
The technical call is easy. The real decision is whether the data model should survive a reset partially or not at all.
Common Pitfalls
- Calling
deleteAll()outside a write transaction. - Using
deleteAll()when only one object type should have been removed. - Forgetting that Realm instances and objects are thread-confined.
- Confusing a transactional database wipe with physically deleting the Realm file.
- Triggering a full local-data reset in application code without clearly intending to wipe everything.
Summary
- To delete everything in a Realm, use
realm.deleteAll()insiderealm.write. - Use
realm.delete(realm.objects(MyType.self))when you only want one model class removed. - Keep deletions on the correct thread for the Realm instance you are using.
- Deleting the Realm file is a different operation from clearing its contents.
- The technical reset is simple, but the data-retention decision should be deliberate.
Related reading
- How can I edit a view using phpMyAdmin 3.2.4?
- How can I find all the tables in MySQL with specific column names in them?
- How can I find all the tables in MySQL with specific column names in them?
- How can I find non-ASCII characters in MySQL?
- How can I enable zoom in on UIWebView which inside the UIScrollView?
- How can I encode/decode a string to Base64 in Swift?
- How can I fix EMPTYUNREACHABLE on deploying a test replset on my mac?
- How can I generate entities classes from database using Spring Boot and IntelliJ Idea?

System Design Fundamentals
Build a strong foundation in designing scalable, reliable distributed systems.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
System Design practice on Codemia
Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.