You can drop a database or databases from the portal in addition to from the code by the usage of .Net SDK. Here, we can speak, in a step-wise manner, how to drop a database in DocumentDB.
Step 1 − Go in your DocumentDB account on Azure portal. For the purpose of demo, I have brought two more databases as seen inside the following screenshot.
Step 2 − To drop any database, you need to click that database. Let’s select tempdb, you may see the following page, pick out the ‘Delete Database’ choice.
Step 3− It will show the affirmation message, now click on the ‘Yes’ button.
You will see that the tempdb is no more available on your dashboard.
You also can delete databases out of your code using .Net SDK. To do following are the steps.
Step 1 − Let's delete the database via specifying the ID of the database we need to delete, but we want its SelfLink.
Step 2 − We are calling the CreateDatabaseQuery like before, however this time we are genuinely supplying a question to return simply the one database with the ID tempdb1.
private async static Task DeleteDatabase(DocumentClient client) {
Console.WriteLine("******** Delete Database ********");
Database database = client
.CreateDatabaseQuery("SELECT * FROM c WHERE c.id = 'tempdb1'")
.AsEnumerable()
.First();
await client.DeleteDatabaseAsync(database.SelfLink);
}
Step 3 − This time, we can name AsEnumerable instead of ToList() because we don't really need a list item. Expecting most effective result, calling AsEnumerable is sufficient so that we are able to get the primary database object returned through the query with First(). This is the database object for tempdb1 and it has a SelfLink that we will use to name DeleteDatabaseAsync which deletes the database.
Step 4− You additionally want to name DeleteDatabase undertaking from the CreateDocumentClient project after DocumentClient is instantiated.
Step 5− To view the listing of databases after deleting the required database, permit’s call GetDatabases technique again.
The use of (var purchaser = new DocumentClient(new Uri(EndpointUrl), AuthorizationKey))
using (var client = new DocumentClient(new Uri(EndpointUrl), AuthorizationKey)) {
//await CreateDatabase(client);
GetDatabases(client);
await DeleteDatabase(client);
GetDatabases(client);
}
Following is the complete Program.Cs record to this point.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Microsoft.Azure.Documents;
using Microsoft.Azure.Documents.Client;
using Microsoft.Azure.Documents.Linq;
using Newtonsoft.Json;
namespace DocumentDBDemo {
class Program {
private const string EndpointUrl = "https://azuredocdbdemo.documents.azure.com:443/";
private const string AuthorizationKey = "BBhjI0gxdVPdDbS4diTjdloJq7Fp4L5RO/
StTt6UtEufDM78qM2CtBZWbyVwFPSJIm8AcfDu2O+AfV T+TYUnBQ==";
static void Main(string[] args) {
try {
CreateDocumentClient().Wait();
} catch (Exception e) {
Exception baseException = e.GetBaseException();
Console.WriteLine("Error: {0}, Message: {1}", e.Message, baseException.Message);
}
Console.ReadKey();
}
private static async Task CreateDocumentClient() {
// Create a new instance of the DocumentClient
using (var client = new DocumentClient(new Uri(EndpointUrl), AuthorizationKey)) {
//await CreateDatabase(client);
GetDatabases(client);
await DeleteDatabase(client);
GetDatabases(client);
}
}
private async static Task CreateDatabase(DocumentClient client) {
Console.WriteLine();
Console.WriteLine("******** Create Database *******");
var databaseDefinition = new Database { Id = "mynewdb" };
var result = await client.CreateDatabaseAsync(databaseDefinition);
var database = result.Resource;
Console.WriteLine(" Database Id: {0}; Rid: {1}",
database.Id, database.ResourceId);
Console.WriteLine("******** Database Created *******");
}
private static void GetDatabases(DocumentClient client) {
Console.WriteLine();
Console.WriteLine();
Console.WriteLine("******** Get Databases List ********");
var databases = client.CreateDatabaseQuery().ToList();
foreach (var database in databases) {
Console.WriteLine(" Database Id: {0}; Rid: {1}", database.Id,
database.ResourceId);
}
Console.WriteLine();
Console.WriteLine("Total databases: {0}", databases.Count);
}
private async static Task DeleteDatabase(DocumentClient client) {
Console.WriteLine();
Console.WriteLine("******** Delete Database ********");
Database database = client
.CreateDatabaseQuery("SELECT * FROM c WHERE c.id = 'tempdb1'")
.AsEnumerable()
.First();
await client.DeleteDatabaseAsync(database.SelfLink);
}
}
}
When the above code is compiled and performed, you will acquire the following output which contains the Database and Resources IDs of the three databases and general wide variety of databases.
******** Get Databases List ********
Database Id: myfirstdb; Rid: Ic8LAA==
Database Id: mynewdb; Rid: ltpJAA==
Database Id: tempdb1; Rid: 06JjAA==
Total databases: 3
******** Delete Database ********
******** Get Databases List ********
Database Id: myfirstdb; Rid: Ic8LAA==
Database Id: mynewdb; Rid: ltpJAA==
Total databases: 2
After deleting the database, you'll additionally see on the end that handiest databases are left in DocumentDB account.