Tool for deleting all records matched by a view?


#1

Every once in a while I get bad data into our couchbase database. Usually, it’s just a few records. When it happens, I create a dev view over the entire dataset that matches all affected records and then manually delete them through the UI. It’s a bit tedious, but it works. However, I was wondering if someone already built a tool that could be given a view and then would automatically enumerate all matching records and delete them. If none exists, I might just build one and share it, but I’d rather not duplicate effort. Or maybe there is a better way to handle this?


#2

I ended up creating my own scripts using NodeJS and cradle for the exact same problem. I haven’t found a better way to handle this yet.
Below is the minimal code needed. A few things to keep in mind:

  • You need to npm install requirejs and cradle
  • Cradle is meant for couchdb so you’re only talking to a single node not the cluster
  • This code is written in coffeescript so you need to compile it first
    Sorry I don’t have a more complete answer for you but I hope it helps you

    requirejs = require “requirejs”

requirejs.config(
nodeRequire: require
baseUrl: “…/lib”
)

requirejs( [ “cradle” ], ( cradle ) ->
console.log( “[WARNING] Deleting documents…” )

# Use with care!
#
connection = new( cradle.Connection )( "127.0.0.1", 8092 )
database   = connection.database( "mydb" )

params =
    stale:              false
    include_docs: true

database.view( "mydesign/myview", params, ( error, results ) ->
    console.log( "Found #{results.total_rows} document(s)" )

    if error
        console.error( "Failed to retrieve view", error )
        process.exit()

    else
        for result in results
            doc = result.doc.meta
            console.log( "Removing doc #{doc.id}" )
            database.remove( doc.id, doc.rev )
            console.log( "Removed doc #{doc.id}" )
)

)