Delete Cosmos Document

Delete Document – https://docs.microsoft.com/en-us/rest/api/cosmos-db/delete-a-document

Authorization behaviour for all the calls will be similar but we have to change verb as per our need, so we have to update Dataweave and HTTP request call to Azure.

Mule flow will look like this

 

Input payload

{
	"id": "103",
	"department": "Accounts"
}

 

Dataweave

%dw 2.0
import java!cosmosdb::GenerateCosmosDBAuthToken
output application/java
var verb = "DELETE"
var resourceType = "docs" 
var resourceId = "dbs/hr/colls/employee/docs/" ++ payload.id
var key = "0SZd1Zs3hySH915hKBTQCbewWnzIEI4S2YK8yHDaRwMrnzrZSwaa0hHy8IW21x4qbsQoswWIh3FW8TOd0fpSYw=="
var keyType = "master"
var tokenVersion = "1.0"		 
var serverDateTime = GenerateCosmosDBAuthToken::getServerTime() as String
---
{
	serverDateTime: serverDateTime,
	authKey: GenerateCosmosDBAuthToken::generate(verb, resourceType, resourceId, key, keyType, tokenVersion, serverDateTime)
}

Please change the resource id(we have to concat payload id while creating the Auth token) and key as per your Azure details. Otherwise we will start getting the unauthorized error

 

HTTP request

<http:request method="DELETE" doc:name="Cosmos DB API" doc:id="a10e0f93-0a08-43af-b6d4-444e1a113f17" config-ref="HTTP_Request_configuration" path="#['dbs/hr/colls/employee/docs/' ++ payload.id]" >
	<http:headers ><![CDATA[#[output application/java
	---
	{
		"Accept" : "application/json",
		"x-ms-version" : "2016-07-11",
		"Authorization" : vars.cosmosHeader.authKey,
		"x-ms-date" : vars.cosmosHeader.serverDateTime,
		"Content-Type" : "application/json",
		"x-ms-documentdb-partitionkey" : '["' ++ payload.department ++ '"]'
	}]]]></http:headers>
</http:request>

Please see the path and different HTTP header we are passing in HTTP request call, please change the same as per your need

Update following in HTTP request call

  • path should now contain the payload id
  • x-ms-documentdb-partitionkey – will hold the value of partition key e.g. Accounts from where we want to delete the document

Test it through SOAP UI

 

Documents in Azure – 103 is deleted

 

Sample application – cosmos-document-delete

Sample SOAP UI project – Cosmos-db-document-delete-soapui-project

 

  
Thank you for taking out time to read the above post. Hope you found it useful. In case of any questions, feel free to comment below. Also, if you are keen on knowing about a specific topic, happy to explore your recommendations as well.
 
For any latest updates or posts on our website, you can follow us on LinkedIn. Look forward to connecting with you there.


Share this:
Subscribe
Notify of
guest
0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments