You are currently browsing legacy 3.0 version of documentation. Click here to switch to the newest 4.2 version.

We can help you with migration to the latest RavenDB

Contact Us Now
see on GitHub

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
- DeleteDatabase
- EnsureDatabaseExists

CreateDatabase

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

Syntax

void CreateDatabase(DatabaseDocument databaseDocument);

Parameters
databaseDocument DatabaseDocument A 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
dbName string Name of a database to delete
hardDelete bool Should 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
name string Name of a database that will be created if it does not exist
ignoreFailures bool Ignore any exceptions that could heve occurred during database creation. Default: false

Example

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