Last active
January 16, 2020 02:46
-
-
Save dejanvasic85/cf9d5c3a947671bd3fb2e76938871585 to your computer and use it in GitHub Desktop.
golang: Deleting all items in Dynamo DB with a full scan
This file contains 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
package main | |
import ( | |
"fmt" | |
"strconv" | |
"github.com/aws/aws-sdk-go/aws" | |
"github.com/aws/aws-sdk-go/aws/session" | |
"github.com/aws/aws-sdk-go/service/dynamodb" | |
"github.com/aws/aws-sdk-go/service/dynamodb/dynamodbattribute" | |
) | |
const ( | |
TableName = "jobs" | |
KeyName = "jobId" | |
Region = "ap-southeast-2" | |
) | |
func getNextJobsPage(db *dynamodb.DynamoDB, exclusiveStartKey map[string]*dynamodb.AttributeValue) (*dynamodb.ScanOutput, error) { | |
var queryInput = &dynamodb.ScanInput{ | |
TableName: aws.String(TableName), | |
Select: aws.String("ALL_ATTRIBUTES"), | |
Limit: aws.Int64(100), | |
ExclusiveStartKey: exclusiveStartKey, | |
} | |
fmt.Println("Fetching. LastKey: %v\n", exclusiveStartKey) | |
return db.Scan(queryInput) | |
} | |
func deleteJob(db *dynamodb.DynamoDB, jobID *string) error { | |
key := make(map[string]*dynamodb.AttributeValue) | |
key[KeyName] = &dynamodb.AttributeValue{ | |
N: jobID, | |
} | |
var deleteQuery = &dynamodb.DeleteItemInput{ | |
TableName: aws.String(TableName), | |
Key: key, | |
ReturnValues: aws.String("NONE"), | |
ReturnConsumedCapacity: aws.String("NONE"), | |
ReturnItemCollectionMetrics: aws.String("NONE"), | |
} | |
_, err := db.DeleteItem(deleteQuery) | |
return err | |
} | |
func main() { | |
sesh := session.New(&aws.Config{ | |
Region: aws.String(Region), | |
}) | |
db := dynamodb.New(sesh) | |
var lastEvaluatedKey map[string]*dynamodb.AttributeValue | |
lastEvaluatedKey = nil | |
for { | |
result, _ := getNextJobsPage(db, lastEvaluatedKey) | |
for _, i := range result.Items { | |
item := Job{} | |
err := dynamodbattribute.UnmarshalMap(i, &item) | |
if err != nil { | |
fmt.Printf("Job %d failed to unmarshal. Error %s", item.JobID, err.Error()) | |
} | |
id := strconv.Itoa(item.JobID) | |
fmt.Printf("Deleting job %s\n", id) | |
if err = deleteJob(db, &id); err != nil { | |
panic(err) | |
} | |
} | |
if result.LastEvaluatedKey == nil { | |
break | |
} | |
lastEvaluatedKey = result.LastEvaluatedKey | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment