This topic describes how to delete a single object or delete multiple objects at a time.
Usage notes
To delete an object, you must have write permissions on the bucket in which the object is stored.
Delete a single object
The following 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. The full path cannot contain the bucket name. Example: exampleobject.txt.
delete.objectKey = @"exampleobject.txt";
OSSTask * deleteTask = [client deleteObject:delete];
[deleteTask continueWithBlock:^id(OSSTask *task) {
if (!task.error) {
// ...
}
return nil;
}];
// [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 the following two modes. Select a return mode based on your requirements.
- verbose: If quiet is set to NO, a list of all deleted objects is returned.
- quiet: If quiet is not specified or is set to YES, a list of objects that failed to be deleted is returned. This is the default return mode.
The following code provides an example on how to delete multiple specified objects from a bucket named examplebucket and return the result in the quiet mode:
OSSDeleteMultipleObjectsRequest *request = [OSSDeleteMultipleObjectsRequest new];
// Specify the name of the bucket. Example: examplebucket.
request.bucketName = @"examplebucket";
// Specify the full paths of multiple objects to be deleted. The full paths of objects cannot contain the bucket name.
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;
}];
References
- Delete a single object
For more information about the API operation that you can call to delete an object, see DeleteObject.
- Delete multiple objects at a time
For more information about the API operation that you can call to delete multiple objects, see DeleteMultipleObjects.