python-2.7azure-cosmosdb

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)

Solution

  • 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)