Wednesday, 14 July 2021

ArangoDB: Drop a collection

There are two approaches to drop a collection.

a.   Using database _drop() method

b.   Using collection drop method

 

Using database _drop() method

‘db._drop()’ method is used to drop a collection including the data and it’s indexes.

 

Signature

db._drop(collection)

db._drop(collection-identifier)

db._drop(collection-name, options)

 

To drop a system collection, you must pass an options object with attribute isSystem set to true.

 

Let’s experiment with an example.

 

Step 1: Login to arango shell.

$arangosh
Please specify a password: 

                                       _     
  __ _ _ __ __ _ _ __   __ _  ___  ___| |__  
 / _` | '__/ _` | '_ \ / _` |/ _ \/ __| '_ \ 
| (_| | | | (_| | | | | (_| | (_) \__ \ | | |
 \__,_|_|  \__,_|_| |_|\__, |\___/|___/_| |_|
                       |___/                 

arangosh (ArangoDB 3.7.11 [darwin] 64bit, using build , VPack 0.1.33, RocksDB 6.8.0, ICU 64.2, V8 7.9.317, OpenSSL 1.1.1k  25 Mar 2021)
Copyright (c) ArangoDB GmbH

Command-line history will be persisted when the shell is exited. You can use `--console.history false` to turn this off
Connected to ArangoDB 'http+tcp://127.0.0.1:8529, version: 3.7.11 [SINGLE, server], database: '_system', username: 'root'

Type 'tutorial' for a tutorial or 'help' to see common examples
127.0.0.1:8529@_system>

Step 2: Create a database ‘demo’.

127.0.0.1:8529@_system> db._createDatabase("demo")
true

127.0.0.1:8529@_system> db._databases()
[ 
  "_system", 
  "demo", 
  "example" 
]


Step 3: Create collections user and _mySystemCollection.

db._create("user")

db._create("_mySystemCollection", {isSystem : true})


127.0.0.1:8529@_system> db._useDatabase("demo")
true

127.0.0.1:8529@demo> db._create("user")
[ArangoCollection 10119, "user" (type document, status loaded)]

127.0.0.1:8529@demo> db._create("_mySystemCollection", {isSystem : true})
[ArangoCollection 10124, "_mySystemCollection" (type document, status loaded)]

127.0.0.1:8529@demo> db._collections()
[ 
  [ArangoCollection 10043, "_analyzers" (type document, status loaded)], 
  [ArangoCollection 10058, "_appbundles" (type document, status loaded)], 
  [ArangoCollection 10055, "_apps" (type document, status loaded)], 
  [ArangoCollection 10046, "_aqlfunctions" (type document, status loaded)], 
  [ArangoCollection 10067, "_fishbowl" (type document, status loaded)], 
  [ArangoCollection 10061, "_frontend" (type document, status loaded)], 
  [ArangoCollection 10040, "_graphs" (type document, status loaded)], 
  [ArangoCollection 10052, "_jobs" (type document, status loaded)], 
  [ArangoCollection 10064, "_modules" (type document, status loaded)], 
  [ArangoCollection 10124, "_mySystemCollection" (type document, status loaded)], 
  [ArangoCollection 10049, "_queues" (type document, status loaded)], 
  [ArangoCollection 10119, "user" (type document, status loaded)] 
]


Step 4: Drop the user collection.

127.0.0.1:8529@demo> db._drop("user")

127.0.0.1:8529@demo> db._collections()
[ 
  [ArangoCollection 10043, "_analyzers" (type document, status loaded)], 
  [ArangoCollection 10058, "_appbundles" (type document, status loaded)], 
  [ArangoCollection 10055, "_apps" (type document, status loaded)], 
  [ArangoCollection 10046, "_aqlfunctions" (type document, status loaded)], 
  [ArangoCollection 10067, "_fishbowl" (type document, status loaded)], 
  [ArangoCollection 10061, "_frontend" (type document, status loaded)], 
  [ArangoCollection 10040, "_graphs" (type document, status loaded)], 
  [ArangoCollection 10052, "_jobs" (type document, status loaded)], 
  [ArangoCollection 10064, "_modules" (type document, status loaded)], 
  [ArangoCollection 10124, "_mySystemCollection" (type document, status loaded)], 
  [ArangoCollection 10049, "_queues" (type document, status loaded)] 
]


Step 5: Drop the collection "_mySystemCollection". To drop a system collection, you should pass attribute ‘isSystem’ to true in options argument.

 

Let’s try to drop a collection excluding isSystem attribute.

127.0.0.1:8529@demo> db._drop("_mySystemCollection")
JavaScript exception in file '/usr/local/Cellar/arangodb/3.7.11_1/share/arangodb3/js/client/modules/@arangodb/arangosh.js' at 99,7: ArangoError 11: forbidden
!      throw error;
!      ^
stacktrace: ArangoError: forbidden
    at Object.exports.checkRequestResult (/usr/local/Cellar/arangodb/3.7.11_1/share/arangodb3/js/client/modules/@arangodb/arangosh.js:97:21)
    at ArangoCollection.drop (/usr/local/Cellar/arangodb/3.7.11_1/share/arangodb3/js/client/modules/@arangodb/arango-collection.js:505:14)
    at Proxy.ArangoDatabase._drop (/usr/local/Cellar/arangodb/3.7.11_1/share/arangodb3/js/client/modules/@arangodb/arango-database.js:461:29)
    at <shell command>:1:4


As you see above output, you got forbidden error while deleting the system collection. Let’s pass isSystem attribute and delete the collection.

127.0.0.1:8529@demo> db._drop("_mySystemCollection", {isSystem : true})

127.0.0.1:8529@demo> db._collections()
[ 
  [ArangoCollection 10043, "_analyzers" (type document, status loaded)], 
  [ArangoCollection 10058, "_appbundles" (type document, status loaded)], 
  [ArangoCollection 10055, "_apps" (type document, status loaded)], 
  [ArangoCollection 10046, "_aqlfunctions" (type document, status loaded)], 
  [ArangoCollection 10067, "_fishbowl" (type document, status loaded)], 
  [ArangoCollection 10061, "_frontend" (type document, status loaded)], 
  [ArangoCollection 10040, "_graphs" (type document, status loaded)], 
  [ArangoCollection 10052, "_jobs" (type document, status loaded)], 
  [ArangoCollection 10064, "_modules" (type document, status loaded)], 
  [ArangoCollection 10049, "_queues" (type document, status loaded)] 
]


Using collection drop method

‘collection.drop()’ method is used to drop a collection, data and all its indexes.

 

Syntax

collection.drop(options)

 

Example

col.drop();

col.drop({ isSystem: true });

 

To drop a system collection, you must set the attribute ‘isSystem’ to true in options argument.

 

Let’s experiment with an example.

 

Step 1: Create two collections ‘person’ and ‘_mySystemCollection2’.

 

127.0.0.1:8529@demo> db._create("person")
[ArangoCollection 10451, "person" (type document, status loaded)]

127.0.0.1:8529@demo> db._create("_mySystemCollection2", {isSystem: true})
[ArangoCollection 10516, "_mySystemCollection2" (type document, status loaded)]

127.0.0.1:8529@demo> db._collections()
[ 
  [ArangoCollection 10043, "_analyzers" (type document, status loaded)], 
  [ArangoCollection 10058, "_appbundles" (type document, status loaded)], 
  [ArangoCollection 10055, "_apps" (type document, status loaded)], 
  [ArangoCollection 10046, "_aqlfunctions" (type document, status loaded)], 
  [ArangoCollection 10067, "_fishbowl" (type document, status loaded)], 
  [ArangoCollection 10061, "_frontend" (type document, status loaded)], 
  [ArangoCollection 10040, "_graphs" (type document, status loaded)], 
  [ArangoCollection 10052, "_jobs" (type document, status loaded)], 
  [ArangoCollection 10064, "_modules" (type document, status loaded)], 
  [ArangoCollection 10516, "_mySystemCollection2" (type document, status loaded)], 
  [ArangoCollection 10049, "_queues" (type document, status loaded)], 
  [ArangoCollection 10451, "person" (type document, status loaded)] 
]


Step 2: Drop the collection person.

127.0.0.1:8529@demo> var userColleciton = db._collection("person")

127.0.0.1:8529@demo> userColleciton.drop()

127.0.0.1:8529@demo> db._collections()
[ 
  [ArangoCollection 10043, "_analyzers" (type document, status loaded)], 
  [ArangoCollection 10058, "_appbundles" (type document, status loaded)], 
  [ArangoCollection 10055, "_apps" (type document, status loaded)], 
  [ArangoCollection 10046, "_aqlfunctions" (type document, status loaded)], 
  [ArangoCollection 10067, "_fishbowl" (type document, status loaded)], 
  [ArangoCollection 10061, "_frontend" (type document, status loaded)], 
  [ArangoCollection 10040, "_graphs" (type document, status loaded)], 
  [ArangoCollection 10052, "_jobs" (type document, status loaded)], 
  [ArangoCollection 10064, "_modules" (type document, status loaded)], 
  [ArangoCollection 10516, "_mySystemCollection2" (type document, status loaded)], 
  [ArangoCollection 10049, "_queues" (type document, status loaded)] 
]


Step 3: Drop the system collection _mySystemCollection2.

127.0.0.1:8529@demo> var sysColleciton = db._collection("_mySystemCollection2")

127.0.0.1:8529@demo> sysColleciton.drop({isSystem: true})

127.0.0.1:8529@demo> db._collections()
[ 
  [ArangoCollection 10043, "_analyzers" (type document, status loaded)], 
  [ArangoCollection 10058, "_appbundles" (type document, status loaded)], 
  [ArangoCollection 10055, "_apps" (type document, status loaded)], 
  [ArangoCollection 10046, "_aqlfunctions" (type document, status loaded)], 
  [ArangoCollection 10067, "_fishbowl" (type document, status loaded)], 
  [ArangoCollection 10061, "_frontend" (type document, status loaded)], 
  [ArangoCollection 10040, "_graphs" (type document, status loaded)], 
  [ArangoCollection 10052, "_jobs" (type document, status loaded)], 
  [ArangoCollection 10064, "_modules" (type document, status loaded)], 
  [ArangoCollection 10049, "_queues" (type document, status loaded)] 
]

 

 

Previous                                                    Next                                                    Home

1 comment: