Delete Documents

  • Capella Operational
  • how-to
March 23, 2025
+ 12
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 document, perform a remove operation.

  1. If you haven’t already done so, use cb-env to set the bucket, scope, and collection where the document is stored.

  2. Use the doc remove command to delete the document by ID.


The example below deletes document hotel-123 from the database.

cb-env bucket travel-sample
cb-env scope inventory
cb-env collection hotel

doc remove hotel-123
Result
console
╭───┬───────────┬─────────┬────────┬──────────┬─────────╮ │ # │ processed │ success │ failed │ failures │ cluster │ ├───┼───────────┼─────────┼────────┼──────────┼─────────┤ │ 0 │ 1 │ 1 │ 0 │ │ capella │ ╰───┴───────────┴─────────┴────────┴──────────┴─────────╯

For further details, refer to Removing in the Couchbase Shell documentation.

Deleting a Sub-Document

To delete a specific field within a document you can perform a sub-document remove operation.

  1. If you haven’t already done so, use cb-env to set the bucket, scope, and collection where the document is stored.

  2. Use the doc get command to retrieve a document by ID.

  3. Pipe the document through the reject filter to remove the field containing the sub-document.

  4. Pipe the output, including the id and content fields, through the doc replace command to update the document.


The example below deletes the url field from document hotel-123.

cb-env bucket travel-sample
cb-env scope inventory
cb-env collection hotel

doc get hotel-123 | reject content.url | doc replace
Result
console
╭───┬───────────┬─────────┬────────┬──────────┬─────────╮ │ # │ processed │ success │ failed │ failures │ cluster │ ├───┼───────────┼─────────┼────────┼──────────┼─────────┤ │ 0 │ 1 │ 1 │ 0 │ │ capella │ ╰───┴───────────┴─────────┴────────┴──────────┴─────────╯
If the field containing the sub-document cannot be found, the reject command returns a Cannot find column error.

For further details, refer to reject for filters in the Nushell documentation.

Key-Value Operations with SDKs:

Sub-Document operations with SDKs: