Deleting Data
- how-to
How to delete documents in Couchbase.
This guide is for Couchbase Server.
Introduction
In situations where data is no longer needed, Couchbase Server provides a remove operation to delete a document from the database permanently.
Read the following for further information about the clients available:
Please note that the examples in this guide will alter the data in your sample database. To restore your sample data, remove and reinstall the travel sample data. Refer to Sample Buckets for details. |
Deleting a Document
To delete a specific field within a document, perform a Sub-Document remove operation.
-
cbc
-
.NET
-
Java
-
Node.js
-
Python
Use the cbc rm
command to delete a document from the database.
The example below deletes document airport_1254
from the database.
cbc rm -u Administrator -P password -U couchbase://localhost/travel-sample airport_1254
airport_1254 Deleted
For further details, refer to cbc(1).
Use the RemoveAsync()
method to delete a document from the database.
The example below deletes document hotel-123
from the database.
await hotelCollection.RemoveAsync("hotel-123");
If the document doesn’t exist, the SDK will return a DocumentNotFoundException error.
|
Click the View button to see this code in context.
For further details, refer to CollectionExtensions.
Use the remove()
method to delete a document from the database.
The example below deletes document hotel-123
from the database.
MutationResult removeResult = hotelCollection.remove("hotel-123");
System.out.println("CAS:" + removeResult.cas());
If the document doesn’t exist, the SDK will return a DocumentNotFoundException error.
|
Click the View button to see this code in context.
For further details, refer to Collection.
Use the remove()
function to delete a document from the database.
The example below deletes document hotel-123
from the database.
const removeResult = await hotelCollection.remove('hotel-123')
console.log('CAS:', removeResult.cas)
If the document doesn’t exist, the SDK will return a DocumentNotFoundError error.
|
Click the View button to see this code in context.
For further details, refer to Collection.
Use the remove()
function to delete a document from the database.
The example below deletes document hotel-123
from the database.
remove_result = hotel_collection.remove("hotel-123")
print("CAS:", remove_result.cas)
If the document doesn’t exist, the SDK will return a DocumentNotFoundException error.
|
Click the View button to see this code in context.
For further details, refer to Collection.
Deleting a Sub-Document
To delete a specific field within a document you can perform a Sub-Document remove operation.
-
cbc-subdoc
-
.NET
-
Java
-
Node.js
-
Python
-
Connect to the
cbc-subdoc
interactive shell. -
Use the
remove
command to delete a field from a document. -
Pass the field to remove with the
--path
argument.
The example below deletes the url
field from document hotel-123
.
cbc-subdoc -u Administrator -P password -U couchbase://localhost/travel-sample
subdoc> remove hotel-123 --path url
hotel-123 CAS=0x16be2f11c6040000
0. Size=0, RC=LCB_SUCCESS (0)
If the path cannot be found, cbc-subdoc will return a LCB_ERR_SUBDOC_PATH_NOT_FOUND error.
|
For further details, refer to cbc-subdoc(1).
-
Call the
MutateInAsync()
method, which takes a document ID and an IEnumerable containingMutateInSpec
objects. -
Use a
MutateInSpec
object to specify the sub-operation to be performed within the lookup.
A MutateInResult
object is returned containing the result and metadata relevant to the sub-document remove operation.
The example below deletes the url
field from document hotel-123
.
var mutateInResult = await hotelCollection.MutateInAsync("hotel-123",
specs => specs.Remove("url")
);
Console.WriteLine($"Cas: {mutateInResult.Cas}");
If the path doesn’t exist, the SDK will return a PathNotFoundException error.
|
Click the View button to see this code in context.
For further details, refer to CollectionExtensions.
-
Call the
mutateIn()
method, which takes a document ID and an array ofMutateInSpec
objects. -
Use a
MutateInSpec
object to specify the sub-operation to be performed within the lookup.
A MutateInResult
object is returned, containing the result and metadata relevant to the sub-document remove operation.
The example below deletes the url
field from document hotel-123
.
List<MutateInSpec> specs = Arrays.asList(MutateInSpec.remove("url"));
MutateInResult mutateInResult = hotelCollection.mutateIn("hotel-123", specs);
System.out.println("CAS:" + mutateInResult.cas());
If the path doesn’t exist, the SDK will return a PathNotFoundException error.
|
Click the View button to see this code in context.
For further details, refer to Collection.
-
Call the
mutateIn()
method, which takes a document ID and an array ofMutateInSpec
objects. -
Use a
MutateInSpec
object to specify the sub-operation to be performed within the lookup.
A MutateInResult
object is returned, containing the result and metadata relevant to the sub-document remove operation.
The example below deletes the url
field from document hotel-123
.
mutateInResult = await hotelCollection.mutateIn('hotel-123', [
couchbase.MutateInSpec.remove('url'),
])
console.log('CAS:', mutateInResult.cas)
If the path doesn’t exist, the SDK will return a PathNotFoundError error.
|
Click the View button to see this code in context.
For further details, refer to Collection.
-
Call the
lookup_in()
function, which takes a document ID and a list ofMutateInSpec
objects. -
Use a
MutateInSpec
object to specify the sub-operation to be performed within the lookup.
A MutateInResult
object is returned, containing the result and metadata relevant to the sub-document remove operation.
The example below deletes the url
field from document hotel-123
.
mutate_in_result = hotel_collection.mutate_in(
"hotel-123", [subdocument.remove("url")]
)
print("CAS:", mutate_in_result.cas)
If the path doesn’t exist, the SDK will return a PathNotFoundException error.
|
Click the View button to see this code in context.
For further details, refer to Collection.