From 2637edfc48a23b2951032b1e974d7097602c4715 Mon Sep 17 00:00:00 2001 From: Michael Austin Date: Fri, 15 May 2015 15:12:41 -0400 Subject: [PATCH] optimize delete to delete up to 1000 at once instead of one at a time --- .../providers/aws/resource_aws_s3_bucket.go | 27 +++++++++++-------- 1 file changed, 16 insertions(+), 11 deletions(-) diff --git a/builtin/providers/aws/resource_aws_s3_bucket.go b/builtin/providers/aws/resource_aws_s3_bucket.go index 134419e6e992..0c4df1d00981 100644 --- a/builtin/providers/aws/resource_aws_s3_bucket.go +++ b/builtin/providers/aws/resource_aws_s3_bucket.go @@ -9,7 +9,6 @@ import ( "github.com/awslabs/aws-sdk-go/aws" "github.com/awslabs/aws-sdk-go/service/s3" - "github.com/awslabs/aws-sdk-go/aws/awsutil" ) func resourceAwsS3Bucket() *schema.Resource { @@ -231,19 +230,25 @@ func resourceAwsS3BucketDelete(d *schema.ResourceData, meta interface{}) error { return fmt.Errorf("[DEBUG] S3 Bucket list Objects err: %s", err) } - log.Printf("[!!!!] %s", awsutil.StringValue(resp)) + objectsToDelete := make([]*s3.ObjectIdentifier, len(resp.Contents)) + for i,v := range resp.Contents { - for _,v := range resp.Contents { - _, deleteErr := s3conn.DeleteObject( - &s3.DeleteObjectInput{ - Bucket: aws.String(bucket), - Key: aws.String(*v.Key), - }, - ) - if (deleteErr != nil) { - log.Printf("[DEBUG] S3 Bucket force_destroy error deleting: %s", deleteErr) + objectsToDelete[i] = &s3.ObjectIdentifier { + Key: v.Key, } } + _, deleteErr := s3conn.DeleteObjects( + &s3.DeleteObjectsInput { + Bucket: aws.String(bucket), + Delete: &s3.Delete{ + Objects:objectsToDelete, + }, + }, + ) + if (deleteErr != nil) { + return fmt.Errorf("[DEBUG] S3 Bucket force_destroy error deleting: %s", deleteErr) + } + // attempting to delete again return resourceAwsS3BucketDelete(d,meta); }