简体   繁体   中英

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 ?

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.

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);
    }
}

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.

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.0/AQL/Operations/Remove.html

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM