Couchbase DCP: Content is null in listener event for deletion

I have a listener to Couchbase collection changes.
For inserting and updating we get in DocumentChange object the content of the document.
But in case of deletion, the content is null.
In my case, I must have part of content data to use it in consumer.

How can I get content also for deletion?

There is no means to get the content.

Hello,

When dealing with Couchbase and its document change listeners, handling deletions can be a bit tricky since, as you’ve observed, the content of the document is typically null for deletions. However, if you need to access the content of a document even when it is deleted, you can consider the following approaches:

  1. Use Document Expiry/Retention
    Instead of immediate deletion, you could implement a “soft delete” approach where you set an expiry time or flag in the document to mark it as deleted but still retain the data for a certain period. This way, when the document is soft-deleted, you can still access its content before it is actually removed from the database.

Example Implementation:

Soft Delete: Update the document with a deleted flag or a deletionTime field.
Listener: Listen for updates to handle the document’s deletion state and process accordingly.
2. Maintain a Change Log
If you need to track the content of documents that are deleted, you can maintain a separate change log or audit collection that records changes, including deletions, with the content before it is removed.

Steps:

Create an Audit Collection: Before deleting a document, write its content to an audit collection.
On Deletion: Write the document’s content and metadata to the audit collection or log.

Example
file://%20pseudo-code%20for%20capturing%20deletions%20in%20an%20audit%20logpublic%20void%20ondocumentchange(documentchange%20change)%20{%20%20%20%20if%20(change.isdeletion())%20{%20%20%20%20%20%20%20%20document%20document%20=%20change.getolddocument();%20//%20Or%20fetch%20from%20an%20audit%20log%20%20%20%20%20%20%20%20//%20Write%20document%20content%20to%20audit%20log%20before%20deletion%20%20%20%20%20%20%20%20auditLog.insert(document.getId(),%20document.getContent());%20%20%20%20%7D%7D Implement a Custom Pre-Deletion Hook
Depending on the capabilities of your Couchbase setup and client library, you might be able to implement a pre-deletion hook or middleware that intercepts the deletion request and logs the document’s content before it’s deleted.

Example Implementation:

Pre-Delete Hook: Intercept the delete request, capture the document content, and then proceed with the deletion.
4. Use a Separate Service
If tracking deleted documents is critical, you might use a separate service or microservice that listens to all changes and manages deletions. This service can cache document contents and handle the deletion logic.

Example Implementation:

Service: A microservice or background job that maintains a cache or temporary store of document contents.
Listener: This service listens to changes, stores document content, and handles deletions.

Review Couchbase Documentation and Support
Check the latest Couchbase documentation or reach out to Couchbase support for any updates or best practices related to handling document deletions and retrieving content. Couchbase may provide new features or recommendations that fit your use case.

Hope that helps!

Hello,

Implement application logic that can store or log the document content before deletion. Before deleting a document, create a backup or log its content to another storage or database. Maintain a change log or audit trail that records details about documents before they are deleted.

@Krina234pestro has a good idea.

The listener (eventing) can process the content as necessary - and then delete the document.

Thank you all for the assistance , we implemented soft delete solution

1 Like

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.