Realm-js: Delete existing realm when migration needed

Created on 23 Jun 2016  路  6Comments  路  Source: realm/realm-js

In Realm for ios, there is configuration for this thing through RLMRealmConfiguration.deleteRealmIfMigrationNeeded, is there any setting that we can use for realm react native?

T-Feature

Most helpful comment

This does not yet exist but we will add it at some point. In the meantime, you have two options:

  1. Update the schemaVersion/run a migration and delete all objects in the migration block to accomplish the same thing:
var realm = new Realm({
  schema: ...,
  schemaVersion: newestSchemaVersion,
  migration: function(oldRealm, newRealm) {
    newRealm.deleteAll();
});
  1. There is a method that we are currently using for testing which deletes all existing Realm files in the documents directory. You could run this as a one-off to delete all Realm files whether or not a migration needs to be performed: Realm.clearTestState(). Beware this method could change or be removed in a future in a release, and you probably shouldn't ship an app which is relying on this method.

All 6 comments

This does not yet exist but we will add it at some point. In the meantime, you have two options:

  1. Update the schemaVersion/run a migration and delete all objects in the migration block to accomplish the same thing:
var realm = new Realm({
  schema: ...,
  schemaVersion: newestSchemaVersion,
  migration: function(oldRealm, newRealm) {
    newRealm.deleteAll();
});
  1. There is a method that we are currently using for testing which deletes all existing Realm files in the documents directory. You could run this as a one-off to delete all Realm files whether or not a migration needs to be performed: Realm.clearTestState(). Beware this method could change or be removed in a future in a release, and you probably shouldn't ship an app which is relying on this method.

@alazier, I would have another use case for possibility to delete Realm feature. My plan is to use separate encrypted Realm for storing user sensitive data that is retrieved from backend. I was planning to create secret on the fly on device and store that securely (device keychain). If for some reason the secret disappears from keychain I could always recover by deleting the existing encrypted Realm, creating a new secret and creating a new Realm with this newly created secret and populating that information from backend.

@jniemin - I think what you are asking for is the same as #363 . Hopefully we can get that into a release in the near future.

@jniemin for now you can manually delete the Realm files and associated files using something like RNFS

============>your app

const realm = await getRealm();
console.log(realm.path);

====================> In Terminal

adb shell rm /data/data/com.example.package/files/default.realm
Was this page helpful?
0 / 5 - 0 ratings