How to delete SQLite database from Android programmatically

AndroidSqlite

Android Problem Overview


I would like to delete the database file from the Android file system programatically? Can I have a shell script launch adb which in turns runs a shell script in the Android space to do the database deletion? Can I get this done from within a JUnit test case (with a system() call)?

How do I delete an entire database in Android? I need to make the whole thing go away so I can test database creation. I can drop tables, but that's not enough. This is in the emulator, not on a phone.

Android Solutions


Solution 1 - Android

Once you have your Context and know the name of the database, use:

context.deleteDatabase(DATABASE_NAME);

When this line gets run, the database should be deleted.

Solution 2 - Android

The SQLiteDatabase.deleteDatabase(File file) static method was added in API 16. If you want to write apps that support older devices, how do you do this?

I tried: file.delete();

but it messes up SQLiteOpenHelper.

Thanks.

NEVER MIND! I later realized you are using Context.deleteDatabase(). The Context one works great and deletes the journal too. Works for me.

Also, I found I needed to call SQLiteOpenHelp.close() before doing the delete, so that I could then use LoaderManager to recreate it.

Solution 3 - Android

It's easy just type from your shell:

adb shell
cd /data/data
cd <your.application.java.package>
cd databases
su rm <your db name>.db

Solution 4 - Android

Try:

this.deleteDatabase(path); 

or

context.deleteDatabase(path);

Solution 5 - Android

context.deleteDatabase("database_name.db");

This might help someone. You have to mention the extension otherwise, it will not work.

Solution 6 - Android

Also from Eclipse you can use DDMS which makes it really easy.

Just make sure your emulator is running, and then switch to DDMS perspective in Eclipse. You'll have full access to the File Explorer which will allow you to go in and easily delete the entire database.

Solution 7 - Android

context.deleteDatabase(DATABASE_NAME); will delete the database only if all the connections are closed. If you are maintaining singleton instance for handling your database helper - it is easy to close the opened Connection.

Incase the databasehelper is used in multiple place by instantiating directly, the deleteDatabase + killProcess will do the job even if some connections are open. This can be used if the application scenario doesn't have any issues in restarting the app.

Solution 8 - Android

Delete old Db when uninstall the app.

Setting android:allowBackup="false" in the application tag in AndroidManifest.xml fixed the problem. It seems that for some weird reason the Android OS was restoring from a backup every time I deployed the app.

Solution 9 - Android

you can create a file object of current database path and then delete it as we delete file from folder

    File data = Environment.getDataDirectory();
    String currentDBPath = "/data/com.example.demo/databases/" + DATABASE_NAME;
    File currentDB = new File(data, currentDBPath);
    boolean deleted = SQLiteDatabase.deleteDatabase(currentDB);

Solution 10 - Android

I used Android database delete method and database removed successfully

public bool DeleteDatabase()
        {
            var dbName = "TenderDb.db";
            var documentDirectoryPath = System.Environment.GetFolderPath(System.Environment.SpecialFolder.Personal);
            var path = Path.Combine(documentDirectoryPath, dbName);
            return Android.Database.Sqlite.SQLiteDatabase.DeleteDatabase(new Java.IO.File(path));
        }

Solution 11 - Android

I have used the following for "formatting" the database on device after I have changed the structure of the database in assets. I simply uncomment the line in MainActivity when I wanted that the database is read from the assets again. This will reset the device database values and structure to mach with the preoccupied database in assets folder.

    //database initialization. Uncomment to clear the database
    //deleteDatabase("questions.db");

Next, I will implement a button that will run the deleteDatabase so that the user can reset its progress in the game.

Solution 12 - Android

From Application Manager, you can delete whole application with data. Or just data by it self. This includes database.

  1. Navigate to Settings. You can get to the settings menu either in your apps menu or, on most phones, by pulling down the notification drawer and tapping a button there.

  2. Select the Apps submenu. On some phones this menu will have a slightly different name such as Application Manager.

  3. Swipe right to the All apps list. Ignore the lists of Running and Downloaded apps. You want the All apps list.

  4. Select the app you wish to disable. A properties screen appears with a button for Force Stop on the upper left and another for either Disable or Uninstall updates on the upper right side.

  5. Delete data.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionJimView Question on Stackoverflow
Solution 1 - AndroidLuke DunstanView Answer on Stackoverflow
Solution 2 - AndroidRick FalckView Answer on Stackoverflow
Solution 3 - AndroidBarmaleyView Answer on Stackoverflow
Solution 4 - AndroidDrwView Answer on Stackoverflow
Solution 5 - Androidvishwanath mirjiView Answer on Stackoverflow
Solution 6 - AndroidxtemporeView Answer on Stackoverflow
Solution 7 - AndroidAunView Answer on Stackoverflow
Solution 8 - AndroidAnurag MishraView Answer on Stackoverflow
Solution 9 - AndroidDharmendra JagodanaView Answer on Stackoverflow
Solution 10 - AndroidAfshin RazaghiView Answer on Stackoverflow
Solution 11 - AndroidJussi TamminenView Answer on Stackoverflow
Solution 12 - Androidf470071View Answer on Stackoverflow