How to delete all documents in a database?


#1

Is there a simple way to delete all documents in a database, but still preserve the database (not use database.delete() ) besides iterating over all query results and delete one document at a time?


#2

There is a flush() available at the BucketManager and through the web UI. Note that it’s disabled by default (you can enable it in the UI) and it proceeds asynchronously. If you want to use it from unit tests, since the cluster does not currently provide a way to check for completion, you will want to sleep for a period of time.


#3

Ups - sorry I didn’t spec the context. It’s more how I can programmatically do it in CBL2 for Android :slight_smile:

This is where I’m stuck - don’t know how to get a “document” from a Result:

Query query = QueryBuilder.select(SelectResult.all()).from(DataSource.database(database));
ResultSet rs = query.execute();
for (Result result: rs) {
database.delete(???);
}

But also I’m think it should be possible to clear all documents in a DB faster than iterating? But if not, what should “???” look like above?


#4

Best to post to the mobile -> Couchbase Lite category on the forum then!


#5

Sorry, but it seems to me that’s what I did? I someone moved this for me - in that case thanks! :slight_smile:
Both “Mobile” and “Couchbase Lite” is listed at the top of this post on my screen now at least.


#6

Indeed, someone moved it for you @bro2.


#7

What you described is the correct procedure. You should select the IDs, then retrieve the document by ID and delete it during the iteration. There is no faster way to do this, as the normal way would be to delete and recreate the database which you have said you don’t want to do (is there a reason why?)


#8

Ok - thanks @borrrden. Think I got that one now.