Company
Date Published
Author
Nic Raboy, Developer Advocate, Couchbase
Word count
1147
Language
English
Hacker News points
None

Summary

To delete all records from a Couchbase bucket that have a key starting with a particular value, create a Couchbase view and execute a range query using the Couchbase Node.js SDK. The process involves creating a development view through the Couchbase Dashboard or by code, then connecting to a locally hosted Couchbase instance and opening a specific bucket in a Node.js application. A ViewQuery object is created from the design document and view, with a range query specified to capture documents with keys starting with a particular prefix. The results are looped through and each document is deleted one by one using the `remove` method, which is non-blocking and allows for concurrent deletion. This approach can be applied to Couchbase server 2.5 and Java client 1.4 as well, but requires modifications to the Java code. Another alternative is to set a random TTL on each object between now and X days from now, allowing the Couchbase cluster to handle deletion with reduced load.