Nicolas Joseph
Nicolas Joseph

Reputation: 1724

How to delete all collections and documents in ArangoDb

I am trying to put together a unit test setup with Arango. For that I need to be able to reset the test database around every test.

I know we can directly delete a database from the REST API but it is mentioned in the documentation that creation and deletion can "take a while".

Would that be the recommended way to do that kind of setup or is there an AQL statement to do something similar ?

Upvotes: 12

Views: 12553

Answers (4)

musemind
musemind

Reputation: 1055

Execute the following AQL query deletes all documents in the collection yourcollectionname:

FOR u IN yourcollectionname
  REMOVE u IN yourcollectionname

https://docs.arangodb.com/3.11/aql/high-level-operations/remove/

Upvotes: 3

yojimbo87
yojimbo87

Reputation: 68305

You can for example retrieve the list of all collections (excluding system ones) and drop or truncate them. The latter will remove all documents and keep indexes. Alternatively, you can use AQL REMOVE statement.

Upvotes: 7

tlama
tlama

Reputation: 617

After some struggling with similar need I have found this solution:

for (let col of db._collections()) {

    if (!col.properties().isSystem) {
        db._drop(col._name);
    }
}

Upvotes: 10

stj
stj

Reputation: 9097

Creation of databases may indeed take a while (a few seconds). If that's too expensive in a unit test setup that sets up and tears down the environment for each single test, there are the following options:

  • create and drop a dedicated test database only once per test suite (that contains multiple tests), and create/drop the required collections per test. This has turned out to be fast enough in many cases, but it depends on how many tests are contained in each test suite.

  • do not create and drop a dedicated test database, but only have each test create and drop the required collections. This is the fastest option, and should be good enough if you start each test run in a fresh database. However it requires the tests to clean everything up properly. This is normally no problem, because the tests will normally use dedicated collections anyway. An exception is there for graph data: creating a named graph will store the graph description in the _graphs collection, and the graph must be deleted from there again.

Upvotes: 2

Related Questions