Remove channels from a document

I’m new to the couchbase. I want to remove channels for some of the documents through sync functions. But right now, it is not updating but adding an extra channel to the document’s meta but not removing the existing channel. Can anyone suggest how I can remove the existing channel in the document?

Sync Function:

function(doc, oldDoc) {
  //....
  if (doc.docType === "differentType") {
    channel("differentChannel");
    expiry(2332800);
    return;
  }
  //.......
}

Document:

{
  "channels": [
    "abcd"
  ],
  "docType": "differentType",
  "_id" : "asjnc"
}

Metadata:

{
  "meta": {
    "id": "asjnc",
    "rev": "64-1b500000000",
    "expiration": 1650383285,
    "flags": 0,
    "type": "json"
  },
  "xattrs": {
    "_sync": {
      "rev": "1-db30e607872",
      "sequence": 777,
      "recent_sequences": [
        777
      ],
      "history": {
        "revs": [
          "1-db30e607872"
        ],
        "parents": [
          -1
        ],
        "channels": [
          [
            "differentChannel"
          ]
        ]
      },
      "channels": {
        "differentChannel": null
      }
    }
  }
}

Expectation of the document with the same metadata:

{
  "channels": [ ], // <--- no channels
  "docType": "differentType",
  "_id" : "asjnc"
}

With this sync function, for the document of type differentType, the channel differentChannel is set in the xattrs section in the metadata. But the channel that was added earlier from the couchbaseLite is not getting removed. Can anyone help?

The "channels" property in a document is counter-intuitively not describing what channels the document is currently in - it’s just a user-definable field that happens to be the default routing for channels if you don’t specify a sync function. It’s up to the writer of the document what it should contain.

If you have another means of channel assignments (like "docType" in your case), then you don’t need to specify "channels" in the document. The sync metadata shows that the document is in “differentChannel” at revision 1-db30e607872 but the contents of the document can be arbitrary.

Hi, thank you for responding. One more question, in this case, as the channels are updated, how can i initiate auto purge as the user doesn’t have access to the new channel?
I created another ticket in regards to that?

Can you please let me know how to purge those documents?

Are you running Couchbase Lite 2.8? Some of this (auto-purge) changed in 3.0, so I’m a little fuzzy on the details. Someone from the Couchbase Lite team could probably help more than I can.

1 Like

Yeah. I’m running CBL 2.8