Deleting information

This operation erases an individual document from the data store for a given key. In some SDKs you can specifically check a document’s CAS value, a unique identifier, and if the number provided as a delete parameter does not match the deletion will fail and return an error. If Couchbase Server successfully deletes a document, it returns a status code indicating success or failure.

Note: Be aware that when you delete a key it may not be removed immediately from the server. Instead Couchbase Server flags an item for deletion and if the key is requested by another client, the server returns a 'key not found' error. Couchbase Server will actually remove the item from the server upon the next request for it. Alternately, Couchbase Server has a maintenance process that runs by default every hour and which removes any items flagged for deletion.

It is important to note that in some SDK’s such as in Ruby, a delete can be performed in synchronous or asynchronous mode; in contrast other SDK’s such as Java support delete as an asynchronous operation only. Consult your respective language reference to find out more about your chosen SDK. For more information about asynchronous calls in Couchbase SDKs, see Synchronous and asynchronous transactions.

The following example demonstrates a delete in Ruby. In this case, parameters can be provided to check the unique identifier for a value, so that if there is mismatch, the delete fails:

# returns the cas/unique identifier on set and assigns to ver

ver = c.set("foo", "bar")

# cas mismatch, raises Couchbase::Error::KeyExists

c.delete("foo", :cas => 123456)

#returns true

c.delete("foo", :cas => ver)

When you delete a document for some SDKs you can provide CAS value for the document in order for delete to succeed. As in other update methods, you can obtain the CAS value by performing a get-with-CAS operation and then pass the CAS value as a parameter:

#returns value, flags and cas

val, flags, cas = client.get("rec1", :extended => true)

#removes document as cas operation

client.delete("rec1", :cas => cas)

The memcached protocol equivalents for this method is delete. For more information about the underlying protocol, see memcached protocol.