Jim
Jim

Reputation: 1861

How to delete SQLite database from Android programmatically

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.

Upvotes: 164

Views: 213742

Answers (13)

Mori
Mori

Reputation: 4631

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

Upvotes: 1

Jussi Tamminen
Jussi Tamminen

Reputation: 3

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.

Upvotes: 0

Dharmendra Jagodana
Dharmendra Jagodana

Reputation: 69

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

Upvotes: 2

Anurag Mishra
Anurag Mishra

Reputation: 103

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.

Upvotes: 4

Afshin Razaghi
Afshin Razaghi

Reputation: 450

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

Upvotes: 0

vishwanath mirji
vishwanath mirji

Reputation: 91

context.deleteDatabase("database_name.db");

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

Upvotes: 9

Barmaley
Barmaley

Reputation: 16363

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

Upvotes: 13

f470071
f470071

Reputation: 1567

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.

Upvotes: -3

Drw
Drw

Reputation: 470

Try:

this.deleteDatabase(path); 

or

context.deleteDatabase(path);

Upvotes: 12

Aun
Aun

Reputation: 1923

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.

Upvotes: 4

Luke Dunstan
Luke Dunstan

Reputation: 4882

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.

Upvotes: 438

Rick Falck
Rick Falck

Reputation: 1778

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.

Upvotes: 15

xtempore
xtempore

Reputation: 5505

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.

Upvotes: 6

Related Questions