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