Search code examples
couchbasesql++

Couchbase : How to Delete Multiple Documents By Using Document ID pattern


I am new to Couchbase database.

I have populated a bucket with 10,000 documents and I want to remove these documents using document Id pattern by using N1Ql delete query. For example the keys are like :ao.sl3:eid:89049032000001000000000016677381. So I want to use a pattern , something like ':ao.sl3:eid%' to delete all the documents.

On Couchbase Web UI, my document looks like below image - enter image description here

I want to use CouchBase web UI query editor to delete the documents.

Thanks


Solution

  • You will need a primary index to do this, but you can simply use the LIKE operator:

    DELETE
    FROM mybucketname
    WHERE META().id LIKE ':ao.sl3:eid%'
    

    Some things to keep in mind:

    • A primary index is a very dangerous thing to have in production. If this is a one-time thing, make sure to remove that primary index after your DELETE has run.
    • If this is something you will be doing in production on a regular basis, you may want to consider an alternate approach. Depending on your use case, you may want to look at TTL, N1QL paging, eventing, or an alternate data model that can use a more efficient index.