Skip to content

Instantly share code, notes, and snippets.

@isc-rsingh
Last active August 29, 2015 14:08
Show Gist options
  • Save isc-rsingh/6044d58e2ae743d7ec5b to your computer and use it in GitHub Desktop.
Save isc-rsingh/6044d58e2ae743d7ec5b to your computer and use it in GitHub Desktop.
NodeJS app to delete all documents in the given Cloudant database except for design documents
/**
* deletes all records in the given database except for design documents
*/
var Cloudant = require('cloudant')
if ( process.argv.length < 4) {
console.warn("usage: node deleteall.js <account_name> <database_name>");
process.exit(1);
}
var password = process.env.cloudant_password // at the shell type export cloudant_password="pwhere"
if ( !password ) {
console.warn("First set your database password by typing: export cloudant_password='<type_real_password_not_this>' at the command line.");
process.exit(1);
}
var me = process.argv[2]; // Set this to your own account
var dbname = process.argv[3];
Cloudant({account:me, password:password}, function(er, cloudant) {
if (er)
return console.log('Error connecting to Cloudant account %s: %s', me, er.message)
// specify the database we are going to use
var ldb = cloudant.db.use(dbname)
// ldb.list({"include_docs":true}, function(er, body) {
ldb.list(function(er, body) {
if (er)
return console.log('Error listing docs')
body.rows.forEach(function(doc) {
if ( doc.id.substring(0,7) != '_design') {
console.log('deleting id: %s, rev: %s', doc.id, doc.value.rev)
ldb.destroy(doc.id, doc.value.rev, function(er, body){
if (er) console.log('ERROR: %s', er)
else console.log(body)
})
}
})
})
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment