-1

I have been looking but can't find Javascript code which will DELETE elements inside Cosmos DB using the Portal UI.

Currently, I have been using the UI to create input and output bindings, and reading and writing through in my index.js:

context.bindings.inputDocument
context.bindings.outputDocument

The inputDocument gives an array, and then I can create new documents by giving outputDocument an array as well. What kind of Javascript code should I write in my index.js or is there another binding to delete specific entries?

Strawberly
  • 53
  • 2
  • 6

2 Answers2

0

you can find azure cosmosdb java script documentation here azure-cosmos

0

The Cosmos DB bindings are useful for read/write as you discovered. For delete operations, you need to manually use the Cosmos DB client.

For Javascript, check the recommended way here:

const cosmos = require('@azure/cosmos');
const endpoint = process.env.COSMOS_ENDPOINT; // Use the name of the setting that contains your Endpoint
const key = process.env.COSMOS_KEY; // Use the name of the setting that contains your Key
const { CosmosClient } = cosmos;

const client = new CosmosClient({ endpoint, key });
// All function invocations also reference the same database and container.
// If on the contrary you need to change the container based on the Trigger, then create the instance inside the Function
const container = client.database("YourDatabase").container("YourContainer");

module.exports = async function (context) {
    const item = container.item("id to delete", "partition key value for item");
    await item.delete();
} 

For more item management samples, see the official ones on the Cosmos JD SDK GitHub.

Matias Quaranta
  • 13,907
  • 1
  • 22
  • 47
  • The @azure/cosmos repository has been moved here [Azure/azure-sdk-for-js](https://github.com/Azure/azure-sdk-for-js/tree/main/sdk/cosmosdb/cosmos) – picklepick Jul 21 '22 at 08:14