Use the OSS Swift SDK to delete a single file or multiple files from a bucket.
Deleted files cannot be recovered. Use the delete operation with caution.
Prerequisites
Before you begin, ensure that you have:
The
oss:DeleteObjectpermission on the target bucket. See Grant a custom access policy to a RAM user.The
OSS_ACCESS_KEY_IDandOSS_ACCESS_KEY_SECRETenvironment variables set with valid credentials.
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.
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
For complete sample code, see the GitHub examples delete_object and delete_mutiple_objects.
For the underlying API operations, see DeleteObject and DeleteMultipleObjects.