Yandex Cloud
Search
Contact UsGet started
  • Blog
  • Pricing
  • Documentation
  • All Services
  • System Status
    • Featured
    • Infrastructure & Network
    • Data Platform
    • Containers
    • Developer tools
    • Serverless
    • Security
    • Monitoring & Resources
    • ML & AI
    • Business tools
  • All Solutions
    • By industry
    • By use case
    • Economics and Pricing
    • Security
    • Technical Support
    • Customer Stories
    • Cloud credits to scale your IT product
    • Gateway to Russia
    • Cloud for Startups
    • Education and Science
    • Yandex Cloud Partner program
  • Blog
  • Pricing
  • Documentation
© 2025 Direct Cursus Technology L.L.C.
Yandex Object Storage
    • All tutorials
      • Uploading an object
      • Multipart upload of an object
      • Get a list of bucket objects
      • Getting information about an object
      • Downloading an object
      • Restoring an object's version
      • Renaming and moving objects
      • Copying objects
      • 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's ACL
      • Managing object labels
      • Managing object custom metadata
  • Terraform reference
  • Monitoring metrics
  • Audit Trails events
  • Bucket logs
  • Release notes
  • FAQ
  1. Step-by-step tutorials
  2. Objects
  3. Deleting all objects

Deleting all objects from a bucket

Written by
Yandex Cloud
Improved by
Tania L.
Updated at May 5, 2025

Note

To automatically delete partially loaded objects in Object Storage, configure a lifecycle rule.

To clear a bucket and avoid storagecharges:

Yandex Cloud CLI
AWS CLI
Python (boto3)
API

If you do not have the Yandex Cloud CLI yet, install and initialize it.

The folder specified when creating the CLI profile is used by default. To change the default folder, use the yc config set folder-id <folder_ID> command. You can specify a different folder using the --folder-name or --folder-id parameter.

  1. See the description of the CLI command for deleting objects:

    yc storage s3api delete-objects --help
    
  2. Get a list of buckets in the default folder:

    yc storage bucket list
    

    Result:

    +------------------+----------------------+-------------+-----------------------+---------------------+
    |       NAME       |      FOLDER ID       |  MAX SIZE   | DEFAULT STORAGE CLASS |     CREATED AT      |
    +------------------+----------------------+-------------+-----------------------+---------------------+
    | first-bucket     | b1gmit33ngp6******** | 53687091200 | STANDARD              | 2022-12-16 13:58:18 |
    +------------------+----------------------+-------------+-----------------------+---------------------+
    
  3. Prepare a JSON file listing the objects to delete. Here is an example of its contents:

    {
        "Objects": [
            {
                "Key": "<object_key>"
            },
            {
                "Key": "<object_key>"
            },
            ...
        ]          
    }
    

    Where:

    • Objects: Array of objects to delete.
    • Key: Object key.
  4. Run this command:

    yc storage s3api delete-objects \
      --bucket <bucket_name> \
      --delete <path_to_JSON_file>
    

    Where:

    • --bucket: Name of your bucket.
    • --delete: Path to the JSON file listing the objects to delete.

    Result:

    deleted:
      - key: file-1
        version_id: "null"
      - key: file-2
        version_id: "null"
    request_id: 4c35e7d4********
    

If you do not have the AWS CLI yet, install and configure it.

  1. Create a variable containing the bucket name:

    BUCKET_NAME=<bucket_name>
    
  2. Delete all object versions 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)"
    

    You can also use --max-keys instead of the --max-items parameter.

    Result:

    {
        "Deleted": [
            {
                "Key": "object_000",
                "VersionId": "0005CDD3********"
            },
            {
                "Key": "object_001",
                "VersionId": "0005CDD3********"
            },
            ...
        ]
    }
    

    Using this command, you can delete a maximum of 1,000 object versions at a time due to the aws s3api delete-objects limitations. If your bucket contains more versions, run the command multiple times.

  3. For buckets with enabled versioning, remove all 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)"
    

    You can also use --max-keys instead of the --max-items parameter.

    Result:

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

    Using this command, you can delete a maximum of 1,000 delete markers at a time due to the aws s3api delete-objects limitations. If your bucket contains more delete markers, run the command multiple 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 include parts of objects whose upload started before and completed after the preceding step. 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()

Use the deleteMultipleObjects S3 API method.

In the management console, the information about the number of objects and storage space used in the bucket is updated with a few minutes' delay.

Was the article helpful?

Previous
Deleting an object
Next
Deleting a partially uploaded object
© 2025 Direct Cursus Technology L.L.C.