Skip to content
On this page

Cleaning up database

For the purpose of automated testing where you need to carefully control the state of the database, Marten supplies few helper functions.

Tearing Down Document Storage

Marten supplies the IDocumentCleaner service to quickly remove persisted document state or even to completely tear down the entire document storage.

This service is exposed as the IDocumentStore.Advanced.Clean property. You can see the usages of the document cleaner below:

public void clean_out_documents(IDocumentStore store)
    // Completely remove all the database schema objects related
    // to the User document type

    // Tear down and remove all Marten related database schema objects

    // Deletes all the documents stored in a Marten database

    // Deletes all of the persisted User documents

    // For cases where you may want to keep some document types,
    // but eliminate everything else. This is here specifically to support
    // automated testing scenarios where you have some static data that can
    // be safely reused across tests
    store.Advanced.Clean.DeleteDocumentsExcept(typeof(Company), typeof(User));

snippet source | anchor

Reset all data

Use IDocumentStore.Advanced.ResetAllData() to deletes all current document, event data and then (re)applies the configured initial data.

theStore.Advanced.InitialDataCollection.Add(new Users());

await theStore.Advanced.ResetAllData();

snippet source | anchor

Released under the MIT License.