41

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.

4
  • Hey Captain, are you doing this on ios or android? Those methods do delete all the objects but you will still run into the migration issues. Commented Oct 3, 2014 at 20:27
  • 4
    A quick way to do this on simulator/phone is to just delete the app from the home screen Commented Oct 3, 2014 at 21:04
  • iOS! And where can I learn more about migration? Commented Oct 4, 2014 at 1:18
  • 1
    realm.io/docs/cocoa/0.86.0/#migrations Commented Oct 4, 2014 at 18:13

4 Answers 4

115

Use deleteAll():

let realm = try! Realm()
try! realm.write {
    realm.deleteAll()
}
Sign up to request clarification or add additional context in comments.

5 Comments

Thanks, you guys should add that to the documentation!
This is documented in Realm's API docs: realm.io/docs/objc/latest/api/Classes/RLMRealm.html#//api/name/…, which are more comprehensive than Realm's getting started documentation at realm.io/docs/objc/latest.
I think the API changed a bit. For me to remove all objects using RealmSwiftthis worked: try! realm.write { realm.deleteAll() }
@JohnB. Updated answer.
if the realm notification still running that time, it might cause crash, if directly use realm.deleteAll()
16

As of v0.87.0, there is a deleteAllObjects method on RLRealm that will clear the Realm of all objects.

4 Comments

@cesarferreira I recommend you file an issue with Realm (github.com/realm/realm-cocoa/issues) then.
@cesarferreira can you share the code in github issues?
what do you mean with share github issue?
If you can create a github issue you can copy and paste some code in there to show where it's not working for you
12

Things have moved on in the Realm world - in case anyone comes across this now, there is a property that can be set:

Realm.Configuration.defaultConfiguration.deleteRealmIfMigrationNeeded = true

It then does as advertised. (btw: a lot of the syntax above has changed in case you are trying any of the other methods)

The Github PR https://github.com/realm/realm-cocoa/pull/3463

Comments

5

I think removing the Realm DB file is the valid answer considering that the question was about removing a whole storage rather than migrating it.

Here is a quick Swift code for that (as of Swift 2.1 and Realm 0.96.2):

if let path = Realm.Configuration.defaultConfiguration.path {
    try! NSFileManager().removeItemAtPath(path)
}

I use this code in the DEBUG version of the app if the migration error happens on loading the storage and then I recreate the storage. During development the schema can change a lot, so it would be too cumbersome to bother with migration all the time.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.