Delete all items in DynamoDB table using Ruby

10,744

Solution 1

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

Solution 2

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
Share:
10,744
duduamar
Author by

duduamar

Updated on June 04, 2022

Comments

  • duduamar
    duduamar almost 2 years

    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!

  • duduamar
    duduamar about 7 years
    You assume I know the table name, while I would like to delete all records from all tables. I will post the script I eventually used.