Currently, my data is in azure cosmos db (DocumentDB) with wrong or un-optimized partition key. Now I want to update partition key and re distribute my data but not able to figure it out. How can I do it?
3 Answers
Now I want to update partition key and re distribute my data but not able to figure it out how can I do it.
Currently it is not possible to update the partition key attribute in a collection or change partition key value in a document.
If you want to change the partition key attribute, you would need to delete that collection and create a new one with correct partition key attribute.
Similarly, if you wish to update the partition key value in a document you must first delete the document and create a new document with correct partition key value.

- 128,066
- 12
- 206
- 241
-
Thanks for your response. – Nikunj Aggarwal Aug 21 '17 at 09:38
-
1Where did you find the info that one has to delete and recreate the document you want to change the partition key value? Is this answer up to date? – Malte Apr 15 '20 at 14:58
-
@Malte, https://learn.microsoft.com/en-us/azure/cosmos-db/partitioning-overview states that "For all containers, your partition key should: Be a property that has a value, which doesn't change. If a property is your partition key, you can't update that property's value." – SHS Aug 15 '23 at 06:24
Firstly, take advantage of the auto-migration feature, by using the V3 version of SDKs.
After that, your container definition will be enhanced with a _partitionKey
property.
Important bit is that, the existing documents within the container aren’t auto migrated.
You will have to repartition the existing documents by reading them one by one through a script or program, and rewrite them back with _partitionKey
property in the documents.
You can use a combination of Item Id and PartitionKey.None
to iterate through existing documents, for example,
CosmosItemResponse<MyItem> readResponse =
await migratedContainer.Items.ReadItemAsync<MyItem>(
partitionKey: PartitionKey.None,
id: device.Id
);
Refer this Microsoft documentation for further information:
Optional: Bulk Update
You can leverage the Bulk update library, which is available in .NET and Java flavors. Microsoft documentation link

- 8,435
- 1
- 22
- 20
Its not possible in the same container, however you can create new containers in the same database with same or different partition key and then you can provision ADF pipelines on top of your source collections (you can update partition key using derived column) and dump in the target collections.

- 367
- 2
- 6