Created
January 21, 2011 17:15
-
-
Save kowsik/790015 to your computer and use it in GitHub Desktop.
_bulk_delete
This file contains 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
# 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