This topic explains how to delete a single object, multiple objects, or objects with a specified prefix from a versioned bucket.
Precautions
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.
In this topic, an OSSClient instance is created by using an OSS endpoint. If you want to create an OSSClient instance by using custom domain names or Security Token Service (STS), see Create an OSSClient instance.
To delete an object, you must have the
oss:DeleteObjectpermission. For more information, see Grant custom permissions to a RAM user.
Delete behaviors for versioned buckets
Temporary deletion (without specifying a version ID):
If you perform a delete operation without specifying a version ID, OSS does not delete the current version of the object. Instead, OSS inserts a delete marker for the object. When you retrieve the object, OSS detects that the current version is a delete marker and returns
404 Not Found. The response also includes thex-oss-delete-marker: trueheader and thex-oss-version-idof the new delete marker.Permanent deletion (by specifying a version ID):
If you perform a delete operation and specify a version ID, OSS permanently deletes the specified version based on the
paramsobject'sversionIdparameter. To delete a version whose ID is "null", add to theparamsobject the settingparams['versionId'] = "null". OSS treats the string "null" as the version ID and deletes the object version whose ID is "null".
Delete a single object
The following examples show how to permanently or temporarily delete a single object.
Permanent deletion
The following code shows how to permanently delete an object by specifying its version ID:
<?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(); // The endpoint of the China (Hangzhou) region is used in this example. Replace it with your actual endpoint. $endpoint = "https://oss-cn-hangzhou.aliyuncs.com"; $bucket= "<yourBucketName>"; // Specify the full path of the object. Do not include the bucket name in the full path. Example: example/test.txt. $object = "<yourObjectName>"; $versionId = "<yourObjectVersionId>"; $config = array( "provider" => $provider, "endpoint" => $endpoint, "signatureVersion" => OssClient::OSS_SIGNATURE_VERSION_V4, "region"=> "cn-hangzhou" ); $ossClient = new OssClient($config); try{ // Delete the object with the specified version ID. $ossClient->deleteObject($bucket, $object, array(OssClient::OSS_VERSION_ID => $versionId)); } catch(OssException $e) { printf(__FUNCTION__ . ": FAILED\n"); printf($e->getMessage() . "\n"); return; } print(__FUNCTION__ . ": OK" . "\n");Temporary deletion
The following code shows how to temporarily delete an object without specifying a version ID:
<?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(); // The endpoint of the China (Hangzhou) region is used in this example. Replace it with your actual endpoint. $endpoint = "https://oss-cn-hangzhou.aliyuncs.com"; $bucket= "<yourBucketName>"; $object = "<yourObjectName>"; $config = array( "provider" => $provider, "endpoint" => $endpoint, "signatureVersion" => OssClient::OSS_SIGNATURE_VERSION_V4, "region"=> "cn-hangzhou" ); $ossClient = new OssClient($config); try{ // Temporarily delete an object without specifying its version ID. This operation adds a delete marker to the object. $ret = $ossClient->deleteObject($bucket, $object); print("delete marker:" .$ret['x-oss-delete-marker'] ."\n"); print("version id:" .$ret['x-oss-version-id']); } catch(OssException $e) { printf(__FUNCTION__ . ": FAILED\n"); printf($e->getMessage() . "\n"); return; } print(__FUNCTION__ . ": OK" . "\n");
For more information, see DeleteObject.
Delete multiple objects
The following examples show how to permanently or temporarily delete multiple objects.
Permanent deletion
The following code shows how to permanently delete multiple objects by specifying their version IDs:
<?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; use OSS\Model\DeleteObjectInfo; // 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(); // The endpoint of the China (Hangzhou) region is used in this example. Replace it with your actual endpoint. $endpoint = "https://oss-cn-hangzhou.aliyuncs.com"; $bucket= "<yourBucketName>"; $config = array( "provider" => $provider, "endpoint" => $endpoint, "signatureVersion" => OssClient::OSS_SIGNATURE_VERSION_V4, "region"=> "cn-hangzhou" ); $ossClient = new OssClient($config); try{ // Delete multiple objects with specified version IDs. $objects = array(); $objects[] = new DeleteObjectInfo("<yourObject1Name>", "<object1VersionId>"); $objects[] = new DeleteObjectInfo("<yourObject2Name>", "<object2VersionId>"); $deletedObjectList = $ossClient->deleteObjectVersions($bucket, $objects); // View the deletion result. if (!empty($deletedObjectList)) { print("deletedObjectList:\n"); foreach ($deletedObjectList as $deletedObjectInfo) { print($deletedObjectInfo->getKey() . ","); print($deletedObjectInfo->getVersionId() . ","); print($deletedObjectInfo->getDeleteMarker() . ","); print($deletedObjectInfo->getDeleteMarkerVersionId() . "\n"); } } } catch(OssException $e) { printf(__FUNCTION__ . ": FAILED\n"); printf($e->getMessage() . "\n"); return; } print(__FUNCTION__ . ": OK" . "\n");Soft delete
The following code shows how to delete multiple objects without specifying their version IDs:
<?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; use OSS\Model\DeleteObjectInfo; // 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(); // The endpoint of the China (Hangzhou) region is used in this example. Replace it with your actual endpoint. $endpoint = "https://oss-cn-hangzhou.aliyuncs.com"; $bucket= "<yourBucketName>"; $config = array( "provider" => $provider, "endpoint" => $endpoint, "signatureVersion" => OssClient::OSS_SIGNATURE_VERSION_V4, "region"=> "cn-hangzhou" ); $ossClient = new OssClient($config); try{ // Temporarily delete multiple objects without specifying their version IDs. This operation adds a delete marker to each object. $objects = array(); $objects[] = new DeleteObjectInfo("<yourObject1Name>"); $objects[] = new DeleteObjectInfo("<yourObject2Name>"); $deletedObjectList = $ossClient->deleteObjectVersions($bucket, $objects); // View the deletion result. if (!empty($deletedObjectList)) { print("deletedObjectList:\n"); foreach ($deletedObjectList as $deletedObjectInfo) { print($deletedObjectInfo->getKey() . ","); print($deletedObjectInfo->getVersionId() . ","); print($deletedObjectInfo->getDeleteMarker() . ","); print($deletedObjectInfo->getDeleteMarkerVersionId() . "\n"); } } } catch(OssException $e) { printf(__FUNCTION__ . ": FAILED\n"); printf($e->getMessage() . "\n"); return; } print(__FUNCTION__ . ": OK" . "\n");
For more information, see DeleteMultipleObjects.