Delete an object from an Object Storage Service (OSS) bucket using OSS SDK for PHP V2.
Prerequisites
Before you begin, ensure that you have:
An OSS bucket in the target region
The
oss:DeleteObjectpermission on the object. For details, see Attach a custom policy to a RAM userYour AccessKey ID and AccessKey secret stored as environment variables (
OSS_ACCESS_KEY_IDandOSS_ACCESS_KEY_SECRET)
Usage notes
The sample code uses the China (Hangzhou) region (
cn-hangzhou) with a public endpoint by default. To access OSS from another Alibaba Cloud service in the same region, specify an internal endpoint. For supported regions and endpoints, see Regions and endpoints.
Delete an object
The following sample code loads credentials from environment variables, builds a DeleteObjectRequest, and calls deleteObject(). A successful deletion returns HTTP 204.
<?php
// Load dependency libraries.
require_once __DIR__ . '/../vendor/autoload.php';
use AlibabaCloud\Oss\V2 as Oss;
// Define command-line parameters.
$optsdesc = [
"region" => ['help' => 'The region where the bucket is located.', 'required' => true],
"endpoint" => ['help' => 'The endpoint for accessing OSS.', 'required' => false],
"bucket" => ['help' => 'The name of the bucket.', 'required' => true],
// Requires the oss:DeleteObject permission on this object.
"key" => ['help' => 'The name of the object.', 'required' => true],
];
$longopts = array_map(fn($key) => "$key:", array_keys($optsdesc));
$options = getopt("", $longopts);
// Validate required parameters.
foreach ($optsdesc as $key => $value) {
if ($value['required'] && empty($options[$key])) {
echo "Error: --$key is required. {$value['help']}" . PHP_EOL;
exit(1);
}
}
$region = $options["region"];
$bucket = $options["bucket"];
$key = $options["key"];
// Load credentials from environment variables.
// Set OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET before running this script.
$credentialsProvider = new Oss\Credentials\EnvironmentVariableCredentialsProvider();
// Initialize the OSS client.
$cfg = Oss\Config::loadDefault();
$cfg->setCredentialsProvider($credentialsProvider);
$cfg->setRegion($region);
if (isset($options["endpoint"])) {
$cfg->setEndpoint($options["endpoint"]);
}
$client = new Oss\Client($cfg);
// Delete the object.
try {
$request = new Oss\Models\DeleteObjectRequest(bucket: $bucket, key: $key);
$result = $client->deleteObject($request);
// HTTP 204 indicates the object was deleted successfully.
if ($result->statusCode === 204) {
echo "Deleted successfully." . PHP_EOL;
}
echo "Request ID: " . $result->requestId . PHP_EOL;
} catch (\Exception $e) {
echo "Failed to delete object: " . $e->getMessage() . PHP_EOL;
exit(1);
}Replace the following placeholders when running the script:
| Parameter | Description | Example |
|---|---|---|
--region | Region where the bucket is located | cn-hangzhou |
--bucket | Name of the bucket | my-bucket |
--key | Name of the object to delete | logs/app.log |
--endpoint | (Optional) Custom endpoint | oss-cn-hangzhou.aliyuncs.com |
References
For the complete sample code, see DeleteObject.php on GitHub.