Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PYTHON - Delete items from a collection in Azure Cosmos DB

I'm new at developing with Azure. I hope you can help with this code. My goal is to delete the items from a collection in Azure Cosmos DB. But I get http error: 400 if I use this value 'partionKey' = '/Structures' and 404 if the value is''. The Error Message = "The partition key supplied in x-ms-partition key header has fewer components than defined in the collection"

client = cosmos_client.CosmosClient("https://....documents.azure.com:443/", {'masterKey': '...'})

options = {}

options['enableCrossPartitionQuery'] = True
options['maxItemCount'] = 5
options['partitionKey'] = '/Structures'

client.DeleteItem("dbs/.../colls/.../docs/.../", options)
like image 567
Aminenima Avatar asked Feb 23 '26 12:02

Aminenima


1 Answers

The error is cause by this line:

options['partitionKey'] = '/Structures'

You need to specify the specific value of partition key here, not the column name.For example,my partition key is '/name',and the specific value in this document is 'A'.

enter image description here

Then your code looks like :

from azure.cosmos import cosmos_client

client = cosmos_client.CosmosClient("https://***.documents.azure.com:443/", {'masterKey': '***'})

options = {}

options['enableCrossPartitionQuery'] = True
options['maxItemCount'] = 5
options['partitionKey'] = 'A'

client.DeleteItem("dbs/db/colls/coll/docs/2", options)
like image 93
Jay Gong Avatar answered Feb 27 '26 02:02

Jay Gong



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!