question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Delete corrupt database and perform a fresh setup

See original GitHub issue

Imagine for some reason we lose our encrypted database key (see Android KeyStore issue) and as a fallback we want to send the user to our app’s registration process. There, the app workflow will be restarted and our Realm database would have to be deleted and set up again.

As we’re not able to delete Realm using deleteAll() API method (lost key), we could remove Realm database file from the file system getting it’s path previously.

Once I send the app to the registration process, I set up the new database with it’s new configuration:

realmConfiguration =
                    new RealmConfiguration.Builder()
                            .encryptionKey(newEncryptionKey)
                            .directory(realmFile)
                            .deleteRealmIfMigrationNeeded()
                            .build();

In this step I get two behaviors:

  1. If the database key is matches the previous one, I get a “class_xxxxxx has no primary key defined” error.
  2. Instead, if the database key is different and fresh one, I get the “Wrong key used to decrypt Realm.” error.

How could I solve this problem?

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Reactions:1
  • Comments:12 (7 by maintainers)

github_iconTop GitHub Comments

2reactions
knethcommented, Oct 11, 2016

@XabierGoros I don’t think we have ever considered this case - thanks for bringing it up 😃

Currently solution is either to manually delete the Realm file or use a different name.

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

1reaction
XabierGoroscommented, Oct 11, 2016

@Zhuinden I’m basically storing all Realm data in a custom folder, and erasing the whole folder which should be similar to the code above except it is performed by RealmCache (not accessible outside):

File realmFile = new File(mContext.getFilesDir() + "/database", "myDbName.realm");
if (realmFile != null) {
    deleteRecursive(realmFile.getParentFile());
}
..
private void deleteRecursive(File fileOrDirectory) {
    if (fileOrDirectory.isDirectory())
        for (File child : fileOrDirectory.listFiles())
            deleteRecursive(child);
     fileOrDirectory.delete();
}
Read more comments on GitHub >

github_iconTop Results From Across the Web

How to manually delete a corrupted database instance
I have a corrupted instance in my database which prevents me from starting up the other instances.
Read more >
How Do I Fix MySQL Server Database Corruption Smoothly
Step 1: Download, install and launch Stellar Repair for MySQL software. Step 2: In Select Data Folder window, select the MySQL version you...
Read more >
How to delete a corrupt database from azure sql
1 Answer 1 · Once you are logged in, set read/write · In serach box find your resource · Click actions (POST/DELETE) //...
Read more >
Windows Update Database Corruption - Microsoft Community
Windows Update Database Corruption · Run Command Prompt as Administrator · Stop BITS, Cryptographic, MSI Installer and Windows Update Services. To ...
Read more >
How to Rebuild a Database with a Corrupted or Deleted SQL ...
The log cannot be rebuilt because there were open transactions/users when the database was shutdown, no checkpoint occurred to the database, or ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found