How to delete files recursively from an S3 bucket

后端 未结 12 466
不知归路
不知归路 2021-01-29 23:54

I have the following folder structure in S3. Is there a way to recursively remove all files under a certain folder (say foo/bar1 or foo or foo/bar2/1 ..)

         


        
相关标签:
12条回答
  • 2021-01-30 00:31

    In case if you want to remove all objects with "foo/" prefix using Java AWS SDK 2.0

    import java.util.ArrayList;
    import java.util.Iterator;
    import software.amazon.awssdk.services.s3.S3Client;
    import software.amazon.awssdk.services.s3.model.*;
    
    //...
    
    ListObjectsRequest listObjectsRequest = ListObjectsRequest.builder()
        .bucket(bucketName)
        .prefix("foo/")
        .build()
    ;
    ListObjectsResponse objectsResponse = s3Client.listObjects(listObjectsRequest);
    
    while (true) {
        ArrayList<ObjectIdentifier> objects = new ArrayList<>();
    
        for (Iterator<?> iterator = objectsResponse.contents().iterator(); iterator.hasNext(); ) {
            S3Object s3Object = (S3Object)iterator.next();
            objects.add(
                ObjectIdentifier.builder()
                    .key(s3Object.key())
                    .build()
            );
        }
    
        s3Client.deleteObjects(
            DeleteObjectsRequest.builder()
                .bucket(bucketName)
                .delete(
                    Delete.builder()
                        .objects(objects)
                        .build()
                )
                .build()
        );
    
        if (objectsResponse.isTruncated()) {
            objectsResponse = s3Client.listObjects(listObjectsRequest);
            continue;
        }
    
        break;
    };
    
    0 讨论(0)
  • 2021-01-30 00:31

    I just removed all files from my bucket by using PowerShell:

    Get-S3Object -BucketName YOUR_BUCKET | % { Remove-S3Object -BucketName YOUR_BUCKET -Key $_.Key -Force:$true }
    
    0 讨论(0)
  • 2021-01-30 00:31

    In the S3 management console, click on the checkmark for the bucket, and click on the empty button from the top right.

    0 讨论(0)
  • 2021-01-30 00:38

    Best way is to use lifecycle rule to delete whole bucket contents. Programmatically you can use following code (PHP) to PUT lifecycle rule.

    $expiration = array('Date' => date('U', strtotime('GMT midnight')));
    $result = $s3->putBucketLifecycle(array(
                'Bucket' => 'bucket-name',
                'Rules' => array(
                    array(
                        'Expiration' => $expiration,
                        'ID' => 'rule-name',
                        'Prefix' => '',
                        'Status' => 'Enabled',
                    ),
                ),
            ));
    

    In above case all the objects will be deleted starting Date - "Today GMT midnight".

    You can also specify Days as follows. But with Days it will wait for at least 24 hrs (1 day is minimum) to start deleting the bucket contents.

    $expiration = array('Days' => 1);
    
    0 讨论(0)
  • 2021-01-30 00:43

    This used to require a dedicated API call per key (file), but has been greatly simplified due to the introduction of Amazon S3 - Multi-Object Delete in December 2011:

    Amazon S3's new Multi-Object Delete gives you the ability to delete up to 1000 objects from an S3 bucket with a single request.

    See my answer to the related question delete from S3 using api php using wildcard for more on this and respective examples in PHP (the AWS SDK for PHP supports this since version 1.4.8).

    Most AWS client libraries have meanwhile introduced dedicated support for this functionality one way or another, e.g.:

    Python

    You can achieve this with the excellent boto Python interface to AWS roughly as follows (untested, from the top of my head):

    import boto
    s3 = boto.connect_s3()
    bucket = s3.get_bucket("bucketname")
    bucketListResultSet = bucket.list(prefix="foo/bar")
    result = bucket.delete_keys([key.name for key in bucketListResultSet])
    

    Ruby

    This is available since version 1.24 of the AWS SDK for Ruby and the release notes provide an example as well:

    bucket = AWS::S3.new.buckets['mybucket']
    
    # delete a list of objects by keys, objects are deleted in batches of 1k per
    # request.  Accepts strings, AWS::S3::S3Object, AWS::S3::ObectVersion and 
    # hashes with :key and :version_id
    bucket.objects.delete('key1', 'key2', 'key3', ...)
    
    # delete all of the objects in a bucket (optionally with a common prefix as shown)
    bucket.objects.with_prefix('2009/').delete_all
    
    # conditional delete, loads and deletes objects in batches of 1k, only
    # deleting those that return true from the block
    bucket.objects.delete_if{|object| object.key =~ /\.pdf$/ }
    
    # empty the bucket and then delete the bucket, objects are deleted in batches of 1k
    bucket.delete!
    

    Or:

    AWS::S3::Bucket.delete('your_bucket', :force => true)
    
    0 讨论(0)
  • 2021-01-30 00:45

    Just saw that Amazon added a "How to Empty a Bucket" option to the AWS console menu:

    http://docs.aws.amazon.com/AmazonS3/latest/UG/DeletingaBucket.html

    0 讨论(0)
提交回复
热议问题