Home > Software engineering >  Update Azure CosmosDB Partition Key and Partition Key Value
Update Azure CosmosDB Partition Key and Partition Key Value

Time:10-04

I have an existing Azure Cosmos DB that uses /id for the partition key. We need to do a bulk delete but we can't do that because the partition key is not the same for all the records. Is there a way to update the partition key and partition key value of existing azure cosmos DB?

CodePudding user response:

To update any property in a document you'd need the id, if you know the id of your documents then might as well just use that for the Bulk Delete.

You can use the SDK in Bulk Mode, get the list of ids and perform concurrent DeleteItemAsync operations, because you know the id and your current partition key is /id then you know the Partition Key value to use too.

Container container = database.GetContainer(ContainerName);
List<Task> tasks = new List<Task>();
foreach (string id in itemsToDelete)
{
    tasks.Add(container.DeleteItemAsync(id, new PartitionKey(id)));
}

// Wait until all are done
await Task.WhenAll(tasks);

CodePudding user response:

Is there a way to update the partition key and partition key value of existing azure cosmos DB?

Simple answer - No. It is not possible to change the partition key attribute of an existing container (collection). Furthermore it is not possible to change the value of a partition key of existing documents inside a container.

To change the partition key value of documents, you will need to create new documents with new partition key value and delete existing ones.

To change the partition key attribute of an existing container, you will need to create a new container and migrate the data from old container to new container.

  • Related