Skip to content

Instantly share code, notes, and snippets.

@astroanu
Created February 27, 2026 10:19
Show Gist options
  • Select an option

  • Save astroanu/05d673ef682dc940f1c66b8ea1495eac to your computer and use it in GitHub Desktop.

Select an option

Save astroanu/05d673ef682dc940f1c66b8ea1495eac to your computer and use it in GitHub Desktop.
Truncate dynamodb table
#!/usr/bin/env bash
set -euo pipefail
TABLE_NAME="$1"
REGION="${2:-us-east-1}"
if [[ -z "$TABLE_NAME" ]]; then
echo "Usage: $0 <table-name> [region]"
exit 1
fi
echo "๐Ÿ” Describing table: $TABLE_NAME"
DESC=$(aws dynamodb describe-table \
--table-name "$TABLE_NAME" \
--region "$REGION")
echo "๐Ÿ’พ Extracting schema..."
ATTRIBUTES=$(echo "$DESC" | jq '.Table.AttributeDefinitions')
KEY_SCHEMA=$(echo "$DESC" | jq '.Table.KeySchema')
BILLING_MODE=$(echo "$DESC" | jq -r '.Table.BillingModeSummary.BillingMode // "PROVISIONED"')
if [[ "$BILLING_MODE" == "PROVISIONED" ]]; then
READ_CAP=$(echo "$DESC" | jq '.Table.ProvisionedThroughput.ReadCapacityUnits')
WRITE_CAP=$(echo "$DESC" | jq '.Table.ProvisionedThroughput.WriteCapacityUnits')
THROUGHPUT="--provisioned-throughput ReadCapacityUnits=$READ_CAP,WriteCapacityUnits=$WRITE_CAP"
else
THROUGHPUT="--billing-mode PAY_PER_REQUEST"
fi
echo "๐Ÿ—‘๏ธ Deleting table..."
aws dynamodb delete-table \
--table-name "$TABLE_NAME" \
--region "$REGION"
echo "โณ Waiting for table deletion..."
aws dynamodb wait table-not-exists \
--table-name "$TABLE_NAME" \
--region "$REGION"
echo "๐Ÿ› ๏ธ Recreating table..."
aws dynamodb create-table \
--table-name "$TABLE_NAME" \
--attribute-definitions "$ATTRIBUTES" \
--key-schema "$KEY_SCHEMA" \
$THROUGHPUT \
--region "$REGION"
echo "โณ Waiting for table to become ACTIVE..."
aws dynamodb wait table-exists \
--table-name "$TABLE_NAME" \
--region "$REGION"
echo "โœ… Table truncated successfully."
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment