如何获取空的DynamoDB表列表以清理闲置资源?
Got it, dealing with thousands of DynamoDB tables and trying to spot empty, unused ones to cut costs makes total sense—every idle resource eats into your budget over time! Here are a couple of practical ways to get that list of empty tables:
Method 1: Use AWS CLI with a Shell Script
This is quick if you're comfortable with command-line tools. We'll loop through all tables and check their item count:
- First, create a shell script (save it as
find-empty-dynamodb-tables.sh):
#!/bin/bash # Fetch all table names (handles pagination automatically in AWS CLI v2) TABLES=$(aws dynamodb list-tables --query 'TableNames' --output text) # Iterate through each table to check item count for TABLE in $TABLES; do # Get the item count for the current table ITEM_COUNT=$(aws dynamodb describe-table --table-name "$TABLE" --query 'Table.ItemCount' --output text) # Check if the table has 0 items if [ "$ITEM_COUNT" -eq 0 ]; then echo "Empty table: $TABLE" fi # Add a small delay to avoid hitting API rate limits (adjust as needed) sleep 0.1 done
- Make the script executable:
chmod +x find-empty-dynamodb-tables.sh
- Run it:
./find-empty-dynamodb-tables.sh
Note: The sleep command helps prevent throttling—since you have over 1000 tables, AWS might limit your describe-table calls without it.
Method 2: Use Python (boto3) for More Control
Python is great if you need to handle pagination smoothly, add error handling, or extend the logic later (like checking CloudWatch metrics for idle activity):
import boto3 import time from botocore.exceptions import ClientError def find_empty_dynamodb_tables(): dynamodb_client = boto3.client('dynamodb') empty_tables = [] # Use a paginator to handle large numbers of tables (list-tables returns max 100 per call) table_paginator = dynamodb_client.get_paginator('list_tables') for page in table_paginator.paginate(): for table_name in page['TableNames']: try: # Fetch table details to get item count table_details = dynamodb_client.describe_table(TableName=table_name) item_count = table_details['Table']['ItemCount'] if item_count == 0: empty_tables.append(table_name) print(f"Found empty table: {table_name}") except ClientError as e: print(f"Could not check table {table_name}: {e.response['Error']['Message']}") # Small delay to avoid API throttling time.sleep(0.1) return empty_tables if __name__ == "__main__": empty_tables = find_empty_dynamodb_tables() print("\nFinal list of empty DynamoDB tables:") for table in empty_tables: print(f"- {table}")
Pro Tip: To confirm a table is truly idle (not just temporarily empty), add logic to check CloudWatch metrics like ReadCapacityUnitsConsumed and WriteCapacityUnitsConsumed over the last 30 days. If those are zero, the table is definitely unused.
Important Considerations
- Permissions: Make sure your IAM role/user has
dynamodb:ListTablesanddynamodb:DescribeTablepermissions. If you plan to delete tables later, adddynamodb:DeleteTabletoo. - Backup First: Before deleting any tables, take a backup (even empty ones—just in case!) or double-check with your team to ensure they're not needed.
- Batch Deletion: Once you have your list, you can extend the scripts to delete tables in bulk, but always add a confirmation step first to avoid accidental deletions.
内容的提问来源于stack exchange,提问作者Alan C.

