Created
March 17, 2011 06:51
-
-
Save ryankirkman/873942 to your computer and use it in GitHub Desktop.
Delete all non-design docs in CouchDB (using cradle)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | |
}); | |
} | |
} | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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);
});