All Products
Search
Document Center

Object Storage Service:Delete files (Swift SDK)

Last Updated:Mar 20, 2026

Use the OSS Swift SDK to delete a single file or multiple files from a bucket.

Warning

Deleted files cannot be recovered. Use the delete operation with caution.

Prerequisites

Before you begin, ensure that you have:

Usage notes

  • The sample code uses the China (Hangzhou) region (cn-hangzhou) with its public endpoint. To access OSS from other Alibaba Cloud services in the same region, use an internal endpoint. For supported regions and endpoints, see Regions and endpoints.

Delete a single file

deleteObject sends an asynchronous request to permanently delete one object from a bucket. The operation succeeds if no exception is thrown.

import AlibabaCloudOSS
import Foundation

@main
struct Main {
    static func main() async {
        do {
            // The region where the bucket is located. Example: cn-hangzhou for China (Hangzhou).
            let region = "cn-hangzhou"
            // The bucket name.
            let bucket = "yourBucketName"
            // Optional. The domain name to access OSS. For China (Hangzhou), set the endpoint to https://oss-cn-hangzhou.aliyuncs.com.
            let endpoint: String? = nil
            // The name of the object to delete, such as test.txt.
            let key = "yourObjectName"

            // Initialize the credential provider to load credentials from environment variables.
            let credentialsProvider = EnvironmentCredentialsProvider()

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

            // Set the endpoint.
            if let endpoint = endpoint {
                config.withEndpoint(endpoint)
            }

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

            // Send a request to delete the object.
            let result = try await client.deleteObject(
                DeleteObjectRequest(
                    bucket: bucket,
                    key: key
                )
            )
            print("result:\n\(result)")

        } catch {
            // Print the error message and continue.
            print("error: \(error)")
        }
    }
}

Delete multiple files

deleteMultipleObjects deletes a batch of objects in a single request. Build a [DeleteObject] array from the object keys, then pass it to DeleteMultipleObjectsRequest.

import AlibabaCloudOSS
import Foundation

@main
struct Main {
    static func main() async {
        do {
            // The region where the bucket is located. Example: cn-hangzhou for China (Hangzhou).
            let region = "cn-hangzhou"
            // The bucket name.
            let bucket = "yourBucketName"
            // Optional. The domain name to access OSS. For China (Hangzhou), set the endpoint to https://oss-cn-hangzhou.aliyuncs.com.
            let endpoint: String? = nil
            // The names of the objects to delete, separated by commas, such as object1.txt,object2.jpg.
            let objects = "yourObjectName1,yourObjectName2"

            // Initialize the credential provider to load OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET from environment variables.
            let credentialsProvider = EnvironmentCredentialsProvider()

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

            // Set the endpoint.
            if let endpoint = endpoint {
                config.withEndpoint(endpoint)
            }

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

            // Parse the object name string into a DeleteObject array.
            var deleteObjects: [DeleteObject] = []
            for object in objects.components(separatedBy: ",") {
                deleteObjects.append(DeleteObject(key: object))
            }

            // Perform a batch delete operation.
            let result = try await client.deleteMultipleObjects(
                DeleteMultipleObjectsRequest(
                    bucket: bucket,
                    objects: deleteObjects
                )
            )
            print("result:\n\(result)")

        } catch {
            // Print the error message and continue.
            print("error: \(error)")
        }
    }
}

FAQ

How do I confirm a file was deleted?

deleteObject returns without throwing an error if the deletion succeeds. To verify programmatically, call doesObjectExist on the OSSClient — it returns false if the object no longer exists. For details, see Determine whether a file exists (Swift SDK).

What's next