Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I easily delete all objects in a Realm

Tags:

ios

swift

realm

I have the choice of doing a migration, but I would prefer to delete everything in my defaultRealm(). How can I do this easily?

realm.deleteObject(object) 

is the only function along with .deleteObjects.

I have tried the following code:

Method 1

realm.deleteObjects(RLMObject.objectsInRealm(realm, withPredicate: NSPredicate(value: true))) 

Method 2

        realm.deleteObjects(Dog.allObjectsInRealm(realm))         realm.deleteObjects(Person.allObjectsInRealm(realm))         realm.deleteObjects(Goal.allObjectsInRealm(realm))         realm.deleteObjects(Goals.allObjectsInRealm(realm)) 

Both fail to prevent the migration exception.

like image 734
CaptainCOOLGUY Avatar asked Oct 03 '14 19:10

CaptainCOOLGUY


People also ask

How do I delete all data from a realm?

The right way of deleting your entire Realm (schema) is to use : Realm realm = Realm. getDefaultInstance(); realm. beginTransaction(); // delete all realm objects realm.

How delete all data from realm react native?

To delete all objects from the realm, call Realm. deleteAll() inside of a write transaction. This clears the realm of all object instances but does not affect the realm's schema.

What is realm react native?

The Realm React Native SDK allows you to use Realm Database and backend Apps from React Native applications for iOS and Android written in JavaScript or TypeScript.


1 Answers

Use deleteAll():

let realm = try! Realm() try! realm.write {     realm.deleteAll() } 
like image 59
jpsim Avatar answered Sep 21 '22 21:09

jpsim