How to delete all collections and documents in ArangoDb

I am trying to connect a unit test installation with Arango. To do this, I need to be able to reset the test database about all tests.

I know that we can directly remove the database from the REST API, but the documentation mentions that creating and deleting may "take some time."

Would this be the recommended way to do this setup, or is there an AQL statement to do something like this?

+6
source share
4 answers

You can, for example, get a list of all collections (except the system ones) and drop or truncate them. The latter will delete all documents and save indexes. Alternatively, you can use AQL REMOVE .

+4
source

Creating databases may take some time (several seconds). If it is too expensive to set up a unit test, which installs and disrupts the environment for each individual test, there are the following options:

  • create and delete the selected test database only once in the test set (which contains several tests) and create / delete the necessary collections for each test. In many cases, this turned out to be quite fast, but it depends on how many tests are contained in each test set.

  • Do not create or delete the selected test database, but only each test creates and deletes the necessary collections. This is the fastest option, and it should be good enough if you start each test run in a new database. However, this requires that the tests thoroughly clean everything. This is usually not a problem because tests will usually use dedicated collections. There is an exception for graphical data: creating a named graph will store the graph description in the _graphs collection, and the graph must be deleted again.

+2
source

After some problems with a similar need, I found this solution:

 for (let col of db._collections()) { if (!col.properties().isSystem) { db._drop(col._name); } } 
+2
source

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

 FOR u IN yourcollectionname REMOVE u IN yourcollectionname 

https://docs.arangodb.com/3.0/AQL/Operations/Remove.html

+1
source

All Articles