Search code examples
xamarin.androidandroid-sqlite

DeleteDatabase function dose not delete database in SQLite


This database exists: user.db3

I want to delete this database.

But DeleteDatabase function dose not delete database.

this.DeleteDatabase("user.db3");
string dpPath1 = System.IO.Path.Combine(System.Environment.GetFolderPath(System.Environment.SpecialFolder.Personal), "user.db3");
if (dpPath1.IndexOf("user.db3") < 0)
{
    Toast.MakeText(this, "user.db3 does not exist", ToastLength.Short).Show();
}

Solution

  • You could try the code below to delete the database file.

    //Delete
        private void Button_Clicked_6(object sender, EventArgs e)
        {
    
            FileInfo fi = new FileInfo(_databasePath);
            try
            {
                if (fi.Exists)
                {                    
                    db.Close();
                    GC.Collect();
                    GC.WaitForPendingFinalizers();
                    fi.Delete();
                }
            }
            catch (Exception ex)
            {
                fi.Delete();
            }
        }
    

    _databasePath:

     static readonly string _databasePath = Path.Combine(Xamarin.Essentials.FileSystem.AppDataDirectory, "user.db3");
    

    db:

     static SQLiteConnection db;