March 23, 2025
+ 12
Differentiate between document deletion and document expiration in Eventing Function logs.

The OnDelete handler in an Eventing Function runs whenever a document is deleted or has expired. The Function accesses the expired field of the handler’s second optional argument, and logs whether a document was deleted or expired from a collection.

Prerequisites

Before trying out the examples on this page, you must first:

  • Create two buckets called bulk and rr100 with a minimum size of 100MB.

  • Inside the bulk bucket, create two keyspaces called bulk.data.source and bulk.data.target.

  • Inside the rr100 bucket, create one keyspace called rr100.eventing.metadata.

For more information about creating buckets, scopes, and collections, see Manage Buckets.

clusters:eventing-service/add-eventing-functions.adoc

Do not add, modify, or delete documents in the Eventing storage keyspace rr100.eventing.metadata while your Eventing Functions are in a deployed state.

Example: Differentiate between Expiration and Deletion

This example walks you through how to differentiate between expiration and deletion in Eventing Function logs.

Create a New Document

You can create a test document with an expiration time in one of the following ways:

  • Using a SQL++ statement in the Query Workbench

  • Using the command line KV client (cbc)

  • Using a Python or Java SDK script

To use a SQL++ statement in the Query Workbench:

  1. Go to Data Tools  Query.

  2. For the Query Context, select bulk as the bucket and data as the scope.

  3. In the code editor, enter the following query:

sql++
UPSERT INTO `bulk`.`data`.`source` (KEY, VALUE) VALUES ("SampleDocument2", {"a_key":"a_value"}, {"expiration":600});

For information about setting a document’s expiration time with SQL++, see Insert a document with expiration.

You now have a document in the source collection with a set expiration date. This document is deleted after 600 seconds.

Create an Eventing Function

To create a new Eventing Function:

  1. Go to Data Tools  Eventing.

  2. Click Add Function.

  3. In the Settings page, enter the following Function settings:

    • delete_v_expiry under Name.

    • Determine if a document was removed due to an expiration or a deletion. under Description.

    • The keyspace bulk.data.source under Listen to Location.

    • The keyspace rr100.eventing.metadata under Eventing Storage.

  4. Click Next.

  5. In the Bindings page, click Next. You do not need to create any bindings for this Function.

  6. In the code editor, replace the placeholder JavaScript code with the following code sample:

    javascript
    function OnDelete(meta, options) { if (options.expired) { log("doc expired:",meta.id); } else { log("doc deleted:",meta.id); } }
  7. Click Create function to create your Eventing Function.

The OnDelete handler creates a log that shows whether a document expired or was deleted.

When the document reaches its expiration time, the document is deleted as soon as one of the following happens:

  • You try to access the document

  • The expiry pager runs (every 60 minutes)

  • Compaction runs

Deploy the Eventing Function

Deploy your Eventing Function:

  1. Go to Data Tools  Eventing.

  2. Click More Options (⋮) next to delete_v_expiry.

  3. Click Deploy to deploy your Function.

After it’s deployed, the Eventing Function executes on all existing documents and any documents you create in the future.

Check Document Expiration

When a document reaches its expiration time, the document is deleted as soon as one of the following happens:

  • You try to access the document

  • The expiry pager runs (every 60 minutes)

  • Compaction runs

To check that the Eventing Function is targeting expired documents:

  1. Go to Data Tools  Documents.

  2. Select the keyspace bulk.data.source in the Get documents from list.

The document SampleDocument2 is already deleted if its expiration time has been reached.

If the document SampleDocument2 is still being displayed, go to another page on the Capella UI and thenr return to Data Tools  Documents. The collection updates and deletes the document because you tried to access it.

Check the Eventing Function Log for Expiration

To check the Eventing Function log:

  1. Go to Data Tools  Eventing.

  2. Click the Log icon next to the delete_v_expiry Eventing Function. You should see the line "doc expired:" "SampleDocument2".

Check Document Deletion

To check that the Eventing Function is targeting deleted documents:

  1. Go to Data Tools  Documents.

  2. Select the keyspace bulk.data.source in the Get documents from list.

  3. Click Create Document.

  4. In the Document ID field, enter SampleDocument3.

  5. Leave the placeholder JSON text.

    json
    { "click": "to edit", "with JSON": "there are no reserved field names" }
  6. Click Save to create the document.

  7. Click the Delete icon next to SampleDocument3.

  8. In the Delete Document dialog, enter delete and click Delete document.

Check the Eventing Function Log for Deletion

To check the Eventing Function log for deletion:

  1. Go to Data Tools  Eventing.

  2. Click the Log icon next to the delete_v_expiry Eventing Function. You should see the line "doc deleted:" "SampleDocument3".