This topic describes how to delete an object.
Warning Delete an object with caution because a deleted object cannot be recovered.
Run the following code to delete an object:
// Create a delete request
DeleteObjectRequest delete = new DeleteObjectRequest("<bucketName>", "<objectKey>");
// Delete an object by calling the asynchronous interface.
OSSAsyncTask deleteTask = oss.asyncDeleteObject(delete, new OSSCompletedCallback<DeleteObjectRequest, DeleteObjectResult>() {
@Override
public void onSuccess(DeleteObjectRequest request, DeleteObjectResult result) {
Log.d("asyncCopyAndDelObject", "success!");
}
@Override
public void onFailure(DeleteObjectRequest request, ClientException clientExcepion, ServiceException serviceException) {
// Handle the exceptions returned for the request.
if (clientExcepion != null) {
// A local exception (such as network exception) occurs.
clientExcepion.printStackTrace();
}
if (serviceException != null) {
// A service exception occurs.
Log.e("ErrorCode", serviceException.getErrorCode());
Log.e("RequestId", serviceException.getRequestId());
Log.e("HostId", serviceException.getHostId());
Log.e("RawMessage", serviceException.getRawMessage());
}
}
});
Notice To call DeleteObject to delete an object, you must have the write permission on the bucket that stores the object.