clearSqlitePersistence function

Future<void> clearSqlitePersistence(
  1. String directory, {
  2. required String appName,
  3. required Uri databaseUrl,
})

Deletes the cache files of databaseUrl of the app named appName in directory: the database file, its side files and its moved-aside copies. Close the FirebaseDatabase first.

databaseUrl is normalised by persistenceDatabaseUrl, which maps a URL onto the key FirebaseDatabase builds: FirebaseDatabase.databaseUrl clears its own files, and so do the spellings of a production URL that the constructor accepts (a trailing /, wss://, the case of the host, port 443, a ?ns= equal to the first label of the host). An emulator cache is named by http://<host>[:<port>]?ns=<namespace> (https on a cloud workstation), with the host and port of the constructor's emulatorHost, not those of its database URL. An emulator URL must carry ?ns=; FirebaseDatabase.databaseUrl always has it. The cloud-workstation cases where a spelling names another cache are described at persistenceDatabaseUrl. Fails with an ArgumentError for a URL that is not a database URL and for an emulator URL without ?ns=.

When the file has a lease row (an open instance, or one that ended without closing, as after a crash or a hot restart), the call first watches it for about 7.5 s. Fails with a StateError while an instance holds the file (in this process or another: it refreshes the row in that time) or another clear of it is running, and with a ClearPersistenceException listing the files that could not be deleted; see SqlitePersistence.clear.

Implementation

Future<void> clearSqlitePersistence(
  String directory, {
  required String appName,
  required Uri databaseUrl,
}) async {
  final Uri normalised;
  try {
    normalised = persistenceDatabaseUrl(databaseUrl);
  } on FormatException catch (e) {
    throw ArgumentError(e.message, 'databaseUrl');
  }
  return SqlitePersistence(
    directory,
  ).clear(PersistenceKey(appName: appName, databaseUrl: normalised));
}