/*
   Copyright 2010-2016 Amazon.com, Inc. or its affiliates. All Rights Reserved.

   This file is licensed under the Apache License, Version 2.0 (the "License").
   You may not use this file except in compliance with the License. A copy of
   the License is located at

    http://aws.amazon.com/apache2.0/

   This file is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
   CONDITIONS OF ANY KIND, either express or implied. See the License for the
   specific language governing permissions and limitations under the License.
*/
package aws.example.s3;
import com.amazonaws.services.s3.AmazonS3;
import com.amazonaws.services.s3.AmazonS3Client;
import com.amazonaws.AmazonServiceException;

/**
 * Delete an Amazon S3 bucket.
 *
 * This code expects that you have AWS credentials set up per:
 * http://docs.aws.amazon.com/java-sdk/latest/developer-guide/setup-credentials.html
 *
 * ++ Warning ++ This code will actually delete the bucket that you specify!
 */
public class DeleteBucket
{
    public static void main(String[] args)
    {
        final String USAGE = "\n" +
            "To run this example, supply the name of an S3 bucket\n" +
            "\n" +
            "Ex: DeleteBucket <bucketname>\n";

        if (args.length < 1)
        {
            System.out.println(USAGE);
            System.exit(1);
        }

        String bucket_name = args[0];

        System.out.println("Deleting S3 bucket: " + bucket_name);
        final AmazonS3 s3 = new AmazonS3Client();
        try {
            s3.deleteBucket(bucket_name);
        }
        catch (AmazonServiceException e) {
            System.err.println(e.getErrorMessage());
            System.exit(1);
        }
        System.out.println("Done!");
    }
}


@@ -557,16 +557,61 @@
 ple.s3;%0A
+import com.amazonaws.AmazonServiceException;%0A
 import c
@@ -715,38 +715,302 @@
 azonaws.
-AmazonServiceException
+services.s3.model.ListVersionsRequest;%0Aimport com.amazonaws.services.s3.model.ObjectListing;%0Aimport com.amazonaws.services.s3.model.S3ObjectSummary;%0Aimport com.amazonaws.services.s3.model.S3VersionSummary;%0Aimport com.amazonaws.services.s3.model.VersionListing;%0Aimport java.util.Iterator
 ;%0A%0A/**%0A 
@@ -1268,16 +1268,67 @@
  specify
+, as%0A *               well as any objects within it
 !%0A */%0Apu
@@ -1848,16 +1848,17 @@
 ient();%0A
+%0A
         
@@ -1863,16 +1863,1662 @@
   try %7B%0A
+            System.out.println(%22 - removing objects from bucket%22);%0A            ObjectListing object_listing = s3.listObjects(bucket_name);%0A            while (true) %7B%0A                for (Iterator%3C?%3E iterator =%0A                        object_listing.getObjectSummaries().iterator();%0A                        iterator.hasNext();) %7B%0A                    S3ObjectSummary summary = (S3ObjectSummary)iterator.next();%0A                    s3.deleteObject(bucket_name, summary.getKey());%0A                %7D%0A%0A                // more object_listing to retrieve?%0A                if (object_listing.isTruncated()) %7B%0A                    object_listing = s3.listNextBatchOfObjects(object_listing);%0A                %7D%0A                else %7B break; %7D%0A            %7D;%0A%0A            System.out.println(%22 - removing versions from bucket%22);%0A            VersionListing version_listing = s3.listVersions(%0A                    new ListVersionsRequest().withBucketName(bucket_name));%0A            while (true) %7B%0A                for (Iterator%3C?%3E iterator =%0A                        version_listing.getVersionSummaries().iterator();%0A                        iterator.hasNext();) %7B%0A                    S3VersionSummary vs = (S3VersionSummary)iterator.next();%0A                    s3.deleteVersion(%0A                            bucket_name, vs.getKey(), vs.getVersionId());%0A                %7D%0A%0A                if (version_listing.isTruncated()) %7B%0A                    version_listing = s3.listNextBatchOfVersions(%0A                            version_listing);%0A                %7D%0A                else %7B break; %7D%0A            %7D%0A%0A            System.out.println(%22 OK, bucket ready to delete!%22);%0A
         

update Java sample S3/DeleteBucket