Created
February 27, 2026 10:19
-
-
Save astroanu/05d673ef682dc940f1c66b8ea1495eac to your computer and use it in GitHub Desktop.
Truncate dynamodb table
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/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