Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Delete all document in couchbase bucket

Tags:

.net

couchbase

I am using Couchbase 3.o with .Net SDK (V2.0). I am using it as distributed cache. I need to implement "Clear" method to clear all items in cache. That translates to deleting all documents in bucket. I do not see any method on Bucket or Cluster to do so. Is there some other API that I can use?

Thanks

like image 513
ByteBlocks Avatar asked Mar 18 '23 17:03

ByteBlocks


2 Answers

The Couchbase .NET SDK has a flush command, here is an example of how to use it:

var configuration = new ClientConfiguration
{
    Servers = new List<Uri>
    {
        new Uri(ConfigurationManager.AppSettings["bootstrapUrl"])
    }
};
using (var cluster = new Cluster(configuration))
{
     using (var bucket = cluster.OpenBucket())
     {
         var manager = bucket.CreateManager("Administrator", "");
         var result = manager.Flush();
         Assert.IsTrue(result.Success);
     }
}
like image 75
jeffrymorris Avatar answered May 12 '23 11:05

jeffrymorris


The keyword you are looking for is "flush". It is for just such a task. I know there is a REST API call to do this on the server, but I think there is a way in the .NET SDK 2.0 to do this as well. Just make sure you turn on the flush capability at the bucket level on the server. Otherwise it will never work.

As an overall cache invalidation strategy, I'd rather see you set a TTL on each piece of data you put into Couchbase and let Couchbase delete those objects over time instead of the overhead of dumping the cache the way you are talking about.

On a completely different note, make sure you are using the GA version of the 2.0 .NET SDK as it just came out the other day and Couchbase 3.0.1 as there was a nasty bug in the 3.0 release.

like image 35
Kirk Avatar answered May 12 '23 11:05

Kirk