user3202457
user3202457

Reputation: 145

How to remove all data from DynamoDB without for loop?

Suppose i have 100 data in DynamoDB then to remove all data i have to execute for loop .So is there any way to remove all data without for loop ?

let dynamoDbObjectMapper = AWSDynamoDBObjectMapper.default()

for var key in 0 ..<100 {

    let itemToDelete: CategoriesDB = CategoriesDB()
    itemToDelete._index = key

    dynamoDbObjectMapper.remove(itemToDelete, completionHandler:              
    {(error: Error?) -> Void in
        if let error = error {
            print(" Amazon DynamoDB Save Error: \(error)")
            return
        }
        print("A Category was deleted.")
    })
}

Upvotes: 2

Views: 515

Answers (2)

Vikdor
Vikdor

Reputation: 24134

If you want to remove all the data in a DynamoDB table, it is better to just delete the table and create it again as that wouldn't cost Write Capacity Units.

Upvotes: 1

F_SO_K
F_SO_K

Reputation: 14869

Ignoring the swift SDK for a moment, DynamoDB has the DeleteItem method to remove a single item from a table, but it also has a batch method.

Quote

In addition to DeleteItem, Amazon DynamoDB supports a BatchWriteItem action for deleting multiple items at the same time.

The problem is, I can't see a way to access BatchWriteItem through the swift SDK. Even if you could use BatchWriteItem, it can only take 25 items at a time, so you would still end up having to write a loop.

Sorry its not a more complete answer, but thought this might be useful. As far as I know there isn't a simple (single) method for doing this.

Upvotes: 0

Related Questions