To stop billing for a bucket, you must delete it. To cease OSS service billing and effectively shut down the service, delete all buckets under the current account.
Warning
-
Once a bucket is deleted, its name becomes available and may be claimed by others. To retain the bucket name, consider emptying the bucket instead of deleting it.
-
Data within a deleted bucket cannot be restored. Ensure you no longer need the data before deleting the bucket, or back it up beforehand. For more information about backups, see Backup Buckets.
Resources to clean up before deleting a bucket
Before deleting a bucket, ensure all resources are cleaned up. While most users only store files in their buckets, some may use advanced features that require additional configuration items to be addressed. The OSS console can automatically detect resources needing cleanup, and it is recommended to use the OSS console for this process.
Methods
Use the OSS console
Log on to the OSS console.
In the left-side navigation pane, click Buckets. On the Buckets page, find and click the desired bucket.
-
In the left-side navigation pane, click Delete Bucket.
-
On the Delete Bucket page, follow the instructions to complete the resource cleanup, and then click Delete Immediately.

Use the command line tool ossutil
The command line tool ossutil can be used to delete a bucket. For information on installing ossutil, see Install Ossutil.
Use the following command to delete the bucket named examplebucket
:
ossutil api delete-bucket --bucket examplebucket
For more details on this command, see Delete Bucket.
Use Alibaba Cloud SDK
Below are code examples for deleting a bucket using common SDKs. For other SDKs, see SDK Overview.
Java
PHP
Node.js
Python
.NET
Android
IOS
C++
C
Ruby
Go
import com.aliyun.oss.ClientException;
import com.aliyun.oss.OSS;
import com.aliyun.oss.common.auth.*;
import com.aliyun.oss.OSSClientBuilder;
import com.aliyun.oss.OSSException;
public class Demo {
public static void main(String[] args) throws Exception {
String endpoint = "https://oss-cn-hangzhou.aliyuncs.com";
EnvironmentVariableCredentialsProvider credentialsProvider = CredentialsProviderFactory.newEnvironmentVariableCredentialsProvider();
String bucketName = "examplebucket";
String region = "cn-hangzhou";
ClientBuilderConfiguration clientBuilderConfiguration = new ClientBuilderConfiguration();
clientBuilderConfiguration.setSignatureVersion(SignVersion.V4);
OSS ossClient = OSSClientBuilder.create()
.endpoint(endpoint)
.credentialsProvider(credentialsProvider)
.clientConfiguration(clientBuilderConfiguration)
.region(region)
.build();
try {
ossClient.deleteBucket(bucketName);
} catch (OSSException oe) {
System.out.println("Caught an OSSException, which means your request made it to OSS, "
+ "but was rejected with an error response for some reason.");
System.out.println("Error Message:" + oe.getErrorMessage());
System.out.println("Error Code:" + oe.getErrorCode());
System.out.println("Request ID:" + oe.getRequestId());
System.out.println("Host ID:" + oe.getHostId());
} catch (ClientException ce) {
System.out.println("Caught an ClientException, which means the client encountered "
+ "a serious internal problem while trying to communicate with OSS, "
+ "such as not being able to access the network.");
System.out.println("Error Message:" + ce.getMessage());
} finally {
if (ossClient != null) {
ossClient.shutdown();
}
}
}
}
<?php
if (is_file(__DIR__ . '/../autoload.php')) {
require_once __DIR__ . '/../autoload.php';
}
if (is_file(__DIR__ . '/../vendor/autoload.php')) {
require_once __DIR__ . '/../vendor/autoload.php';
}
use OSS\Credentials\EnvironmentVariableCredentialsProvider;
use OSS\OssClient;
use OSS\CoreOssException;
$provider = new EnvironmentVariableCredentialsProvider();
$endpoint = "https://oss-cn-hangzhou.aliyuncs.com";
$bucket= "examplebucket";
try{
$config = array(
"provider" => $provider,
"endpoint" => $endpoint,
"signatureVersion" => OssClient::OSS_SIGNATURE_VERSION_V4,
"region"=> "cn-hangzhou"
);
$ossClient = new OssClient($config);
$ossClient->deleteBucket($bucket);
} catch(OssException $e) {
printf(__FUNCTION__ . ": FAILED\n");
printf($e->getMessage() . "\n");
return;
}
print(__FUNCTION__ . ": OK" . "\n");
const OSS = require('ali-oss');
const client = new OSS({
region: 'yourregion',
accessKeyId: process.env.OSS_ACCESS_KEY_ID,
accessKeySecret: process.env.OSS_ACCESS_KEY_SECRET,
authorizationV4: true,
bucket: 'yourBucketName',
});
async function deleteBucket() {
try {
const result = await client.deleteBucket('yourbucketname');
console.log(result);
} catch (err) {
console.log(err);
}
}
deleteBucket();
import oss2
from oss2.credentials import EnvironmentVariableCredentialsProvider
auth = oss2.ProviderAuthV4(EnvironmentVariableCredentialsProvider())
endpoint = "https://oss-cn-hangzhou.aliyuncs.com"
region = "cn-hangzhou"
bucket = oss2.Bucket(auth, endpoint, "yourBucketName", region=region)
try:
bucket.delete_bucket()
except oss2.exceptions.BucketNotEmpty:
print('bucket is not empty.')
except oss2.exceptions.NoSuchBucket:
print('bucket does not exist')
using System;
using Aliyun.OSS;
using Aliyun.OSS.Common;
namespace Samples
{
public class Program
{
public static void Main(string[] args)
{
var endpoint = "https://oss-cn-hangzhou.aliyuncs.com";
var accessKeyId = Environment.GetEnvironmentVariable("OSS_ACCESS_KEY_ID");
var accessKeySecret = Environment.GetEnvironmentVariable("OSS_ACCESS_KEY_SECRET");
var bucketName = "examplebucket314";
const string region = "cn-hangzhou";
var conf = new ClientConfiguration();
conf.SignatureVersion = SignatureVersion.V4;
var client = new OssClient(endpoint, accessKeyId, accessKeySecret, conf);
client.SetRegion(region);
try
{
client.DeleteBucket(bucketName);
Console.WriteLine("Delete bucket succeeded");
}
catch (Exception ex)
{
Console.WriteLine("Delete bucket failed. {0}", ex.Message);
}
}
}
}
DeleteBucketRequest deleteBucketRequest = new DeleteBucketRequest("bucketName");
OSSAsyncTask deleteBucketTask = oss.asyncDeleteBucket(deleteBucketRequest, new OSSCompletedCallback<DeleteBucketRequest, DeleteBucketResult>() {
@Override
public void onSuccess(DeleteBucketRequest request, DeleteBucketResult result) {
Log.d("asyncDeleteBucket", "Success!");
}
@Override
public void onFailure(DeleteBucketRequest request, ClientException clientException, ServiceException serviceException) {
if (clientException != null) {
clientException.printStackTrace();
}
if (serviceException != null) {
Log.e("ErrorCode", serviceException.getErrorCode());
Log.e("RequestId", serviceException.getRequestId());
Log.e("HostId", serviceException.getHostId());
Log.e("RawMessage", serviceException.getRawMessage());
}
}
});
OSSDeleteBucketRequest * delete = [OSSDeleteBucketRequest new];
delete.bucketName = @"examplebucket";
OSSTask * deleteTask = [client deleteBucket:delete];
[deleteTask continueWithBlock:^id(OSSTask *task) {
if (!task.error) {
NSLog(@"delete bucket success!");
} else {
NSLog(@"delete bucket failed, error: %@", task.error);
}
return nil;
}];
#include <alibabacloud/oss/OssClient.h>
using namespace AlibabaCloud::OSS;
int main(void)
{
std::string Endpoint = "yourEndpoint";
std::string Region = "yourRegion";
std::string BucketName = "examplebucket";
InitializeSdk();
ClientConfiguration conf;
conf.signatureVersion = SignatureVersionType::V4;
auto credentialsProvider = std::make_shared<EnvironmentVariableCredentialsProvider>();
OssClient client(Endpoint, credentialsProvider, conf);
client.SetRegion(Region);
DeleteBucketRequest request(BucketName);
auto outcome = client.DeleteBucket(request);
if (outcome.isSuccess()) {
std::cout << "Delete bucket successfully." << std::endl;
} else {
std::cout << "Failed to delete bucket. Error code: " << outcome.error().Code()
<< ", Message: " << outcome.error().Message()
<< ", RequestId: " << outcome.error().RequestId() << std::endl;
}
ShutdownSdk();
return 0;
}
#include "oss_api.h"
#include "aos_http_io.h"
const char *endpoint = "yourEndpoint";
const char *bucket_name = "examplebucket";
const char *region = "yourRegion";
void init_options(oss_request_options_t *options)
{
options->config = oss_config_create(options->pool);
aos_str_set(&options->config->endpoint, endpoint);
aos_str_set(&options->config->access_key_id, getenv("OSS_ACCESS_KEY_ID"));
aos_str_set(&options->config->access_key_secret, getenv("OSS_ACCESS_KEY_SECRET"));
aos_str_set(&options->config->region, region);
options->config->signature_version = 4;
options->config->is_cname = 0;
options->ctl = aos_http_controller_create(options->pool, 0);
}
int main(int argc, char *argv[])
{
if (aos_http_io_initialize(NULL, 0) != AOSE_OK) {
exit(1);
}
aos_pool_t *pool;
aos_pool_create(&pool, NULL);
oss_request_options_t *oss_client_options;
oss_client_options = oss_request_options_create(pool);
init_options(oss_client_options);
aos_string_t bucket;
aos_table_t *resp_headers = NULL;
aos_status_t *resp_status = NULL;
aos_str_set(&bucket, bucket_name);
resp_status = oss_delete_bucket (oss_client_options, &bucket, &resp_headers);
if (aos_status_is_ok(resp_status)) {
printf("delete bucket succeeded\n");
} else {
printf("delete bucket failed\n");
}
aos_pool_destroy(pool);
aos_http_io_deinitialize();
return 0;
}
require 'aliyun/oss'
client = Aliyun::OSS::Client.new(
endpoint: 'https://oss-cn-hangzhou.aliyuncs.com',
access_key_id: ENV['OSS_ACCESS_KEY_ID'],
access_key_secret: ENV['OSS_ACCESS_KEY_SECRET']
)
client.delete_bucket('examplebucket')
package main
import (
"context"
"flag"
"log"
"github.com/aliyun/alibabacloud-oss-go-sdk-v2/oss"
"github.com/aliyun/alibabacloud-oss-go-sdk-v2/oss/credentials"
)
var (
region string
bucketName string
)
func init() {
flag.StringVar(®ion, "region", "", "The region in which the bucket is located.")
flag.StringVar(&bucketName, "bucket", "", "The name of the bucket.")
}
func main() {
flag.Parse()
if len(bucketName) == 0 {
flag.PrintDefaults()
log.Fatalf("invalid parameters, bucket name required")
}
if len(region) == 0 {
flag.PrintDefaults()
log.Fatalf("invalid parameters, region required")
}
cfg := oss.LoadDefaultConfig().
WithCredentialsProvider(credentials.NewEnvironmentVariableCredentialsProvider()).
WithRegion(region)
client := oss.NewClient(cfg)
request := &oss.DeleteBucketRequest{
Bucket: oss.Ptr(bucketName),
}
result, err := client.DeleteBucket(context.TODO(), request)
if err != nil {
log.Fatalf("failed to delete bucket %v", err)
}
log.Printf("delete bucket result:%#v\n", result)
}
Related API
The methods described above are based on API operations. For highly customized program requirements, you can directly call REST API operations, which include signature calculations. For more details, see DeleteBucket.
Permission description
By default, an Alibaba Cloud account has full permissions. RAM users or RAM roles under an Alibaba Cloud account have no permissions by default and must be granted permissions through RAM Policy or Bucket Policy.
API | Action | Description |
DeleteBucket | oss:DeleteBucket
| Delete a bucket. |
Billing description
Deleting a bucket may incur certain billable items. For detailed pricing information, see or OSS Pricing.
API | Billable items | Description |
API | Billable items | Description |
DeleteBucket | PUT requests | You are charged request fees based on the number of successful requests. |
FAQ
How to delete a bucket for which OSS-HDFS is enabled?
To delete a bucket with OSS-HDFS enabled, you must use the OSS console and ensure all data within the bucket is removed first.
Warning
Be cautious when deleting buckets and objects as they cannot be restored once removed.
For example, to delete a non-empty bucket named examplebucket in the China (Hangzhou) region with OSS-HDFS enabled, follow these steps:
-
Remove all data from examplebucket.
-
Delete all objects on the HDFS tab.
Use the OSS console
Use HDFS shell commands
Use Jindo CLI commands
-
Click Buckets, and then select the bucket where OSS-HDFS is enabled.
In the left-side navigation tree, choose Object Management > Objects.
-
Delete all files under the HDFS tab.
Note
Files on the HDFS tab do not support batch deletion. You can manually delete files on the HDFS tab individually.
jindo fs -rm oss://examplebucket.cn-shanghai.oss-dls.aliyuncs.com/*
For more details, see Access Using Jindo CLI.
-
Delete all objects on the OSS Object tab.
Use the OSS console
Use the command line tool ossutil
-
Click Buckets, and then select the bucket where OSS-HDFS is enabled.
In the left-side navigation tree, choose Object Management > Objects.
-
Select all files on the OSS Object tab, and then click Permanently Delete.
ossutil rm oss://examplebucket --all-versions -r
-
Delete examplebucket.
How do i create a bucket that has the same name as a deleted bucket?
You can create a bucket with the same name as a deleted bucket approximately 4 to 8 hours after deletion. Once deleted, the bucket name is available for anyone to claim.