Yandex Cloud
  • Services
  • Solutions
  • Why Yandex Cloud
  • Blog
  • Pricing
  • Documentation
  • Contact us
Get started
Language / Region
Yandex project
© 2023 Yandex.Cloud LLC
Yandex Object Storage
  • Getting started
  • Step-by-step instructions
    • All instructions
    • Buckets
      • Creating a bucket
      • Deleting a bucket
      • Limiting the maximum size of a bucket
      • Allowing public access to a bucket
      • Bucket encryption
      • Managing object lifecycles
      • Managing CORS configurations
      • Access policy management
      • Managing bucket versioning
      • Editing the ACL of a bucket
      • Enabling logging
      • Object lock management
    • Objects
      • Uploading an object
      • Getting information about an object
      • Downloading an object
      • Restoring an object's version
      • Getting a public link to an object
      • Configuring an object lock
      • Deleting an object
      • Deleting all objects
      • Deleting a partially uploaded object
      • Editing an object ACL
    • Static website hosting
      • Hosting setup
      • Support for your own domain
      • Support for multiple domain names
      • Configuring HTTPS
  • Tutorials
    • Getting object query statistics with S3 Select
    • Getting website traffic statistics with S3 Select
    • Generating a billing report with resource break-down using S3 Select
    • Server-side encryption
    • Integrating an L7 load balancer with the CDN and Object Storage
    • Blue-green and canary deployment of service versions
    • Analyzing logs in DataLens
    • Using initialization scripts to configure GeeseFS in Yandex Data Proc
    • Mounting a bucket as a disk on Windows
    • Migrating data from Yandex Data Streams using Yandex Data Transfer
  • Concepts
    • Overview
    • Bucket
    • Objects
    • Bucket versioning
    • Object locks
    • Encryption
    • Object lifecycles
    • CORS
    • Static website hosting
    • Pre-signed URLs
    • Multipart upload
    • Access control lists (ACLs)
    • Bucket Policy
    • Uploading files via an HTML form
    • Storage class
    • Logging actions with a bucket
    • Backups
    • TLS protocol
    • S3 Select query language
    • Quotas and limits
  • Tools
    • All tools
    • Console clients
      • AWS CLI
      • S3cmd
    • File browsers
      • CyberDuck
      • WinSCP
    • SDK
      • AWS SDK for Java
      • Python SDK (boto)
      • AWS SDK for .NET
      • AWS SDK for C++
      • AWS SDK for PHP
      • AWS SDK for Go
    • FUSE
      • GeeseFS
      • s3fs
      • goofys
    • FTP
      • Docker container for (S)FTP(S)
  • API reference
    • REST (Amazon S3-compatible)
      • How to use the API
      • Signing requests
      • REST
        • All services and methods
        • Bucket
          • All methods
          • create
          • getBucketEncryption
          • getMeta
          • listObjects
          • listBuckets
          • deleteBucket
          • deleteBucketEncryption
          • putBucketEncryption
          • putBucketVersioning
          • getBucketVersioning
          • putBucketLogging
          • getBucketLogging
          • listObjectVersions
          • putObjectLockConfiguration
          • getObjectLockConfiguration
        • Object
          • All methods
          • upload
          • get
          • copy
          • getObjectMeta
          • delete
          • deleteMultipleObjects
          • options
          • selectObjectContent
          • putObjectRetention
          • putObjectLegalHold
          • getObjectRetention
          • getObjectLegalHold
        • Multipart upload
          • General multipart upload procedure
          • startUpload
          • uploadPart
          • copyPart
          • listParts
          • abortUpload
          • completeUpload
          • listUploads
        • Static Website Hosting
          • All methods
          • upload
          • get
          • delete
          • Static website response codes
        • CORS
          • All methods
          • XML structure of CORS configuration
          • upload
          • get
          • delete
        • Lifecycles
          • All methods
          • XML structure of lifecycle configuration
          • upload
          • get
          • delete
        • ACL
          • All methods
          • XML structure of ACL configuration
          • objectGetAcl
          • objectPutAcl
          • bucketGetAcl
          • bucketPutAcl
        • Bucket Policy
          • All methods
          • GetBucketPolicy
          • PutBucketPolicy
          • DeleteBucketPolicy
          • Data schema
          • Actions
          • Conditions
        • Common request headers
        • Common response headers
        • Responses
    • gRPC and REST
      • Authentication in the API
      • gRPC
        • Overview
        • BucketService
        • OperationService
      • REST
        • Overview
        • Bucket
          • Overview
          • create
          • delete
          • deleteHTTPSConfig
          • get
          • getHTTPSConfig
          • getStats
          • list
          • setHTTPSConfig
          • update
  • Access management
  • Pricing policy
    • Current pricing policy
    • Archive
      • Before January 1, 2019
      • From January 1 to March 1, 2019
  • Questions and answers
  1. Step-by-step instructions
  2. Objects
  3. Deleting all objects

Deleting all objects in a bucket

Written by
Yandex Cloud

    To clear a bucket and not pay for storage:

    AWS CLI
    Python 3 (boto3)

    If you don't have the AWS CLI yet, install and configure it.

    1. Create a variable containing the bucket name:

      BUCKET_NAME=<bucket_name>
      
    2. Delete all versions of objects from the bucket:

      aws s3api delete-objects \
        --endpoint-url https://storage.yandexcloud.net \
        --bucket $BUCKET_NAME \
        --delete \
          "$(aws s3api list-object-versions \
            --endpoint-url https://storage.yandexcloud.net \
            --bucket $BUCKET_NAME \
            --query '{Objects: Versions[].{Key: Key, VersionId: VersionId}}' \
            --max-items 1000)"
      

      Result:

      {
          "Deleted": [
              {
                  "Key": "object_000",
                  "VersionId": "0005CDD3521954A6"
              },
              {
                  "Key": "object_001",
                  "VersionId": "0005CDD3523B6ADB"
              },
              ...
          ]
      }
      

      With this command, you can delete up to 1000 versions of objects: this is related to restrictions for the aws s3api delete-objects operation. If there are more versions in the bucket, run the command several times.

    3. If the bucket has versioning enabled, delete all the delete markers:

      aws s3api delete-objects \
        --endpoint-url https://storage.yandexcloud.net \
        --bucket $BUCKET_NAME \
        --delete \
          "$(aws s3api list-object-versions \
            --endpoint-url https://storage.yandexcloud.net \
            --bucket $BUCKET_NAME \
            --query '{Objects: DeleteMarkers[].{Key: Key, VersionId: VersionId}}' \
            --max-items 1000)"
      

      Result:

      {
          "Deleted": [
              {
                  "Key": "object_034",
                  "DeleteMarker": true,
                  "DeleteMarkerVersionId": "0005CDD35274A467"
              },
              {
                  "Key": "object_057",
                  "DeleteMarker": true,
                  "DeleteMarkerVersionId": "0005CDD3528B1BC3"
              },
              ...
          ]
      }
      

      With this command, you can delete up to 1000 delete markers: this is related to restrictions for the aws s3api delete-objects operation. If there are more versions in the bucket, run the command several times.

    4. Delete partially uploaded objects:

      aws s3api list-multipart-uploads \
        --endpoint-url https://storage.yandexcloud.net \
        --bucket $BUCKET_NAME \
      | jq -r '.Uploads[] | "--key \"\(.Key)\" --upload-id \(.UploadId)"' \
      | while read -r line; do
        eval
          "aws s3api abort-multipart-upload \
            --endpoint-url https://storage.yandexcloud.net \
            --bucket $BUCKET_NAME \
            $line";
      done
      
    5. Get a list of object parts remaining in the bucket:

      aws s3api list-multipart-uploads \
        --endpoint-url https://storage.yandexcloud.net \
        --bucket $BUCKET_NAME \
      | jq -r '.Uploads[] | "--key \"\(.Key)\" --upload-id \(.UploadId)"' \
      | while read -r line; do
        eval
          "aws s3api list-parts \
            --endpoint-url https://storage.yandexcloud.net \
            --bucket $BUCKET_NAME \
            $line";
      done
      

      The list may contain parts of objects that began uploading before and finished uploading after the previous step executed. If the list is not empty, repeat steps 4 and 5.

    Run the following code:

    import boto3
    
    bucket_name = '<bucket_name>'
    s3 = boto3.resource('s3',
        endpoint_url='https://storage.yandexcloud.net',
        aws_access_key_id='<key_ID>',
        aws_secret_access_key='<secret_key>')
    bucket = s3.Bucket(bucket_name)
    
    # Deleting all versions (works for non-versioned buckets too).
    bucket.object_versions.delete()
    
    # Aborting all multipart uploads, which also deletes all parts.
    for multipart_upload in bucket.multipart_uploads.iterator():
        # Part uploads that are currently in progress may or may not succeed,
        # so it might be necessary to abort a multipart upload multiple times.
        while len(list(multipart_upload.parts.all())) > 0:
            multipart_upload.abort()
    

    Was the article helpful?

    Language / Region
    Yandex project
    © 2023 Yandex.Cloud LLC