Skip to content Skip to sidebar Skip to footer

Remove Room Database On App Uninstall

I am making an app and I am using Android Room Persistence Library to handle my database layer. Room Library works like charm and everything is fine with it. But I want the databas

Solution 1:

Is there a configuration of the databasebuilder that does the job

AFAIK room does not know about app getting uninstall so Room probably won't wipe the database for you on uninstall.

How come the database persists after app uninstall

Beginning with Android 6.0 (API level 23), Android offers the Auto Backup feature which is enabled by default. According to the documentations, it backup the following to google drive:

  • Shared preferences files.
  • Files saved to your app's internal storage, accessed by getFilesDir() or getDir(String, int).
  • Files in the directory returned by getDatabasePath(String), which also includes files created with the SQLiteOpenHelper class.
  • Files on external storage in the directory returned by getExternalFilesDir(String).

This backup your room database too.

How to disable auto backup

In your manifest:

<manifest... >
    ...
    <applicationandroid:allowBackup="false"... >
        ...
    </application></manifest>

Solution 2:

  1. Make your App Uninstall aware like described in this answer from AnniJais.
  2. Make a method in the Dao with the @Query Annotation that deletes the Database.
@Query("Drop Database myDatabaseName")
publicvoidnukeDatabase();
  1. Call your Dao method in the part previously added:

if (activityName.equals("com.android.packageinstaller.UninstallerActivity")) { // User has clicked on the Uninstall button under the Manage Apps settings

Step 2 is not really testet of me.

Post a Comment for "Remove Room Database On App Uninstall"