LoginSignup
2
3

More than 5 years have passed since last update.

AndroidでRealmをファイルごと削除する

Last updated at Posted at 2018-03-22

概要

Realmを削除したいけど、全てcloseしておかないといけないとか色々な事情でrealmインスタンスを取得できないときがあるかもしれないです。

個人的な例

Realmを暗号化した状態で、復号に必要な鍵を紛失したときにRealmが開けなくてクラッシュしてしまいます。
Realm側でサポートしてくれればいいのですが、今現在はサポートされていないみたいです。

Long term solution is that we add a deleteRealmIfWrongEncryptionKey() option to RealmConfiguration.

Delete corrupt database and perform a fresh setup

解決策

Delete corrupt database and perform a fresh setup によると、全てのファイルを消すこと = 手動での削除という風に書いてありますので、Realmに関係するファイル・フォルダ毎削除してしまいます。

fun deleteRealm(context: Context) {
  val dir = context.filesDir
  dir.list().forEach {
    if (it.contains("realm")) {
      File(dir, it).deleteRecursively()
    }
  }
}

これで削除されますので、再度初期化処理を行いましょう。

2
3
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
3