11

Is there any way that I can drop all data from a CouchDB database?

What I'm doing currently is dropping and re-creating the whole database

curl -X DELETE http://localhost:5984/foobar
curl -X PUT    http://localhost:5984/foobar

but I'm not sure if this is the best way to do this.

Jakub Arnold
  • 85,596
  • 89
  • 230
  • 327
  • If it is a dev environment its best approach, I am doing the same. If it is not a dev environment, and a user with regular access is able to do it, then its a serious issue. – GLK Mar 20 '19 at 03:11

7 Answers7

8

I found this bookmarklet that adds you features to the Futon view. It adds you select all, delete all buttons and a delete column with delete checkboxes. It could be a good start, but you might want to modify it a bit since it doesn't seem to work all the time.

http://www.elijahmanor.com/couch-potato-bookmarklet-lazy-features-for-couchdbs-futon/

Niels Abildgaard
  • 2,662
  • 3
  • 24
  • 32
airpaulg
  • 565
  • 3
  • 13
5

The code below deletes all databases (not all records!) using Node.js. You need to install nano and after that execute following code:

var nano = require('nano')('http://localhost:5984');

nano.db.list(function(err, body) {
  body.forEach(function(db) {
     nano.db.destroy(db);
  });
});
giacecco
  • 661
  • 1
  • 5
  • 8
Rez
  • 650
  • 7
  • 5
4

There is no way to drop Data except deleting each doc (or updating a bunch of known doc-id's with _rev=xxx and "_deleted:true" in a _bulk)

Deleting and recreating is ok.

Devid Farinelli
  • 7,514
  • 9
  • 42
  • 73
okurow
  • 969
  • 10
  • 6
3

Here is a python script to do the job:

import couchdb
couch = couchdb.Server('http://localhost:5984/')
couchdb = 'DATABASE'
db = couch[couchdb]
for id in db:
    db.delete(db[id])
amagard
  • 339
  • 3
  • 8
0

I made a command line tool to do some operations on my CouchDB server besides the ones given by Futon. It supports delete, backup and restore of documents for now.

You can find it here.

Cristiano Santos
  • 2,157
  • 2
  • 35
  • 53
0

You can use this pouch db plugin. What it does is erase all the documents in your pouch db. It can be used both on server and client side javascript applications. Here is the link : Pouch Db Erase Plugin

kartik
  • 583
  • 8
  • 24
0

Thanks to amagards' answer, I wrote a small Python script for deleting all documents and compacting the database afterwards:

import couchdb

user = "user"
passwd = "pass"
dbName = "databaseName"

couch = couchdb.Server("http://%s:%s@localhost:5984/" % (user, passwd))
db = couch[dbName]

print("start delete and compact routine")
count = 0
while True:
    print("requesting next 100 documents...")
    items = db.view("_all_docs", limit=100)
    if len(items) == 0:
        print("no documents available")
        break;

    for item in items:
        count += 1
        documentID = item.id.encode("utf-8")
        print ("deleting document: %s (count: %d)" % (documentID, count))
        db.delete(db[documentID])

print("deleted %d documents, requesting compact" % (count))
db.compact();
print("finished");

Shrunk my 10GBs large logging-DB downto 40MB but took a whole night running.

Rüdiger
  • 1,674
  • 1
  • 20
  • 28