Home » Android » How to delete SQLite database from Android programmatically

How to delete SQLite database from Android programmatically

Posted by: admin March 10, 2020 Leave a comment

Questions:

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.

How to&Answers:

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.

Answer:

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

Answer:

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.

Answer:

Try:

this.deleteDatabase(path); 

or

context.deleteDatabase(path);

Answer:

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.

Answer:

context.deleteDatabase("database_name.db");

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

Answer:

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.

Answer:

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.

Answer:

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);

Answer:

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));
        }

Answer:

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.

Answer:

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.