Docs Menu
Docs Home
/ /
Atlas Device SDKs
/ / /

Delete a Realm - Kotlin SDK

On this page

  • Overview
  • Delete a Realm File to Avoid Migration

In some circumstances, such as a client reset scenario, you might need to delete a realm file and its auxiliary files. This is often useful during development to quickly reset your environment. However, doing so when you are running your app and realm instances are still open can cause data corruption or disrupt Atlas Device Sync.

To avoid losing data and disrupting Device Sync, you can delete these files when all instances of a realm are closed. Before you delete a realm file, be sure that you back up any important objects, as you will lose all unsynced data in the realm.

To safely delete a realm file while the app is running, you can use the Realm.deleteRealm() method. The following code demonstrates this:

// You must close a realm before deleting it
realm.close()
// Delete the realm
Realm.deleteRealm(config)

If you iterate rapidly as you develop your app, you may want to delete a realm file instead of migrating it when you make schema changes. The Realm configuration provides a deleteRealmIfMigrationNeeded parameter to help with this case.

When you use deleteRealmIfMigrationNeeded, Realm deletes the realm file if a migration is required. Then, you can create objects that match the new schema instead of writing migration blocks for development or test data.

val config = RealmConfiguration.Builder(
schema = setOf(Frog::class)
)
.deleteRealmIfMigrationNeeded()
.build()
val realm = Realm.open(config)
Log.v("Successfully opened realm: ${realm.configuration.name}")

Important

Do Not Use deleteRealmIfMigrationNeeded in Production

Never release an app to production with this flag set to true.

Back

Reduce Realm File Size

Next

Encrypt a Realm