All Products
Search
Document Center

Object Storage Service:Delete files (PHP SDK V2)

Last Updated:Mar 20, 2026

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:DeleteObject permission on the object. For details, see Attach a custom policy to a RAM user

  • Your AccessKey ID and AccessKey secret stored as environment variables (OSS_ACCESS_KEY_ID and OSS_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:

ParameterDescriptionExample
--regionRegion where the bucket is locatedcn-hangzhou
--bucketName of the bucketmy-bucket
--keyName of the object to deletelogs/app.log
--endpoint(Optional) Custom endpointoss-cn-hangzhou.aliyuncs.com

References