Skip to content

Commit d47c5c9

Browse files
authored
Merge pull request #208606 from deborahc/dech-pkdelete-docs
Adding public docs for Cosmos DB delete by partition key preview feature
2 parents 4407418 + bb42060 commit d47c5c9

File tree

3 files changed

+108
-1
lines changed

3 files changed

+108
-1
lines changed

articles/cosmos-db/sql/TOC.yml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -418,6 +418,8 @@
418418
href: ../how-to-define-unique-keys.md
419419
- name: Perform transactional batch operations
420420
href: transactional-batch.md
421+
- name: Delete items by partition key (preview)
422+
href: how-to-delete-by-partition-key.md
421423
- name: Find request unit charge
422424
displayName: request units, RUs, RU, charge, consumption
423425
href: find-request-unit-charge.md
@@ -1278,4 +1280,4 @@
12781280
- name: Common use cases
12791281
href: ../use-cases.md?toc=/azure/cosmos-db/sql/toc.json
12801282
- name: Social media apps
1281-
href: ../social-media-apps.md?toc=/azure/cosmos-db/sql/toc.json
1283+
href: ../social-media-apps.md?toc=/azure/cosmos-db/sql/toc.json
Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
---
2+
title: Delete items by partition key value using the Cosmos SDK (preview)
3+
description: Learn how to delete items by partition key value using the Cosmos SDKs
4+
author: deborahc
5+
ms.author: dech
6+
ms.service: cosmos-db
7+
ms.subservice: cosmosdb-sql
8+
ms.topic: how-to
9+
ms.date: 08/19/2022
10+
ms.custom:
11+
---
12+
13+
# Delete items by partition key value - SQL API (preview)
14+
[!INCLUDE[appliesto-sql-api](../includes/appliesto-sql-api.md)]
15+
16+
This article explains how to use the Cosmos SDKs to delete all items by logical partition key value.
17+
18+
## Feature overview
19+
20+
The delete by partition key feature is an asynchronous, background operation that allows you to delete all documents with the same logical partition key value, using the Comsos SDK.
21+
22+
Because the number of documents to be deleted may be large, the operation runs in the background. Though the physical deletion operation runs in the background, the effects will be available immediately, as the documents to be deleted will not appear in the results of queries or read operations.
23+
24+
To help limit the resources used by this background task, the delete by partition key operation is constrained to consume at most 10% of the total available RU/s on the container each second.
25+
26+
## Getting started
27+
28+
To use the feature, your Cosmos account must be enrolled in the preview. To enroll, submit a request for the **DeleteAllItemsByPartitionKey** feature via the [**Preview Features** page](../../azure-resource-manager/management/preview-features.md) in your Azure Subscription overview page.
29+
30+
:::image type="content" source="media/how-to-delete-by-partition-key/preview-enrollment-delete-by-partition-key.png" alt-text="Screenshot that shows the enroll in Delete All Items By Partition Key in Preview Features blade.":::
31+
32+
#### [.NET](#tab/dotnet-example)
33+
34+
## Sample code
35+
Use [version 3.25.0-preview](https://www.nuget.org/packages/Microsoft.Azure.Cosmos) (or a higher preview version) of the Azure Cosmos DB .NET SDK to delete items by partition key.
36+
37+
```csharp
38+
// Suppose our container is partitioned by tenantId, and we want to delete all the data for a particular tenant Contoso
39+
40+
// Get reference to the container
41+
var container = cosmosClient.GetContainer("DatabaseName", "ContainerName");
42+
43+
// Delete by logical partition key
44+
ResponseMessage deleteResponse = await container.DeleteAllItemsByPartitionKeyStreamAsync(new PartitionKey("Contoso"));
45+
46+
if (deleteResponse.IsSuccessStatusCode) {
47+
Console.WriteLine($"Delete all documents with partition key operation has successfully started");
48+
}
49+
```
50+
#### [Java](#tab/java-example)
51+
52+
Use [version 4.19.0](https://mvnrepository.com/artifact/com.azure/azure-cosmos) (or a higher version) of the Azure Cosmos DB Java SDK to delete items by partition key. The delete by partition key API will be marked as beta.
53+
54+
55+
```java
56+
// Suppose our container is partitioned by tenantId, and we want to delete all the data for a particular tenant Contoso
57+
58+
// Delete by logical partition key
59+
CosmosItemResponse<?> deleteResponse = container.deleteAllItemsByPartitionKey(
60+
new PartitionKey("Contoso"), new CosmosItemRequestOptions()).block();
61+
```
62+
---
63+
64+
### Frequently asked questions (FAQ)
65+
#### Are the results of the delete by partition key operation reflected immediately?
66+
Yes, once the delete by partition key operation starts, the documents to be deleted will not appear in the results of queries or read operations. This also means that you can write new a new document with the same ID and partition key as a document to be deleted without resulting in a conflict.
67+
68+
See [Known issues](#known-issues) for exceptions.
69+
70+
#### What happens if I issue a delete by partition key operation, and then immediately write a new document with the same partition key?
71+
When the delete by partition key operation is issued, only the documents that exist in the container at that point in time with the partition key value will be deleted. Any new documents that come in will not be in scope for the deletion.
72+
73+
### How is the delete by partition key operation prioritized among other operations against the container?
74+
By default, the delete by partition key value operation can consume up to a reserved fraction - 0.1, or 10% - of the overall RU/s on the resource. Any Request Units (RUs) in this bucket that are unused will be available for other non-background operations, such as reads, writes, and queries.
75+
76+
For example, suppose you have provisioned 1000 RU/s on a container. There is an ongoing delete by partition key operation that consumes 100 RUs each second for 5 seconds. During each of these 5 seconds, there are 900 RUs available for non-background database operations. Once the delete operation is complete, all 1000 RU/s are now available again.
77+
78+
### Known issues
79+
For certain scenarios, the effects of a delete by partition key operation is not guaranteed to be immediately reflected. The effect may be partially seen as the operation progresses.
80+
81+
- [Aggregate queries](sql-query-aggregate-functions.md) that use the index - for example, COUNT queries - that are issued during an ongoing delete by partition key operation may contain the results of the documents to be deleted. This may occur until the delete operation is fully complete.
82+
- Queries issued against the [analytical store](../analytical-store-introduction.md) during an ongoing delete by partition key operation may contain the results of the documents to be deleted. This may occur until the delete operation is fully complete.
83+
- [Continuous backup (point in time restore)](../continuous-backup-restore-introduction.md) - a restore that is triggered during an ongoing delete by partition key operation may contain the results of the documents to be deleted in the restored collection. It is not recommended to use this preview feature if you have a scenario that requires continuous backup.
84+
85+
## How to give feedback or report an issue/bug
86+
* Email [email protected] with questions or feedback.
87+
88+
### SDK requirements
89+
90+
Find the latest version of the SDK that supports this feature.
91+
92+
| SDK | Supported versions | Package manager link |
93+
| --- | --- | --- |
94+
| **.NET SDK v3** | *>= 3.25.0-preview (must be preview version)* | <https://www.nuget.org/packages/Microsoft.Azure.Cosmos/> |
95+
| **Java SDK v4** | *>= 4.19.0 (API is marked as beta)* | <https://mvnrepository.com/artifact/com.azure/azure-cosmos> |
96+
97+
Support for other SDKs is planned for the future.
98+
99+
## Next steps
100+
101+
See the following articles to learn about more SDK operations in Azure Cosmos DB.
102+
- [Query an Azure Cosmos container
103+
](how-to-query-container.md)
104+
- [Transactional batch operations in Azure Cosmos DB using the .NET SDK
105+
](transactional-batch.md)
109 KB
Loading

0 commit comments

Comments
 (0)