Delete Documents
- how-to
How to delete documents with a command line tool or an SDK.
Introduction
In situations where data is no longer needed, Couchbase Capella 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 Import Data with the Capella UI for details. |
Deleting a Document
To delete a specific field within a document, perform a Sub-Document remove operation.
-
.NET
-
Java
-
Node.js
-
Python
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.
-
.NET
-
Java
-
Node.js
-
Python
-
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.