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 document, perform a remove operation.

    • cbsh

    • .NET

    • Java

    • Node.js

    • Python

    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
    ╭───┬───────────┬─────────┬────────┬──────────┬─────────╮
    │ # │ processed │ success │ failed │ failures │ cluster │
    ├───┼───────────┼─────────┼────────┼──────────┼─────────┤
    │ 0 │         1 │       1 │      0 │          │ capella │
    ╰───┴───────────┴─────────┴────────┴──────────┴─────────╯

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

    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.

    • cbsh

    • .NET

    • Java

    • Node.js

    • Python

    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
    ╭───┬───────────┬─────────┬────────┬──────────┬─────────╮
    │ # │ 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.

    1. Call the MutateInAsync() method, which takes a document ID and an IEnumerable containing MutateInSpec objects.

    2. 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.

    1. Call the mutateIn() method, which takes a document ID and an array of MutateInSpec objects.

    2. 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.

    1. Call the mutateIn() method, which takes a document ID and an array of MutateInSpec objects.

    2. 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.

    1. Call the lookup_in() function, which takes a document ID and a list of MutateInSpec objects.

    2. 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.

    Key-Value Operations with SDKs:

    Sub-Document operations with SDKs: