title: How to delete non empty S3 bucket using aws-java-sdk 2.0
body: <p>I am able to delete a non-empty bucket using <code>aws-java-sdk 1.0</code> but not with aws-java-sdk 2.0. Here is the code:</p>
  
  <p><strong>Not working code with aws-java-sdk 2.0 for deleting non empty s3 bucket:</strong></p>
  
  <pre><code>{
              boolean isTruncated = true;
              while(isTruncated) {
                  ListObjectsV2Iterable objectListing = s3Client.listObjectsV2Paginator(ListObjectsV2Request.builder()
                          .bucket(bucketName)
                          .build());
                  for (ListObjectsV2Response response : objectListing) {
                      /*
                       * There could be many pages in object list response is just one page
                       * */
                      for (S3Object s3Object : response.contents()) {
                          s3Client.deleteObject(DeleteObjectRequest.builder()
                                  .bucket(bucketName)
                                  .key(s3Object.key())
                                  .build());
                      }
                      isTruncated = response.isTruncated();
                  }
              }
  
  
              // Delete all object versions (required for versioned buckets).
              ListObjectVersionsIterable versionList = s3Client.listObjectVersionsPaginator(ListObjectVersionsRequest.builder()
                      .bucket(bucketName)
                      .build());
              isTruncated = true;
  
              while (isTruncated) {
                  for (ListObjectVersionsResponse response : versionList) {
                      for (ObjectVersion version : response.versions()) {
                          s3Client.deleteObject(DeleteObjectRequest.builder()
                                  .bucket(bucketName)
                                  .key(version.key())
                                  .versionId(version.versionId())
                                  .build());
                      }
                      isTruncated = response.isTruncated();
                  }
              }
  
          s3Client.deleteBucket(DeleteBucketRequest.builder().bucket(bucketName).build());
      }
  </code></pre>
  
  <p>Above code is unable to delete bucket, it throws exception:</p>
  
  <blockquote>
    <p>software.amazon.awssdk.services.s3.model.S3Exception: The bucket you tried to delete is not empty. You must delete all versions in the bucket. (Service: S3, Status Code: 409, Request ID: F5D76141223DC5EA)</p>
  </blockquote>
  
  <p><strong>Working code with aws-java-sdk 1.0 for deleting non empty bucket:</strong></p>
  
  <pre><code>ObjectListing objectListing = s3Client.listObjects(bucketName);
              while (true) {
                  Iterator&lt;S3ObjectSummary&gt; objIter = objectListing.getObjectSummaries().iterator();
                  while (objIter.hasNext()) {
                      s3Client.deleteObject(bucketName, objIter.next().getKey());
                  }
  
                  // If the bucket contains many objects, the listObjects() call
                  // might not return all of the objects in the first listing. Check to
                  // see whether the listing was truncated. If so, retrieve the next page of objects
                  // and delete them.
                  if (objectListing.isTruncated()) {
                      objectListing = s3Client.listNextBatchOfObjects(objectListing);
                  } else {
                      break;
                  }
              }
  
              // Delete all object versions (required for versioned buckets).
              VersionListing versionList = s3Client.listVersions(new ListVersionsRequest().withBucketName(bucketName));
              while (true) {
                  Iterator&lt;S3VersionSummary&gt; versionIter = versionList.getVersionSummaries().iterator();
                  while (versionIter.hasNext()) {
                      S3VersionSummary vs = versionIter.next();
                      s3Client.deleteVersion(bucketName, vs.getKey(), vs.getVersionId());
                  }
  
                  if (versionList.isTruncated()) {
                      versionList = s3Client.listNextBatchOfVersions(versionList);
                  } else {
                      break;
                  }
              }
  
              // After all objects and object versions are deleted, delete the bucket.
              s3Client.deleteBucket(bucketName);
  </code></pre>