This topic describes how to delete a single object and multiple objects at a time.
Deleted objects cannot be recovered. Exercise caution when you delete objects.
Usage notes
Before you run the sample code in this topic, you must create an OSSClient instance by using methods such as using a custom domain name or Security Token Service (STS). For more information, see Initialization.
To delete an object, you must have the write permissions on the bucket in which the object is stored.
Delete a single object
The following sample code provides an example on how to delete an object named exampleobject.txt from a bucket named examplebucket:
OSSDeleteObjectRequest * delete = [OSSDeleteObjectRequest new];
// Specify the name of the bucket. Example: examplebucket.
delete.bucketName = @"examplebucket";
// Specify the full path of the object. Do not include the bucket name in the full path. Example: exampleobject.txt.
delete.objectKey = @"exampleobject.txt";
OSSTask * deleteTask = [client deleteObject:delete];
[deleteTask continueWithBlock:^id(OSSTask *task) {
if (!task.error) {
// ...
}
return nil;
}];
// Implement synchronous blocking to wait for the task to complete.
// [deleteTask waitUntilFinished];
Delete multiple objects at a time
You can delete up to 1,000 objects at a time.
The result can be returned in one of the following modes:
verbose: If quiet is set to NO, a list of all deleted objects are returned.
quiet: If quiet is not specified or is set to YES, a list of objects that failed to be deleted are returned.
The following sample code provides an example on how to delete multiple specified objects from a bucket named examplebucket and return the result in quiet mode:
OSSDeleteMultipleObjectsRequest *request = [OSSDeleteMultipleObjectsRequest new];
// Specify the name of the bucket. Example: examplebucket.
request.bucketName = @"examplebucket";
// Specify the full paths of objects to be deleted. Do not include the bucket name in the full paths of objects.
request.keys = @[@"exampleobject.txt", @"testfolder/sampleobject.txt"];
// Set quiet to YES to return only a list of objects that failed to be deleted.
request.quiet = YES;
OSSTask * deleteMultipleObjectsTask = [client deleteMultipleObjects:request];
[deleteMultipleObjectsTask continueWithBlock:^id(OSSTask *task) {
if (!task.error) {
OSSDeleteMultipleObjectsResult *result = task.result;
NSLog(@"delete objects: %@", result.deletedObjects);
} else {
NSLog(@"delete objects failed, error: %@", task.error);
}
return nil;
}];
// Implement synchronous blocking to wait for the task to complete.
// [deleteTask waitUntilFinished];
References
Delete a single object
For more information about the API operation that you can call to delete a single object, see DeleteObject.
Delete multiple objects at a time
For information about the API operation that you can call to delete multiple objects, see DeleteMultipleObjects.
For more information about how to initialize an OSSClient instance, see Initialization.