I'm trying to write a simple ruby script that delete all items in a DynamoDB table, but I'm having trouble understand which argument to pass to "delete_items", this is what I have so far:
dynamoDB = Aws::DynamoDB::Resource.new(region: 'us-west-2')
dynamoDB.tables.each do |table|
puts "Table #{table.name}"
scan_output = table.scan({
select: "ALL_ATTRIBUTES"
})
scan_output.items.each do |item|
keys = item.keys
table.delete_item({
key: ???
})
end
end
I tried passing the item, or item.keys - both did not work.
Thanks!
Here is the code to scan and delete all the items from DynamoDB table though I am not sure why can't you delete the table and recreate if you would like to delete all the items from a table.
Please note that this is not a recommended approach unless you have some very specific use case. This would cost you as the code reads the item from table and then delete the item.
Code:-
You may need to change the table name and key value in the below code. In the below code, the table name used is files and its key value is fileName.
If you have both partition key and sort key, then you need to set both the values. The files table has only partition key.
#! /usr/bin/ruby
require "aws-sdk-core"
# Configure SDK
# use credentials file at .aws/credentials
Aws.config[:credentials] = Aws::SharedCredentials.new
Aws.config[:region] = "us-west-2"
# point to DynamoDB Local, comment out this line to use real DynamoDB
Aws.config[:dynamodb] = { endpoint: "http://localhost:8000" }
dynamodb = Aws::DynamoDB::Client.new
tableName = "files"
scanParams = {
table_name: tableName
}
puts "Scanning files table."
begin
loop do
result = dynamodb.scan(scanParams)
result.items.each{|files|
puts "Item :" + "#{files}"
puts "Going to delete item :" + "#{files["fileName"]}"
deleteParams = {
table_name: tableName,
key: {
fileName: files["fileName"]
}
}
begin
deleteResult = dynamodb.delete_item(deleteParams)
puts "Deleted item." + files["fileName"]
rescue Aws::DynamoDB::Errors::ServiceError => error
puts "Unable to delete item:"
puts "#{error.message}"
end
}
break if result.last_evaluated_key.nil?
puts "Scanning for more..."
scanParams[:exclusive_start_key] = result.last_evaluated_key
end
rescue Aws::DynamoDB::Errors::ServiceError => error
puts "Unable to scan:"
puts "#{error.message}"
end
I eventually wrote this script which delete all records from all tables (not very useful for most cases, but for mine it was exactly what I needed, as I was using it in a dedicated testing account):
#!/usr/bin/env ruby
require 'aws-sdk'
dynamoDB = Aws::DynamoDB::Resource.new(region: 'us-west-2')
dynamoDB.tables.each do |table|
puts "Table #{table.name}"
scan_output = table.scan({
select: "ALL_ATTRIBUTES"
})
scan_output.items.each do |item|
item_key = Hash.new
table.key_schema.each do |k|
item_key[k.attribute_name] = item[k.attribute_name]
end
table.delete_item({
key: item_key
})
end
end
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With