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:
