diff --git a/tools/purge_archived_storages.rb b/tools/purge_archived_storages.rb new file mode 100755 index 00000000000..5111e8e1e23 --- /dev/null +++ b/tools/purge_archived_storages.rb @@ -0,0 +1,28 @@ +#!/usr/bin/env ruby +require File.expand_path('../config/environment', __dir__) +require "optimist" + +opts = Optimist.options do + opt :dry_run, "Just print out what would be done without modifying anything", :type => :boolean, :default => true +end + +if opts[:dry_run] + puts "\n" + puts "* This is a dry run and will not modify the database" + puts "* To actually delete archived datastores pass --no-dry-run\n\n" +end + +active_storage_ids = HostStorage.pluck(:storage_id).uniq +archived_storages = Storage.where.not(:id => active_storage_ids).pluck(:id, :name) + +if archived_storages.empty? + puts "No archived storages found" +else + puts "Deleting the following storages:" + puts archived_storages.map { |id, name| "ID [#{id}] Name [#{name}]" }.join("\n") +end + +return if opts[:dry_run] + +archived_storage_ids = archived_storages.map(&:first) +Storage.destroy(archived_storage_ids)