160

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.

Eric Leschinski
  • 146,994
  • 96
  • 417
  • 335
Jim
  • 1,821
  • 2
  • 13
  • 7

13 Answers13

436

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.

Eric Leschinski
  • 146,994
  • 96
  • 417
  • 335
Luke Dunstan
  • 4,862
  • 1
  • 17
  • 15
  • 16
    Make sure you close all Database connections before deleting. Else you'll have to restart the application. – Ifrit Aug 02 '13 at 15:58
15

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.

Rick Falck
  • 1,778
  • 3
  • 15
  • 19
12

Try:

this.deleteDatabase(path); 

or

context.deleteDatabase(path);
Kjartan
  • 18,591
  • 15
  • 71
  • 96
Drw
  • 470
  • 8
  • 19
12

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
Dean2690
  • 230
  • 1
  • 6
  • 14
Barmaley
  • 16,638
  • 18
  • 73
  • 146
  • I get the message back "rm failed for mydatabase.db, Permission denied". Must you have a rooted phone for this to work? Or is there a way to specify a permission? Or does it only work on the emulator? – PeteH Jan 24 '13 at 07:21
  • 2
    @PeteH On emulator you don't need permission, but for real device u'd need to be rooted – Barmaley Jan 24 '13 at 07:30
  • 12
    This requires root and **is not programmatically**. – Zoe Jun 11 '17 at 10:06
  • For me this did not require any special setup. I mean sure, I was automatically logged in as root, but everything worked :) – vlad-ardelean Sep 04 '17 at 20:42
  • OK, I'm here. `root@generic_x86:/data/data/com.dslomer64.servyhelperton/databases #`. Other than removing, copying, and other things, isn't there a `dir` or `list files` or `what the heck is in this directory/folder` command? – DSlomer64 Sep 12 '17 at 10:20
  • `ls` lists files in current folder. – DSlomer64 Sep 22 '17 at 12:25
9
context.deleteDatabase("database_name.db");

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

6

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.

xtempore
  • 5,260
  • 4
  • 36
  • 43
  • 1
    Does this work with an actual phone device? I could not see any subfolders under data folder while viewing the in the DDMS mode. – sammiwei Feb 02 '12 at 22:20
  • 1
    But this question is about deleting the database programmatically? – Simon Dec 06 '15 at 16:49
4

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.

Aun
  • 1,883
  • 1
  • 17
  • 26
4

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.

2

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

If you are going to delete Table or Database , this way is worked:

1- Delete Table - It means keep Database but clean data from a table

    * Just in DataHelper class add


SQLiteDatabase db = this.getWritableDatabase();
db.execSQL("delete from " + TABLE_NAME);

2- Delete Database - It means clean all records but keep file name

In the Activity

  myDb = new DbHelper(this);
  myDb.close();
  myDb.delAll(getApplicationContext());
  recreate();

In DataHelper class add

public void delAll(Context context) {

        context.deleteDatabase(DB_NAME);
    }
Mori
  • 2,653
  • 18
  • 24
0

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));
        }
Afshin Razaghi
  • 410
  • 4
  • 10
0

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.

-3

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.

Chris
  • 3,328
  • 1
  • 32
  • 40
f470071
  • 1,527
  • 3
  • 18
  • 30
  • 8
    The OP said *programatically*. Going to settings and hitting a button is **not** programatically. – mmking Nov 05 '15 at 19:26
  • 1
    @mmking I was aware of that. It is just that this is the simplest wa to remove database and not tables and content. He said he needs it to test database creation and this does that. I have been looking for the same thing and settled for that. – f470071 Nov 05 '15 at 19:29