Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to delete properties from an item in CosmosDB

I have a Python code to manage a collection in Cosmos DB by utilizing CosmosClient. It provides upsert_item, query_item, create_item, etc. But I need to delete properties from an existing item. Because the item could contain lots of properties, I wonder if there any better approach other than updating the whole item. I tried following code:

for i, item in enumerate(container.query_items(
        query='SELECT * FROM col c WHERE c._partitionKey="POLICY"',
        enable_cross_partition_query=True)):
    if (item["id"] == "$DEFAULT"):
        item['p1']=''
        item['p2']=''
        response = container.upsert_item(item)

This only updated p1 and p2 to empty string instead of deleting them. Anyone knows how to delete them programmatically? Since the partitionKey is POLICY, I couldn’t delete them in the portal.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

To delete an item from a dictionary in Python, you will need to use del. So your code would be something like:

for i, item in enumerate(container.query_items(
        query='SELECT * FROM col c WHERE c._partitionKey="POLICY"',
        enable_cross_partition_query=True)):
    if (item["id"] == "$DEFAULT"):
        del item['p1']
        del item['p2']
        response = container.upsert_item(item)
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading