AWS Certified Database - Specialty: How to Truncate a DynamoDB Table

Truncate a DynamoDB Table

Question

A database specialist needs to truncate a DynamoDB table.

How can this operation be performed?

Answers

Explanations

Click on the arrows to vote for the correct answer

A. B. C. D.

Answer: C.

DynamoDB does not have an equivalent command to the SQL TRUNCATE command.

The only way to delete all data in a DynamoDB table is to perform a table scan to fetch all the item keys.

Then you must iterate through the keys and delete each item using the delete-item command.

Therefore, Option C is CORRECT, and other options are incorrect.

Instead of using the delete-item command, it is possible to delete multiple items using the batch-write-item command.

One must be careful with using this approach as scan operation, and batch-write-item operations consume read and write capacity units.

If there are no requirements to keep the table, a faster and more cost-effective approach would be to delete and recreate the entire DynamoDB table.

Reference:

https://medium.com/@samnco/deleting-content-in-dynamodb-from-the-cli-831ce5ab083c

To truncate a DynamoDB table, the correct answer is B. Use aws dynamodb truncate-table CLI command.

Explanation:

Truncating a DynamoDB table involves deleting all items from the table while keeping the table schema intact. This is a faster and more efficient way of deleting all items from a DynamoDB table than iterating through each item and deleting them one-by-one.

There are several ways to delete items from a DynamoDB table, but only one of the options presented in the question is specifically designed for truncating a table. Let's go through each answer option to understand why some are incorrect:

A. Use aws dynamodb delete-from CLI command. The "delete-from" command does not exist in the AWS CLI for DynamoDB. The closest equivalent is the "delete-item" command, which deletes a single item from a table. This command would not be useful for truncating a table, as it would require iterating through every item in the table to delete them one-by-one.

B. Use aws dynamodb truncate-table CLI command. This command is the correct answer. The "truncate-table" command deletes all items from a table while preserving the table schema. This is the most efficient way to delete all items from a table.

C. Use aws dynamodb scan CLI command to scan the table. Iterate through all keys and use aws dynamodb delete-item CLI command to delete each item. This approach would work to delete all items from a table, but it would be less efficient than using the "truncate-table" command. Scanning the entire table could take a long time, especially if the table is large, and deleting each item individually would require multiple API calls.

D. Use aws dynamodb batch-delete-item CLI command. The "batch-delete-item" command is useful for deleting multiple items from a table at once, but it requires specifying each item to be deleted in the request. This command would not be useful for truncating a table, as it would require first scanning the table to retrieve all item keys before being able to delete them in batches.

In summary, the most efficient and straightforward way to truncate a DynamoDB table is by using the "aws dynamodb truncate-table" command.