Hello, anyone here using kotbase, I could not find...
# multiplatform
e
Hello, anyone here using kotbase, I could not find a channel for it so I am posting here. Is there a way to simply purge everything from the database?
p
GitHub discussion or issues maybe
j
The simplest way would be to just delete and then recreate the database. Otherwise you can query all document IDs and purge them. You'd do this for all the documents in a collection. So if you're using collections other than the default, you'd do this for each of them. Doing this in a transaction is quickest.
Copy code
database.inBatch {
    QueryBuilder.select(SelectResult.expression(Meta.id))
        .from(DataSource.collection(collection))
        .execute().use { rs ->
            rs.forEach { result ->
                result.getString(0)?.let { id ->
                    collection.purge(id)
                }
            }
        }
}
Or with the KTX query extensions:
Copy code
database.inBatch { 
    select(Meta.id)
        .from(collection)
        .execute().use { rs ->
            rs.forEach { result -> 
                result.getString(0)?.let { id ->
                    collection.purge(id)
                }
            }
        }
}
Note the difference between purge and delete, where delete preserves a tombstone for syncing the deletion, if you're replicating data with a backend. Purge simply purges the document entirely.
While there's not a dedicated Kotbase channel, #couchbase would be a good channel for questions and discussion. I frequent #multiplatform, but I'll definitely see questions in #couchbase, since there's less traffic there. There are also Couchbase developers in that channel and they're the real experts for the underlying Couchbase Lite database. I list some other good community resources on the Kotbase website here.
e
Thank you that worked.
👍🏼 1