Delete a bucket (Swift SDK)

Updated at:

Delete an OSS bucket when you no longer need it to avoid unnecessary charges.

Warning

Deleting a bucket is irreversible. Back up any data you want to keep before proceeding. Once deleted, the bucket name becomes available for others to use — if you want to retain the name, empty the bucket instead of deleting it.

Prerequisites

Before you begin, make sure that:

  • All objects in the bucket are deleted

    • Few objects: delete them manually. See Delete objects.

    • Many objects: use lifecycle rules to delete them in bulk. See Lifecycle.

    Important

    For a versioned bucket, delete all current and previous versions of every object. See Versioning.

  • All parts from multipart upload or resumable upload tasks are deleted. See Delete parts.

Permissions

By default, an Alibaba Cloud account has full permissions on its resources. RAM users and RAM roles have no permissions by default and must be granted access via RAM Policy or bucket policy.

APIActionDescription
DeleteBucketoss:DeleteBucketDeletes a bucket
If a RAM user has the oss:DeleteBucket action in their RAM policy but still cannot delete the bucket, a bucket policy with a Deny effect on oss:DeleteBucket may be overriding it. Change the effect to Allow or remove that bucket policy before retrying.

Delete a bucket

The example below uses the cn-hangzhou region. By default, a public endpoint is used. To access OSS from another Alibaba Cloud service in the same region, replace the endpoint with an internal endpoint. For supported regions and endpoints, see Regions and endpoints.

The credentials are loaded from the OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET environment variables. Set these variables before running the example.

import AlibabaCloudOSS
import Foundation

@main
struct Main {
    static func main() async {

        do {

            // Specify the region where the bucket is located.
            let region = "cn-hangzhou"
            // Specify the bucket name.
            let bucket = "examplebucket"
            // (Optional) Specify the endpoint. If not set, the default public endpoint for the region is used.
            let endpoint: String? = nil

            // Load credentials from environment variables (OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET).
            let credentialsProvider = EnvironmentCredentialsProvider()

            // Configure the OSS client.
            let config = Configuration.default()
                .withRegion(region)
                .withCredentialsProvider(credentialsProvider)

            if let endpoint = endpoint {
                config.withEndpoint(endpoint)
            }

            // Create an OSS client instance.
            let client = Client(config)

            // Delete the bucket.
            let result = try await client.deleteBucket(
                DeleteBucketRequest(
                    bucket: bucket
                )
            )

            print("result:\n\(result)")

        } catch {
            print("error:\n\(error)")
        }
    }
}

Troubleshooting

If the delete operation fails, check the following:

  • The bucket is not emptyDeleteBucket only succeeds on an empty bucket. Delete all objects, object versions (for versioned buckets), and multipart upload parts first.

  • Missing permission — Confirm that your RAM user or RAM role has the oss:DeleteBucket action. Contact your Alibaba Cloud account administrator if needed.

  • Bucket policy with Deny effect — Even if your RAM policy grants oss:DeleteBucket, a bucket policy with a Deny effect takes precedence. Remove or update the Deny statement, then retry.

What's next