Delete and re-create database

I am looking for a way to delete or even flush all the documents then re-create the database before I insert documents. Basically I have an array to insert with batch insertion so i want to delete the existing database (if it exists) or even clear out the documents but I am having different errors:

database.delete();

at times it doesn’t work at all with no error (likely I don’t know how to wait for the operation to finish?). At times, the error is is:

cannot delete database with open connections.

So i tried closing it first with:

database.close();
databas.delete();

but it fails with error cannot do operation on a closed database.

the complete code is:

   private void saveInformation(ReadableArray data, Callback callback) {
     String TAG = "saveInfo";
     //this.database.close();
     this.database.delete();
     this.createDB();
    Integer totalDocumentsToAdd = data.size();
    try {
        this.database.inBatch(() -> {
            for (int i = 0; i < totalDocumentsToAdd; i++) {
                ReadableMap record = data.getMap(i);
                //Date startDate = Utils.StringToDate(record.getString("StartDate"), "T");
                //Date endDate = Utils.StringToDate(record.getString("EndDate"), "T");
                String startDate = record.getString("StartDate");
                String endDate = record.getString("EndDate");
                MutableDocument document = new MutableDocument()
                .setDouble("Value", record.getDouble("Value"))
                .setString("StartDate", startDate)
                .setString("EndDate", endDate);

                try {
                    this.database.save(document);
                    Log.i(TAG, "saved document " + i);
                } catch (CouchbaseLiteException e) {
                    e.printStackTrace();
                    Log.i(TAG, "error saving  document " + e.toString());
                }


            }

        });
        Log.i(TAG, "finished saving  documents ");
        callback.invoke(null, true);
    } catch (CouchbaseLiteException e) {
        Log.i(TAG, "error finished savingdata " + e.toString());
        e.printStackTrace();
        callback.invoke(e, null);
    }

}

Which version of Couchbase Lite are you using? I would recommend upgrading to 2.8.1 which includes enhancements to the delete() and close() APIs that should simplify your task of closing/stopping replications, removing live query observers etc.

Also, what is your use case? Is this a testing requirement ? I don’t see a real world application of regularly deleting a local database (defeats the purpose of a local database)