You can delete a single object, multiple objects, objects whose names contain a specified prefix, or a directory and all objects stored in this directory.

Warning Deleted objects cannot be recovered. Exercise caution when you delete objects.

Delete a single object

The following code provides an example on how to delete an object named exampleobject.txt from a bucket named examplebucket:

<?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\OssClient;
use OSS\Core\OssException;

// Security risks may arise if you use the AccessKey pair of an Alibaba Cloud account to access OSS because the account has permissions on all API operations. We recommend that you use a Resource Access Management (RAM) user to call API operations or perform routine O&M. To create a RAM user, log on to the RAM console. 
$accessKeyId = "yourAccessKeyId";
$accessKeySecret = "yourAccessKeySecret";
// Set yourEndpoint to the endpoint of the region in which the bucket is located. For example, if the bucket is located in the China (Hangzhou) region, set yourEndpoint to https://oss-cn-hangzhou.aliyuncs.com. 
$endpoint = "yourEndpoint";
// Specify the name of the bucket. Example: examplebucket. 
$bucket = "examplebucket";
// Specify the full path of the object. Example: exampledir/exampleobject.txt. The full path of the object cannot contain the bucket name. 
$object = "exampledir/exampleobject.txt";

try{
    $ossClient = new OssClient($accessKeyId, $accessKeySecret, $endpoint);

    $ossClient->deleteObject($bucket, $object);
} catch(OssException $e) {
    printf(__FUNCTION__ . ": FAILED\n");
    printf($e->getMessage() . "\n");
    return;
}
print(__FUNCTION__ . "OK" . "\n");          

Delete multiple objects

You can delete up to 1,000 objects each time. You can delete multiple objects, objects whose names contain a specified prefix, or a directory and all objects stored in this directory.

You can also configure lifecycle rules to automatically delete objects. For more information, see Lifecycle rules in OSS Developer Guide.

  • Delete multiple specified objects

    The following code provides an example on how to delete multiple specified objects from a bucket named examplebucket:

    <?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\OssClient;
    use OSS\Core\OssException;
    
    // Security risks may arise if you use the AccessKey pair of an Alibaba Cloud account to access OSS because the account has permissions on all API operations. We recommend that you use a RAM user to call API operations or perform routine O&M. To create a RAM user, log on to the RAM console. 
    $accessKeyId = "yourAccessKeyId";
    $accessKeySecret = "yourAccessKeySecret";
    // Set yourEndpoint to the endpoint of the region in which the bucket is located. For example, if the bucket is located in the China (Hangzhou) region, set yourEndpoint to https://oss-cn-hangzhou.aliyuncs.com. 
    $endpoint = "yourEndpoint";
    // Specify the name of the bucket. Example: examplebucket. 
    $bucket = "examplebucket";
    
    try {
       $ossClient = new OssClient($accessKeyId, $accessKeySecret, $endpoint, false);
       // Specify the full paths of the multiple objects that you want to delete. The full paths of the objects cannot contain bucket names. 
       $objects = array();
       $objects[] = "exampleobjecta.txt";
       $objects[] = "exampledir/sampleobject.txt";
       $result = $ossClient->deleteObjects($bucket, $objects);
    
       foreach ($result as $info){
          $obj = strval($info);
          printf("Delete ".$obj." : Success" . "\n");
       }
       printf("Delete Objects : OK" . "\n");
    } catch (OssException $e) {
       printf("Delete Objects : Failed" . "\n");
       printf($e->getMessage() . "\n");
       return;
    }            
  • Delete objects whose names contain a specified prefix
    The following code provides an example on how to delete objects whose names contain the "file" prefix from a bucket named examplebucket:
    <?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\OssClient;
    use OSS\Core\OssException;
    
    // Security risks may arise if you use the AccessKey pair of an Alibaba Cloud account to access OSS because the account has permissions on all API operations. We recommend that you use a RAM user to call API operations or perform routine O&M. To create a RAM user, log on to the RAM console. 
    $accessKeyId = "yourAccessKeyId";
    $accessKeySecret = "yourAccessKeySecret";
    // Set yourEndpoint to the endpoint of the region in which the bucket is located. For example, if the bucket is located in the China (Hangzhou) region, set yourEndpoint to https://oss-cn-hangzhou.aliyuncs.com. 
    $endpoint = "yourEndpoint";
    // Specify the name of the bucket. Example: examplebucket. 
    $bucket = "examplebucket";
    
    try {
       $ossClient = new OssClient($accessKeyId, $accessKeySecret, $endpoint, false);
       $option = array(
          OssClient::OSS_MARKER => null,
          OssClient::OSS_PREFIX => "file",
       );
       $bool = true;
       while ($bool){
          $result = $ossClient->listObjects($bucket,$option);
          $objects = array();
          if(count($result->getObjectList()) > 0){
             foreach ($result->getObjectList() as $key => $info){
                printf("key name:".$info->getKey().PHP_EOL);
                $objects[] = $info->getKey();
             }
             $delObjects = $ossClient->deleteObjects($bucket, $objects);
             foreach ($delObjects as $info){
                $obj = strval($info);
                printf("Delete ".$obj." : Success" . PHP_EOL);
             }
          }
    
          if($result->getIsTruncated() === 'true'){
             $option[OssClient::OSS_MARKER] = $result->getNextMarker();
          }else{
             $bool = false;
          }
       }
       printf("Delete Objects : OK" . PHP_EOL);
    } catch (OssException $e) {
       printf("Delete Objects : Failed" . PHP_EOL);
       printf($e->getMessage() . PHP_EOL);
       return;
    }
  • Delete a specified directory and all objects stored in the directory
    The following code provides an example on how to delete a directory named examplebucket and all objects stored in the directory from a bucket named examplebucket:
    <?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\OssClient;
    use OSS\Core\OssException;
    // Security risks may arise if you use the AccessKey pair of an Alibaba Cloud account to access OSS because the account has permissions on all API operations. We recommend that you use a RAM user to call API operations or perform routine O&M. To create a RAM user, log on to the RAM console. 
    $accessKeyId = "yourAccessKeyId";
    $accessKeySecret = "yourAccessKeySecret";
    // Set yourEndpoint to the endpoint of the region in which the bucket is located. For example, if the bucket is located in the China (Hangzhou) region, set yourEndpoint to https://oss-cn-hangzhou.aliyuncs.com. 
    $endpoint = "yourEndpoint";
    // Specify the name of the bucket. Example: examplebucket. 
    $bucket = "examplebucket";
    
    try {
       $ossClient = new OssClient($accessKeyId, $accessKeySecret, $endpoint, false);
       $option = array(
          OssClient::OSS_MARKER => null,
          OssClient::OSS_PREFIX => "exampledir/",
       );
       $bool = true;
       while ($bool){
          $result = $ossClient->listObjects($bucket,$option);
          $objects = array();
          if(count($result->getObjectList()) > 0){
             foreach ($result->getObjectList() as $key => $info){
                printf("key name:".$info->getKey().PHP_EOL);
                $objects[] = $info->getKey();
             }
             $delObjects = $ossClient->deleteObjects($bucket, $objects);
             foreach ($delObjects as $info){
                $obj = strval($info);
                printf("Delete ".$obj." : Success" . PHP_EOL);
             }
          }
    
          if($result->getIsTruncated() === 'true'){
             $option[OssClient::OSS_MARKER] = $result->getNextMarker();
          }else{
             $bool = false;
          }
       }
       printf("Delete Objects : OK" . PHP_EOL);
    } catch (OssException $e) {
       printf("Delete Objects : Failed" . PHP_EOL);
       printf($e->getMessage() . PHP_EOL);
       return;
    }
Note If you use try{}catch{} and an exception is returned, no object is deleted. In this case, you can use $e->getMessage to obtain the error message and analyze the causes.