Filtering out documents that have been soft deleted


We’re currently soft deleting our documents by setting a deleted flag on every document that has been “deleted” instead of actually removing it from the database. I want to provide a mechanism for customers to run N1QL queries on their specific database but I don’t want them to have to include WHERE deleted = “0” in every query nor do I want to have to include it in every N1QL query or .NET SDK query our application uses to retrieve data.

What is the best approach to ensure that any query on data will only ever return documents that have their deleted property set to 0, regardless of the source or type of query? Is that possible with Couchbase?


There is no way to do this in Couchbase. You have to include the hiding predicate in every query.

One thing you could do that might fit your use case is to move soft-deleted records to another bucket, rather than deleting them. That way they would still be around, but wouldn’t be visible in the original bucket.

1 Like

Hmmm… Thats actually a pretty good idea!

Thanks Johan!

1 Like

I’m curious on your use case, are the items being kept for analytical purposes and that’s why you’re implementing your own logical deletion?

No they’re being kept for recovery purposes. If a client accidentally deletes something, our support team can quickly restore the document.

1 Like