DocumentDB Delete Collection - DocumentDB

How Collection can be Deleted in DocumentDB?

The collection or collections can be deleted by doing the same from the portal as well as from the code by using .Net SDK.

Step 1 : On Azure portal go to DocumentDB account. The two more collections are added to show the demo as shown in the following figure.

delete collection

Step 2 : Click on that collection which you want to delete. Select TempCollection1. The following page will be displayed, select the ‘Delete Collection’ option.

select collection

Step 3 : The confirmation message will be displayed. Now click ‘Yes’ button.

delete collection message

From the above figure you can see that, the TempCollection1 is not available on your dashboard.

collection deleted

The collections can also be deleted from your code using .Net SDK. To do these follow the steps.

Step 1 : By specifying the ID of the collection you can delete the collection which you want to delete.
It's the usual pattern of querying by Id to obtain the selfLinks needed to delete a resource.

  • The preferred way of constructing a parameterized query is shown. We're not hardcoding the collectionId so this method is used to delete any collection.
  • For a specific collection we are querying by Id where the Id parameter is defined in this SqlParameterCollection assigned to the parameter's property of this SqlQuerySpec.
  • Now, the SDK does the work of constructing the final query string for DocumentDB with the collectionId embedded inside of it.

Step 2 : Run the query and its SelfLink is used to delete the collection from the CreateDocumentClient task.

The complete implementation of Program.cs files as follows.

On compiling and executing the above code the following result is produced:

All rights reserved © 2020 Wisdom IT Services India Pvt. Ltd Protection Status

DocumentDB Topics