2

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.

0

1 Answer 1

2

I tested the code in my previous thread: Cosmos DB - Delete Document with Python and it works fine.

import pydocumentdb;
import pydocumentdb.document_client as document_client

config = {
    'ENDPOINT': 'Your url',
    'MASTERKEY': 'Your master key',
    'DOCUMENTDB_DATABASE': 'familydb',
    'DOCUMENTDB_COLLECTION': 'familycoll'
};

# 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 server s' }

options = {}
options['enableCrossPartitionQuery'] = True
options['maxItemCount'] = 2

result_iterable = client.QueryDocuments('dbs/familydb/colls/familycoll', query, options)

results = list(result_iterable);

print(results)

client.DeleteDocument('dbs/familydb/colls/familycoll/docs/id1',options)

print 'delete success'

I searched exact issue from github and i found that it is caused by the sdk version. You could upgrade your package version or monitor the https request by Fiddler Tool.Please refer to below threads:

1.https://github.com/Azure/azure-sdk-for-ios/issues/108

2.https://github.com/Azure/azure-sdk-for-android/issues/75

3.https://github.com/Azure/azure-sdk-for-ios/pull/114

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.