Deleting a table Entry in CosmosDB from an azure Function

Nicolas Fromme 25 Reputation points
2023-12-08T10:24:33.1933333+00:00

Hi,

I want to delete a table entry in my cosmosDB from my Azure function. I only found ways to write and read from the table.

An example in nodeJS in the portal would be appreciated.

Thanks in advance!

Azure Functions
Azure Functions
An Azure service that provides an event-driven serverless compute platform.
5,929 questions
Azure Cosmos DB
Azure Cosmos DB
An Azure NoSQL database service for app development.
1,906 questions
{count} votes

2 answers

Sort by: Most helpful
  1. Sajeetharan 2,261 Reputation points Microsoft Employee
    2023-12-08T13:43:28.5766667+00:00

    You need to create a new Function and within the Trigger, you can write a delete logic using the SDK method,

      await item.delete();																																														
    
    
    

    Here is a sample implementation with .Net.https://stackoverflow.com/questions/64326819/delete-cosmosdb-entry-using-azure-function you can follow the same approach for Nodejs as well. https://github.com/Azure/azure-sdk-for-js/blob/main/sdk/cosmosdb/cosmos/samples-dev/ItemManagement.ts#L235

    1 person found this answer helpful.

  2. MuthuKumaranMurugaachari-MSFT 22,441 Reputation points Moderator
    2023-12-11T16:24:11.9266667+00:00

    Nicolas Fromme Thanks for posting your question in Microsoft Q&A. As per doc: Azure Cosmos DB trigger for Azure Functions 2.x and higher, the trigger uses the Azure Cosmos DB change feed to listen for inserts and updates across partitions but doesn't include updates for deletions. Hence, you would need to use cosmos SDK as suggested by Sajeetharan.

    From the description above, it appears that you would like to perform it via in-portal development. Here are the steps you can follow:

    1. Select Advanced Tools -> Go option in the azure portal to open Kudu console and then choose CMD from Debug console option at the top.
    2. Then navigate to C:\home\site\wwwroot directory and install https://www.npmjs.com/package/@azure/cosmos library with the below command (npm install @azure/cosmos):

    User's image

    That should create package.json file which includes cosmos package with the latest supported version, and you can perform delete operation inside the function like below:

    const { CosmosClient } = require("@azure/cosmos");
    
    module.exports = async function (context, req) {
        context.log('JavaScript HTTP trigger function processed a request.');
    
        const name = (req.query.name || (req.body && req.body.name));
        const responseMessage = name
            ? "Hello, " + name + ". This HTTP triggered function executed successfully."
            : "This HTTP triggered function executed successfully. Pass a name in the query string or in the request body for a personalized response.";
    
        const endpoint = process.env.COSMOS_ENDPOINT;
        const key = process.env.COSMOS_KEY;
        const databaseId = process.env.COSMOS_DATABASE_ID;
        const containerId = process.env.COSMOS_CONTAINER_ID;
    
        const client = new CosmosClient({ endpoint, key });
        const container = client.database(databaseId).container(containerId);
    
        const itemId = "your-item-id";
        const partitionKey = "your-partition-key";
    
        const { resource } = await container.item(itemId, partitionKey).delete();
    
        context.res = {
            // status: 200, /* Defaults to 200 */
            body: responseMessage
        };
    }
    

    Note the above code snippet is just for reference and please make changes for your scenario accordingly. More code samples and usage about the package can be found in https://www.npmjs.com/package/@azure/cosmos.

    I hope this helps and let me know if you have any questions.


    If you found the answer to your question helpful, please take a moment to mark it as Yes for others to benefit from your experience. Or simply add a comment tagging me and would be happy to answer your questions.

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.