A newer version of this documentation is available.

View Latest

Function: Advanced DELETE Operation

    Couchbase Server Enterprise Edition 7.6 reaches End of Life on March 31, 2027. Please upgrade to 8.0.x by following Upgrade, or contact Couchbase Support.

    Perform the Advanced DELETE operation where Eventing interacts with the Data Service.

    The advancedDeleteOp function:

    • Performs the Advanced DELETE operation

    • Requires Eventing Storage (or a metadata collection) and a source collection

    • Requires a binding of type bucket alias

    • Operates on any mutation where doc.type === "control_adv_delete"

    • Always tries to insert the test document and ignores insert errors

    • Has 4 modes of operation: no_cas, bad_cas, good_cas, and no_key

    For more information about the Advanced Self-Recursion Parameter, see Advanced DELETE Operation.

    • advancedDeleteOp

    • Input data

    • Output data

    // Configure the settings for the advancedDeleteOp function as follows:
    //
    // Version 7.1+
    //   "Function Scope"
    //     *.* (or try bulk.data if non-privileged)
    // Version 7.0+
    //   "Listen to Location"
    //     bulk.data.source
    //   "Eventing Storage"
    //     rr100.eventing.metadata
    //   Binding(s)
    //    1. "binding type", "alias name...", "bucket.scope.collection", "Access"
    //       "bucket alias", "src_col",       "bulk.data.source",        "read and write"
    //
    // Version 6.6.1
    //   "Source Bucket"
    //     source
    //   "MetaData Bucket"
    //     metadata
    //   Binding(s)
    //    1. "binding type", "alias name...", "bucket",     "Access"
    //       "bucket alias", "src_col",       "source",     "read and write"
    
    function OnUpdate(doc, meta) {
        if (!meta.id.startsWith("control_adv_delete")) return;
    
        log('input meta', meta);
        log('input doc ', doc);
    
        // Setup, make sure there is a doc to "delete", ignore any errors
        couchbase.insert(src_col,{"id":"test_adv_delete:" + doc.ins_id},{"a:": 1});
    
        var new_meta;
        if (doc.mode && doc.mode === "no_cas") {
            // No CAS is passed - it always succeeds
            new_meta = {"id":"test_adv_delete:" + doc.ins_id};
        }
        if (doc.mode && doc.mode === "bad_cas") {
            // Pass a non-matching CAS - it always fails
            new_meta = {"id":"test_adv_delete:" + doc.ins_id, "cas":"1111111111111111111"};
        }
        if (doc.mode && doc.mode === "good_cas") {
            // Pass the matching or current CAS - it succeeds
            var tmp_r = couchbase.get(src_col,{"id":"test_adv_delete:" + doc.ins_id});
            if (tmp_r.success) {
                // Use the current CAS to read via couchbase.get(...)
                new_meta = {"id":"test_adv_delete:" + doc.ins_id, "cas": tmp_r.meta.cas};
            } else {
                log('Cannot delete due to no such key',"test_adv_delete:" + doc.ins_id);
                return;
            }
        }
        if (doc.mode && doc.mode === "no_key") {
            // Remove (delete with a basic bucket op) so that we have: no such key
            delete src_col["test_adv_delete:" + doc.ins_id]
            new_meta = {"id":"test_adv_delete:" + doc.ins_id};
        }
        var result = couchbase.delete(src_col,new_meta);
        if (result.success) {
            log('success adv. delete: result',result);
        } else {
            log('failure adv. delete: id',new_meta.id,'result',result);
        }
    }
    Mutation #1
    
    INPUT: KEY control_adv_delete::1
    
    {
      "id": 1,
      "type": "control_adv_delete",
      "ins_id": 10,
      "mode": "no_cas"
    }
    
    Mutation #2
    
    INPUT: KEY control_adv_delete::2
    
    {
      "id": 2,
      "type": "control_adv_delete",
      "ins_id": 10,
      "mode": "bad_cas"
    }
    
    Mutation #3
    
    INPUT: KEY control_adv_delete::3
    
    {
      "id": 3,
      "type": "control_adv_delete",
      "ins_id": 10,
      "mode": "no_key"
    }
    
    Mutation #4
    
    INPUT: KEY control_adv_delete::4
    
    {
      "id": 4,
      "type": "control_adv_delete",
      "ins_id": 10,
      "mode": "good_cas"
    }

    Perform 4 deletion attempts. The second attempt fails because of a CAS mismatch. The third attempt fails because the document key does not exist.

    Logs from Mutation #1
    
    2021-01-08T11:45:02.897-08:00 [INFO] "input meta"
    {
        "cas": "1610134800219308032",
        "id": "control_adv_delete::1",
        "expiration": 0,
        "flags": 33554438,
        "vb": 221,
        "seq": 1
    }
    2021-01-08T11:45:02.898-08:00 [INFO] "input doc "
    {
        "id": 1,
        "type": "control_adv_delete",
        "ins_id": 10,
        "mode": "no_cas"
    }
    2021-01-08T11:45:02.899-08:00 [INFO] "success adv. delete: result"
    {
        "meta": {
            "id": "test_adv_delete:10",
            "cas": "1610135102898962432"
        },
        "success": true
    }
    
    Logs from Mutation #2
    
    2021-01-08T11:46:11.225-08:00 [INFO] "input meta"
    {
        "cas": "1610135171148152832",
        "id": "control_adv_delete::2",
        "expiration": 0,
        "flags": 33554438,
        "vb": 468,
        "seq": 3
    }
    2021-01-08T11:46:11.225-08:00 [INFO] "input doc "
    {
        "id": 2,
        "type": "control_adv_delete",
        "ins_id": 10,
        "mode": "bad_cas"
    }
    2021-01-08T11:46:11.228-08:00 [INFO] "failure adv. delete: id" "test_adv_delete:10" "result"
    {
        "error": {
            "code": 272,
            "name": "LCB_KEY_EEXISTS",
            "desc": "The document key exists with a CAS value different than specified",
            "cas_mismatch": true
        },
        "success": false
    }
    
    Logs from Mutation #3
    
    2021-01-08T11:52:51.520-08:00 [INFO] "input meta"
    {
        "cas": "1610135571485425664",
        "id": "control_adv_delete::3",
        "expiration": 0,
        "flags": 33554438,
        "vb": 723,
        "seq": 5
    }
    2021-01-08T11:52:51.520-08:00 [INFO] "input doc "
    {
        "id": 3,
        "type": "control_adv_delete",
        "ins_id": 10,
        "mode": "no_key"
    }
    2021-01-08T11:52:51.522-08:00 [INFO] "failure adv. delete: id" "test_adv_delete:10" "result"
    {
        "error": {
            "code": 272,
            "name": "LCB_KEY_ENOENT",
            "desc": "The document key does not exist on the server",
            "key_not_found": true
        },
        "success": false
    }
    
    Logs from Mutation #4
    
    2021-01-08T11:53:36.070-08:00 [INFO] "input meta"
    {
        "cas": "1610135616063602688",
        "id": "control_adv_delete::4",
        "expiration": 0,
        "flags": 33554438,
        "vb": 183,
        "seq": 3
    }
    2021-01-08T11:53:36.070-08:00 [INFO] "input doc "
    {
        "id": 4,
        "type": "control_adv_delete",
        "ins_id": 10,
        "mode": "good_cas"
    }
    2021-01-08T11:53:36.074-08:00 [INFO] "success adv. delete: result"
    {
        "meta": {
            "id": "test_adv_delete:10",
            "cas": "1610135616073760768"
        },
        "success": true
    }