Forked from KlausTrainer/delete_documents_from_view_result.rb
Created
October 4, 2012 00:55
-
-
Save ernestom/3830876 to your computer and use it in GitHub Desktop.
Delete all documents whose id appears in a given view result.
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
#!/usr/bin/env ruby | |
require 'rubygems' | |
require 'rest_client' | |
require 'json' | |
require 'cgi' | |
def bye | |
abort("Usage: #{$0} [view_url]\nExample: #{$0} http://admin:[email protected]:5984/db/_design/ddoc/_view/recent-posts") | |
end | |
def delete_documents_from_view_result(url) | |
case url | |
when /^(https?:\/\/.+?\/.+?)\/_design\/.+?\/_view\/.+/ | |
db_url = $1 | |
end | |
unless db_url | |
bye | |
end | |
json = JSON.parse(RestClient.get(url)) | |
json["rows"].each do |row| | |
doc_url = db_url + '/' + CGI.escape(row["id"]) | |
begin | |
rev = RestClient.head(doc_url).headers[:etag].gsub('"', '') | |
RestClient.delete(doc_url + '?rev=' + rev) | |
rescue Exception | |
puts "warning: could not delete '#{doc_url}'" | |
next | |
end | |
end | |
end | |
unless ARGV[0] | |
bye | |
end | |
delete_documents_from_view_result(ARGV[0]) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment