Instantly share code, notes, and snippets.

Embed
What would you like to do?
Delete ASpace locations
require 'json'
require 'rest_client'
require 'csv'
# this is a script to bulk delete locations in ArchivesSpace.
# To run this, you should have a recent version of ruby installed ( like > 1.9.3 ).
# Then, add the rest_client gem by using the command `gem install rest_client`
# To invoke the script, run simply run it with no parameters
# ( i.e. ruby delete_locations.rb )
# This will generate an output.csv file, which you can use to review all your
# locations in ASpace.
# You can use this file to select a subset of locations you want to delete.
# Simply make another spreadsheet with a column labeled "uri" with a list of
# uris you want removed.
# Then, rerun the script with an arguement pointing to your new CSV
# ( i.e. ruby delete_locations.rb delete_these.csv )
# To configure, change the values below to point to your ASpace backend
# and an admin username and password.
# Please note: you need to execute this script from a location that has acccess
# to the backend and is NOT firewalled off.
#
BACKEND_URL = "http://localhost:8089"
USER = "admin"
PASSWORD = "admin"
def get_all_locations
file = CSV.open("output.csv", 'wb', :headers => true)
file << ["uri", "building", "title", "coordinate_1_label", "coordinate_1_indicator", "created_by", "last_modified_by", "create_time", "system_mtime", "user_mtime", "temporary", "external_ids"]
locations = JSON.parse( RestClient.get "#{BACKEND_URL}/locations?all_ids=true", TOKEN )
locations.each do |location_id|
file << JSON.parse( RestClient.get "#{BACKEND_URL}/locations/#{location_id}", TOKEN )
end
puts "+" * 50
puts "LOCATION OUTPUT WRITTEN TO #{file.path}"
puts "Please review then rerun this script with version of the CSV file to delete"
puts "for example:"
puts "ruby delete_locations.rb list_to_delete.csv"
puts "+" * 50
end
def delete_locations(file)
uris = []
CSV.foreach(file, :headers => true) { |row| uris << row['uri'] }
puts "#{file} found. This script will delete all URIs in the URI column found in this file. Please enter 'YES' to continue. Otherwise, do something like CTL-C or whatnot to stop the script. "
response = $stdin.gets.chomp
if response == "YES"
uris.each do |uri|
begin
puts RestClient.delete("#{BACKEND_URL}#{uri}", TOKEN)
rescue RestClient::ResourceNotFound
puts "#{uri} NOT FOUND"
end
end
end
puts "done."
end
if __FILE__ == $0
TOKEN = { "X-ArchivesSpace-Session" => JSON.parse( RestClient.post "#{BACKEND_URL}/users/#{USER}/login", { password: PASSWORD} )["session"] }
file = ARGV[0]
if file && File.exists?(file)
delete_locations(file)
elsif file
puts "#{file} not found. Please provide a path to a CSV file with a URI column of locations to delete"
else
get_all_locations
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment