Todos los productos
Search
Centro de documentos

Object Storage Service:Eliminar buckets con OSS SDK para PHP 2.0

Última actualización: Sep 03, 2026

Si ya no utiliza un bucket de Object Storage Service (OSS), elimínelo para evitar cargos innecesarios.

Notas de uso

  • El código de ejemplo de este tema usa el ID de región cn-hangzhou correspondiente a la región China (Hangzhou). De forma predeterminada, se usa un endpoint público para acceder a los recursos de un bucket. Si desea acceder a los recursos del bucket mediante otros servicios de Alibaba Cloud en la misma región donde se encuentra el bucket, use un endpoint interno. Para obtener más información sobre las regiones y endpoints de OSS, consulte Regions and endpoints.

  • El nombre de un bucket es único. Una vez eliminado, su nombre queda disponible para que otros lo usen. Se recomienda vaciar el bucket en lugar de eliminarlo si aún desea conservar el nombre.

  • Un bucket eliminado no se puede restaurar. Asegúrese de que los datos del bucket ya no sean necesarios antes de eliminarlo. Si desea seguir usando los datos del bucket, haga una copia de seguridad con antelación. Para obtener más información, consulte Back up buckets.

  • Para eliminar un bucket, debe contar con el permiso oss:DeleteBucket. Para obtener más información, consulte Grant a custom policy.

Requisitos previos

  • Todos los objetos del bucket están eliminados.

    Importante

    Para eliminar un bucket con control de versiones habilitado, asegúrese de haber eliminado todas las versiones actuales y anteriores de los objetos en el bucket. Para obtener más información, consulte Versioning.

    • Si el bucket contiene pocos objetos, elimínelos manualmente. Para obtener más información, consulte Delete objects.

    • Si el bucket contiene muchos objetos, configure reglas de ciclo de vida para eliminarlos. Para obtener más información, consulte 生命周期.

  • Las partes generadas por tareas de carga multiparte o cargas reanudables en el bucket están eliminadas. Para obtener más información, consulte Delete parts.

  • El usuario de RAM tiene otorgado el permiso oss:DeleteBucket si desea usar dicho usuario para eliminar el bucket. Para obtener más información, consulte Grant a custom policy.

    Nota

    Si el usuario de RAM tiene el permiso oss:DeleteBucket en una directiva de RAM pero no puede eliminar el bucket, es posible que una directiva de bucket contenga el permiso oss:DeleteBucket con efecto Deny. En este caso, cambie Deny a Allow o elimine la directiva de bucket. Después podrá eliminar el bucket.

Ejemplos

A continuación se muestra un código de ejemplo para eliminar un bucket:

<?php

require_once __DIR__ . '/../vendor/autoload.php'; // Automatically load objects and dependency libraries.

use AlibabaCloud\Oss\V2 as Oss;

// Specify command line parameters.
$optsdesc = [
    "region" => ['help' => The region in which the bucket is located.', 'required' => True], // (Required) Specify the region in which the bucket is located. Example: oss-cn-hangzhou. 
    "endpoint" => ['help' => The domain names that other services can use to access OSS.', 'required' => False], // (Optional) Specify the endpoint that can be used by other services to access OSS. 
    "bucket" => ['help' => The name of the bucket, 'required' => True], // (Required) Specify the name of the bucket. 
];
$longopts = \array_map(function ($key) {
    return "$key:"; // Add a colon (:) to the end of each parameter to indicate that a value is required.
}, array_keys($optsdesc));

// Parse the command line parameters.
$options = getopt("", $longopts); 

// Check whether the required parameters are configured.
foreach ($optsdesc as $key => $value) {
    if ($value['required'] === True && empty($options[$key])) {
        $help = $value['help'];
        echo "Error: the following arguments are required: --$key, $help"; // Specifies that the required parameters are not configured.
        exit(1); 
    }
}

// Obtain the values of the command line parameters.
$region = $options["region"]; // The region in which the bucket is located. 
$bucket = $options["bucket"]; // The name of the bucket. 

// Use environment variables to load the credential information (AccessKey ID and AccessKey secret).
$credentialsProvider = new Oss\Credentials\EnvironmentVariableCredentialsProvider(); 

// Use the default configurations of the SDK.
$cfg=Oss\Config::loadDefault(); // Load the default configurations of the SDK. 
$cfg->setCredentialsProvider($credentialsProvider); // Specify the credential provider. 
$cfg->setRegion($region); // Specify the region. 
if (isset($options["endpoint"])) {
    $cfg->setEndpoint($options["endpoint"]); // Specify the endpoint if an endpoint is provided. 
}

// Create an OSSClient instance.
$client = new Oss\Client($cfg); 

// Create a request to delete the bucket.
$request = new Oss\Models\DeleteBucketRequest($bucket); 

// Use the deleteBucket method to delete the bucket.
$result = $client->deleteBucket($request); 

// Display the returned result.
printf(
    'status code:' . $result-> statusCode. PHP_EOL . // The HTTP response status code. 
    'request id:' . $result->requestId // The unique identifier of the request. 
);

Referencias

  • Para consultar el código de ejemplo completo para eliminar un bucket, visite GitHub.

  • Para obtener más información sobre la operación de API que permite eliminar un bucket, consulte DeleteBucket.