4

Reading the Azure portal I've understood how to make a POST, PUT and GET operation with CosmosDB through the Azure Functions. But deleting, I don't understand how to do this.

Which bindings should I use. Should it occur either through sql query or collection's method, like Remove()?

        [**FunctionName**("EmployeeDocumentDB")]
        public static async Task<HttpResponseMessage> Run(
        [HttpTrigger(AuthorizationLevel.Function, "post", "put", "delete", Route = "EmployeeDocumentDB/partitionkey/{key}/id/{id}")]HttpRequestMessage req,
        [DocumentDB(
        databaseName: "MyDatabase",
        collectionName: "MyCollection",
        ConnectionStringSetting = "CosmosDBEmulator")] ICollector<Person> outputDocument,
        TraceWriter log)
    {
        dynamic data = await req.Content.ReadAsAsync<Person>();

        return req.CreateResponse(HttpStatusCode.Accepted);
    }
Janusz Nowak
  • 2,595
  • 1
  • 17
  • 36
AlfredBauer
  • 105
  • 2
  • 6

3 Answers3

3

I combined:

  • HTTP trigger
  • CosmoDB DocumentClient Input
  • CosmoDB Input with look up ID from query string
public static async Task<IActionResult> Run(
            [HttpTrigger(AuthorizationLevel.Function, "delete")] HttpRequest req,
            [CosmosDB(databaseName: "storage", collectionName: "pizza", Id = "{Query.id}", PartitionKey = "{Query.storeId}", ConnectionStringSetting = "..."] Document document,
            [CosmosDB(databaseName: "storage", collectionName: "pizza", ConnectionStringSetting = ...)] DocumentClient client)
        {
            string storeId = req.Query["storeId"];

            if(document == null || string.IsNullOrEmpty(storeId))
                return new BadRequestResult();

            await client.DeleteDocumentAsync(document.SelfLink, new RequestOptions() { PartitionKey = new PartitionKey(storeId) });

            return new OkResult();
        }
Magnielcz
  • 31
  • 5
1

You could do this by binding directly to the DocumentClient itself, and delete the Document programatically.

[FunctionName("DeleteDocument")]
public static async Task Run(
    [TimerTrigger("00:01", RunOnStartup = true)] TimerInfo timer,
    [DocumentDB] DocumentClient client,
    TraceWriter log)
{
    var collectionUri = UriFactory.CreateDocumentCollectionUri("ItemDb", "ItemCollection");
    var documents = client.CreateDocumentQuery(collectionUri);

    foreach (Document d in documents)
    {
        await client.DeleteDocumentAsync(d.SelfLink);
    }
}

See CosmosDBSamples

Mikhail Shilkov
  • 34,128
  • 3
  • 68
  • 107
Wah Yuen
  • 1,569
  • 1
  • 9
  • 13
1

I would prefer to leverage the stored procedures programming for Azure Cosmos DB and delete the documents inside a single stored procedure in batch for better performance (network traffic latency, store overhead for transactions, etc.). For more details, you could refer to here.

For creating the stored procedure, you could create it for your collection on Azure Portal in a simple way. And you could follow the sample here for deleting documents in batch for a given query.

For your azure function, you could call the code below for executing the stored procedure to delete the documents in batch:

StoredProcedureResponse<object> result = await client.ExecuteStoredProcedureAsync<object>(
    UriFactory.CreateStoredProcedureUri("MyDatabase", "MyCollection", "bulkDeleteSproc"), 
    "SELECT c._self FROM c WHERE c.founded_year = 2008");
Bruce Chen
  • 18,207
  • 2
  • 21
  • 35