Skip to content

Instantly share code, notes, and snippets.

@kowsik
Created January 21, 2011 17:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kowsik/790015 to your computer and use it in GitHub Desktop.
Save kowsik/790015 to your computer and use it in GitHub Desktop.
_bulk_delete
# Assuming the docs have created_at, delete everything older than
# 5 minutes. We assume an emit like this:
#
# emit(Date.parse(doc.created_at), doc._rev)
#
# Notice that the value in emitted kv pair is the doc's current _rev
old = (Time.now - 300).utc.to_i * 1000
opts = { :startkey => old, :descending => true }
# Directly map the results of the view into a bulk delete items. This
# is fast in that it also doesn't fetch the entire doc. Just the id and
# and the _rev
docs = DB.view 'docs/by_created_at', opts do |row|
{ '_id' => row['id'], '_rev' => row['value'], '_deleted' => true }
end
DB.bulk_delete docs, false
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment