Problem description
When you use Alibaba Cloud Object Storage Service (OSS), you may be unable to delete a bucket that you no longer need. This can happen in the OSS console or when you use tools such as an OSS software development kit (SDK) or ossutil. The following error messages may be returned:
When you use an OSS SDK or ossutil to delete a bucket, the error message "The bucket you tried to delete is not empty" is returned.
When you delete a bucket in the OSS console, the error message "The bucket is not empty. Check whether the bucket contains undeleted objects or incomplete multipart upload parts" is returned.
Causes
The bucket that you want to delete contains undeleted objects, parts, LiveChannels, or incomplete multipart upload tasks. To prevent accidental deletion, OSS does not allow you to delete a non-empty bucket.
Solutions
Data cannot be recovered after a bucket is deleted. Before you delete a bucket, make sure that your data is backed up. Then, delete all objects, parts, and LiveChannels from the bucket. To delete the bucket, perform the following steps:
Check for undeleted files and parts
To delete a bucket without files or fragments, follow these steps:
Log on to the OSS console.
On the Objects tab, check whether any files exist.
If files exist, select one or more files and click Permanently Delete to delete them. You can also use ossutil to delete the files. For more information, see Use ossutil to delete buckets, files, and parts.
If it does not exist, continue to the next step.
Use ossutil to run the following command to delete the bucket. If the "Bucket is not empty" error persists, proceed to the next step.
./ossutil rm oss://[$Bucket_Name] -barNote[$Bucket_Name] is the name of the bucket.
Check for previous versions of objects
Versioning is a data protection feature at the bucket level. After you enable versioning, any overwrites or deletions of objects are saved as previous versions. If you accidentally overwrite or delete an object, you can restore it to any of its previous versions. However, when you view objects in the console or using a tool without specifying a version number, only the latest versions are displayed. Previous versions are not shown. To delete previous versions of objects, use one of the following methods:
Use the OSS console
Log on to the OSS console.
Click Buckets, and then click the name of the target bucket.
In the navigation pane on the left, select Content Security > Versioning and check whether the versioning feature is enabled. If the versioning feature is enabled, perform the following operations to delete previous object versions.
Use ossutil
Run the following command to check whether versioning is enabled. For more information, see bucket-versioning.
./ossutil bucket-versioning --method get oss://[$Bucket_Name]A command output similar to the following is displayed.
bucket versioning status:EnabledIf versioning is enabled, the result is `Enabled`. Run the following command to delete the previous versions of objects in the bucket.
./ossutil rm oss://[$Bucket_Name]/ --all-versions -rA command output similar to the following is displayed.

If versioning is disabled, the result is `Disable`.
Check whether LiveChannels exist in the bucket
You cannot view or delete LiveChannels in a bucket from the console or using tools. You can use API operations or an SDK to perform these operations. For more information, see DeleteLiveChannel. The following sample code is provided for your reference:
import java.util.List;
import com.alibaba.fastjson.JSON;
import com.aliyun.oss.ClientException;
import com.aliyun.oss.OSS;
import com.aliyun.oss.OSSClientBuilder;
import com.aliyun.oss.OSSException;
import com.aliyun.oss.model.CreateLiveChannelRequest;
import com.aliyun.oss.model.CreateLiveChannelResult;
import com.aliyun.oss.model.ListLiveChannelsRequest;
import com.aliyun.oss.model.LiveChannelGenericRequest;
import com.aliyun.oss.model.LiveChannelListing;
import com.aliyun.oss.model.LiveChannelStatus;
import com.aliyun.oss.model.LiveChannelTarget;
import com.aliyun.oss.model.LiveChannel;
import com.aliyun.oss.common.auth.*;
public class DeleteLiveChannel {
private static String endpoint = "http://oss-cn-hangzhou.aliyuncs.com";
/**
* Obtain access credentials from environment variables.
*/
private static final EnvironmentVariableCredentialsProvider CREDENTIALS_PROVIDER = CredentialsProviderFactory.newEnvironmentVariableCredentialsProvider();
private static String bucketName = "bucketname";
public static void createLiveChannel() {
// Create an OSSClient instance.
// When the OSSClient instance is no longer in use, call the shutdown method to release its resources.
String liveChannelName = "liveChannelName1";
OSS oss = new OSSClientBuilder().build(endpoint, CREDENTIALS_PROVIDER);
CreateLiveChannelRequest request = new CreateLiveChannelRequest(bucketName,
liveChannelName, "desc", LiveChannelStatus.Enabled, new LiveChannelTarget());
CreateLiveChannelResult result = oss.createLiveChannel(request);
//Get the ingest URL.
List<String> publishUrls = result.getPublishUrls();
for (String item : publishUrls) {
System.out.println(item);
}
//Get the playback URL.
List<String> playUrls = result.getPlayUrls();
for (String item : playUrls) {
System.out.println(item);
}
oss.shutdown();
}
public static void listLiveChannels() {
// Create an OSSClient instance.
OSS oss = new OSSClientBuilder().build(endpoint, CREDENTIALS_PROVIDER);
ListLiveChannelsRequest request = new ListLiveChannelsRequest(bucketName);
LiveChannelListing liveChannelListing = oss.listLiveChannels(request);
List<LiveChannel> list = liveChannelListing.getLiveChannels();
for(LiveChannel i:list){
deleteLiveChannel(i.getName());
}
oss.shutdown();
}
public static void deleteLiveChannel(String liveChannelName) {
// Create an OSSClient instance.
OSS oss = new OSSClientBuilder().build(endpoint, CREDENTIALS_PROVIDER);
LiveChannelGenericRequest request = new LiveChannelGenericRequest(bucketName, liveChannelName);
System.out.println("Delete liveChannel "+liveChannelName+" Done");
try {
oss.deleteLiveChannel(request);
} catch (OSSException ex) {
ex.printStackTrace();
} catch (ClientException ex) {
ex.printStackTrace();
} finally {
oss.shutdown();
}
}
public static void main(String[] args) {
// TODO Auto-generated method stub
DeleteLiveChannel.listLiveChannels();
}
}