I am working with Cosmos db from azure and the SQL API using python.
I have uploaded some data into cosmos db, but now I want to delete it.
I have seen in this question how to do it: Cosmos DB - Delete Document with Python
But for me is not working, This is my code:
config = {
'ENDPOINT': "ENDPOINT",
'MASTERKEY': 'key',
'DOCUMENTDB_DATABASE': 'database',
'DOCUMENTDB_COLLECTION': 'collection'
};
# Initialize the Python DocumentDB client
client = document_client.DocumentClient(config['ENDPOINT'], {'masterKey': config['MASTERKEY']})
# use a SQL based query to get a bunch of documents
query = { 'query': 'SELECT * FROM c' }
options = {}
options['enableCrossPartitionQuery'] = True
options['maxItemCount'] = -1
options['partitionKey'] = '2017'
result_iterable = client.QueryDocuments('dbs/database/colls/collection', query, options)
results = list(result_iterable);
print(results)
#client.DeleteDocument('dbs/ToDoList/colls/nc4data/docs/'+result_iterableID,options)
for x in range(0, len (results)):
docID = results[x]['id']
print (docID)
client.DeleteDocument('dbs/database/colls/collection/docs/'+docID, options=options)
print ('deleted', docID)
#print ('delete success')
My partition key are years, from 2016 to 2019. What I have tried is to asing partition key in options as one of the years, as the answer I linked above:
options['partitionKey'] = '2017'
Also I have tried to do this:
options['partitionKey'] = 2017 (year is string, but I was trying)
options['partitionKey'] = 'year'
options['partitionKey'] = '/year'
When I insert this 'partitionKey' in my options dictionary I am obtaining an empy list in results.
Also I have tryied without this 'partitionKey', then I get all data, but I get this error:
{\"Errors\":[\"The partition key supplied in x-ms-partitionkey header has fewer components than defined in the the collection.