Skip to content

Instantly share code, notes, and snippets.

@ryankirkman
Created March 17, 2011 06:51
Show Gist options
  • Save ryankirkman/873942 to your computer and use it in GitHub Desktop.
Save ryankirkman/873942 to your computer and use it in GitHub Desktop.
Delete all non-design docs in CouchDB (using cradle)
var cradle = require('cradle');
var database = 'app';
cradle.setup({
host: '127.0.0.1',
port: 5984,
auth: { username: "YOUR_USERNAME", password: "YOUR_PASSWORD" }
});
var db = new(cradle.Connection)().database(database);
/* Delete non-design documents in a database. */
db.all(function(err, doc) {
/* Loop through all documents. */
for(var i = 0; i < doc.length; i++) {
/* Don't delete design documents. */
if(doc[i].id.indexOf("_design") == -1) {
db.remove(doc[i].id, doc[i].value.rev, function(err, doc) {
console.log(doc);
});
}
}
});
@pulkitsinghal
Copy link

nice!

@blissdev
Copy link

blissdev commented Nov 9, 2012

This was crazy helpful in a time of need, thank you!

@dmitrymatveev
Copy link

Using couchDB bulk documentation we could also do this in one go (be ware of conflicts though as with all bulk operations).

db.view( 'design/_all', { include_docs: true }, function (err, res) {

var docs = [];
for(i in res) {
var doc = res[i].doc;
doc._deleted = true;
docs.push(doc);
}

if ( docs.length > 1 ) db.save(docs);
});

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment