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

public 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
DatabaseDocument databaseDocument = new DatabaseDocument();
databaseDocument.setId("NewDatabase");
Map<String, String> settings = new HashMap<>();
settings.put("Raven/ActiveBundles", "PeriodicExport");
settings.put("Raven/DataDir", "~\\Databases\\NewDatabase");
databaseDocument.setSettings(settings);
store.getDatabaseCommands().getGlobalAdmin().createDatabase(databaseDocument);

DeleteDatabase

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

Syntax

public void deleteDatabase(String dbName);
public void deleteDatabase(String dbName, boolean hardDelete);
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.getDatabaseCommands().getGlobalAdmin().deleteDatabase("NewDatabase", true);

EnsureDatabaseExists

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

Syntax

public void ensureDatabaseExists(String name);
public void ensureDatabaseExists(String name, boolean ignoreFailures);
Parameters
name String Name of a database that will be created if it does not exist
ignoreFailures boolean Ignore any exceptions that could have occurred during database creation. Default: false

Example

store.getDatabaseCommands().getGlobalAdmin().ensureDatabaseExists("NewDatabase", false);