2

Why does the SQLite database file get deleted when the release mode app closes?

My SQLite database works perfectly during the app lifecycle but gets completely deleted when the app is closed only in release mode. Debug mode works fine and the database persists correctly.

I have used GenAI to try help and before removing it I tried to set up platform specific locations to try help- however no luck. Looking at this there seems to be no need for this. I am very lost on this!

Andrio path: /data/data/com.better.mobileapp/files/BetterSQLite.db3

Environment:

  • .NET MAUI 8.0
  • SQLite-net-pcl 1.8.116
  • Target platforms: Android API 34, iOS 17+

What works:

  • Database initializes successfully on app start
  • All tables are created properly
  • Database operations (CRUD) work perfectly during app session
  • File permissions are correct (verified via file explorer on Android)
  • Database file exists and has expected size (~140KB with data)

What fails:

  • After closing the app completely and reopening, database file is gone
  • No error messages or exceptions thrown
  • Directory still exists but the .db3 file is deleted.

This behaviour happens on both Android and iOS. I have tried to debug via Device logs when you close the app, the logs close with it - unless you can force it open?

Minimal Reproduction project if anyone is keen to have a look

Minimal Reproduction Environment:

  • .NET MAUI 9.0
  • SQLite-net-pcl 1.9.172

GitHuh Minimal Reproduction Repo


Project Code:

public static async Task InitializeAsync()
{
     if (_connection != null) return;

     await _semaphore.WaitAsync();

     try
     {
         if (_connection != null) 
             return; // Double-check after acquiring lock

         string dbPath = Path.Combine(FileSystem.AppDataDirectory, "BetterSQLite.db3");

         //// Delete the database file if it exists (uncomment to enable)
         //if (File.Exists(dbPath))
         //    File.Delete(dbPath);

         _connection = new SQLiteAsyncConnection(dbPath);

         // Create the tables if they don't exist
         await _connection.CreateTablesAsync<...,...>();

         _logger?.LogInformation("SQLite database initialized successfully at {DbPath}", dbPath);
     }
     catch (Exception ex)
     {
         _logger?.LogError(ex, "Failed to initialize SQLite database");
         throw new InvalidOperationException("Failed to initialize database", ex);
     }
     finally
     {
         _semaphore.Release();
     }
}

Android Studio file view:

Android Studio File View

14
  • ? You're showing us "tables create" code ... At what point are you actually trying to open an "existing" database? (It's implied you create a new set of tables every time you start up). Re: your code comment vs the "docs": "It is assumed that none of the tables exist in the database." Commented Aug 31 at 16:22
  • 1
    @Lewie The files stored in FileSystem.AppDataDirectory are backed up with the operating system syncing framework. You can try storing the database in other directories (such as Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), "BetterSQLite")) and see if that works. If it's the case, it might be system syncing framework problem and you should report it on maui's github Commented Sep 1 at 12:49
  • 2
    Could it be that you're running a PRAGMA command that changes the persistence of the database? e.g. sqlite.org/pragma.html#pragma_temp_store or sqlite.org/pragma.html#pragma_auto_vacuum? Commented Sep 2 at 4:31
  • 1
    Lewie, not sure cause of problem, but some ideas. 1) Do small tests to isolate what changes between debug and release. It’s likely that some code runs on one case, but not the other. Perhaps DB not properly flushed/closed? Make sure you put it into good state in OnPause. When run again, test whether exists, and pop up an alert to let you know. (I suspect file exists, but in a bad state, so it appears tables not there.) 2) If you run debug build from the app icon on device (instead of from IDE), does it fail same way as release build? Commented Sep 2 at 4:33
  • 2
    @ToolmakerSteve You just need to perform a google search regarding FileSystem.AppDataDirectory. Here you have the link: learn.microsoft.com/en-us/dotnet/maui/platform-integration/…, and here the link to specific Android implementation: developer.android.com/identity/data/autobackup. Since you dind't know about this documentation existence, how could you tell it doesn't affect the file existence? Anyway, I suggested a helpful try would be to store database in other directories just to rule out that possibility. Commented Sep 2 at 6:50

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.