Skip to main content

Commands: How to create or delete database?

This article will describe the following commands (and extensions) that enable you to manage databases on a server:

CreateDatabase

This method is used to create a new database on a server.

Syntax

void CreateDatabase(DatabaseDocument databaseDocument);
Parameters
databaseDocumentDatabaseDocumentA document containing all configuration options for new database (e.g. active bundles, name/id, path to data)

Example

// create database 'NewDatabase' with 'PeriodicExport' bundle enabled
store
.DatabaseCommands
.GlobalAdmin
.CreateDatabase(new DatabaseDocument
{
Id = "NewDatabase",
Settings =
{
{ "Raven/ActiveBundles", "PeriodicExport" },
{ "Raven/DataDir", @"~\Databases\NewDatabase" }
}
});

DeleteDatabase

This method is used to delete a database from a server, with a possibility to remove all the data from hard drive.

Syntax

void DeleteDatabase(string dbName, bool hardDelete = false);
Parameters
dbNamestringName of a database to delete
hardDeleteboolShould all data be removed (data files, indexing files, etc.). Default: false

Example

store.DatabaseCommands.GlobalAdmin.DeleteDatabase("NewDatabase", hardDelete: true);

EnsureDatabaseExists - extension method

This extension method creates database on a server with default configuration and indexes if that database does not exist.

Syntax

// required namespace in usings
public static void EnsureDatabaseExists(
this IGlobalAdminDatabaseCommands self,
string name,
bool ignoreFailures = false)
{
throw new CodeOmitted();
}
Parameters
namestringName of a database that will be created if it does not exist
ignoreFailuresboolIgnore any exceptions that could heve occurred during database creation. Default: false

Example

store
.DatabaseCommands
.GlobalAdmin
.EnsureDatabaseExists("NewDatabase", ignoreFailures: false);