Delete a single object, multiple objects by name, objects matching a prefix, or an entire directory.
Deleted objects cannot be recovered. Proceed with caution.
Usage notes
-
In this topic, the public endpoint of the China (Hangzhou) region is used. To access OSS from other Alibaba Cloud services in the same region, use an internal endpoint. For details about supported regions and endpoints, see Regions and Endpoints.
-
This topic demonstrates creating an OSSClient instance with an OSS endpoint. For alternative configurations, such as using a custom domain or authenticating with credentials from Security Token Service (STS), see Create an OssClient.
-
To delete an object, you must have the
oss:DeleteObjectpermission. For more information, see Attach a custom policy to a RAM user.
Delete a single object
The following code deletes 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\Credentials\EnvironmentVariableCredentialsProvider;
use OSS\OssClient;
use OSS\CoreOssException;
// Obtain access credentials from environment variables. Before you run the sample code, make sure that the OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET environment variables are configured.
$provider = new EnvironmentVariableCredentialsProvider();
// Set endpoint 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 the endpoint to https://oss-cn-hangzhou.aliyuncs.com.
$endpoint = "yourEndpoint";
// Specify the bucket name. Example: examplebucket.
$bucket = "examplebucket";
// Specify the full path of the object. Example: exampledir/exampleobject.txt. The full path cannot contain the bucket name.
$object = "exampledir/exampleobject.txt";
try{
$config = array(
"provider" => $provider,
"endpoint" => $endpoint,
"signatureVersion" => OssClient::OSS_SIGNATURE_VERSION_V4,
"region"=> "cn-hangzhou"
);
$ossClient = new OssClient($config);
$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 at a time by name, by prefix, or by directory.
You can also configure lifecycle rules to automatically delete objects. For more information, see Lifecycle rules based on last modification time.
Delete multiple objects with specified names
The following code deletes multiple objects by name:
<?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;
// Obtain access credentials from environment variables. Before you run the sample code, make sure that the OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET environment variables are configured.
$accessKeyId = getenv("OSS_ACCESS_KEY_ID");
$accessKeySecret = getenv("OSS_ACCESS_KEY_SECRET");
// Set endpoint 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 the endpoint to https://oss-cn-hangzhou.aliyuncs.com.
$endpoint = "yourEndpoint";
// Specify the bucket name. Example: examplebucket.
$bucket = "examplebucket";
try {
$ossClient = new OssClient($accessKeyId, $accessKeySecret, $endpoint, false);
// Specify the full paths of the objects that you want to delete. The full paths cannot contain the bucket name.
$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 multiple objects with the specified object name prefix or in the specified directory
The following code deletes objects by prefix or deletes an entire directory:
If OSS_PREFIX in the following code is set to an empty string or NULL, all objects in the bucket are deleted. Proceed with caution.
<?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;
// Obtain access credentials from environment variables. Before you run the sample code, make sure that the OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET environment variables are configured.
$accessKeyId = getenv("OSS_ACCESS_KEY_ID");
$accessKeySecret = getenv("OSS_ACCESS_KEY_SECRET");
// Set endpoint 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 the endpoint to https://oss-cn-hangzhou.aliyuncs.com.
$endpoint = "yourEndpoint";
// Specify the bucket name. Example: examplebucket.
$bucket = "examplebucket";
try {
$ossClient = new OssClient($accessKeyId, $accessKeySecret, $endpoint, false);
$option = array(
OssClient::OSS_MARKER => null,
// To delete all objects whose names start with the prefix "src", set the prefix to "src". This operation deletes all non-folder objects with this prefix, the "src" folder, and all objects within the "src" folder.
OssClient::OSS_PREFIX => "src",
// To delete only the "src" folder and all objects in it, set the prefix to "src/".
// OssClient::OSS_PREFIX => "src/",
);
$bool = true;
while ($bool){
// List and delete multiple objects.
$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;
}
If an exception is thrown and caught in a try{}catch{} block, the objects are not deleted. Call $e->getMessage to obtain the error message and analyze the cause.
References
-
For the complete sample code for deleting objects, see the GitHub example.
-
For more information about the API operation for deleting a single object, see DeleteObject.
-
For more information about the API operation for deleting multiple objects, see DeleteMultipleObjects.