All Products
Search
Document Center

Object Storage Service:Delete object tags (OSS SDK for PHP V2)

Last Updated:Mar 20, 2026

Use OSS SDK for PHP 2.0 to remove all tags from an object by calling deleteObjectTagging.

Prerequisites

Before you begin, ensure that you have:

  • An OSS bucket with at least one tagged object

  • The oss:DeleteObjectTagging permission. For more information, see Attach a custom policy to a RAM user

  • OSS SDK for PHP 2.0 installed via Composer (vendor/autoload.php available)

Usage notes

  • The sample code uses region ID cn-hangzhou. Replace it with the region where your bucket is located. For a full list of supported regions and endpoints, see Regions and endpoints.

  • By default, the SDK uses a public endpoint. If your application runs in an Alibaba Cloud service in the same region as the bucket, use an internal endpoint.

  • Object tags are key-value pairs. For more information, see Object tagging.

Delete object tags

The following example removes all tags from a specified object.

<?php

// Load Composer dependencies.
require_once __DIR__ . '/../../vendor/autoload.php';

use AlibabaCloud\Oss\V2 as Oss;

// Define command-line arguments.
$optsdesc = [
    "region"   => ['help' => 'The region where the bucket is located.', 'required' => true],  // REQUIRED
    "endpoint" => ['help' => 'The endpoint for accessing OSS.',          'required' => false], // Optional
    "bucket"   => ['help' => 'The bucket name.',                         'required' => true],  // REQUIRED
    "key"      => ['help' => 'The object name.',                         'required' => true],  // REQUIRED
];

// Build long option list for getopt (each option requires a value).
$longopts = array_map(fn($key) => "$key:", array_keys($optsdesc));

// Parse command-line arguments.
$options = getopt("", $longopts);

// Validate required arguments.
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 (AccessKey ID and AccessKey secret).
// Avoid hardcoding credentials in source code.
$credentialsProvider = new Oss\Credentials\EnvironmentVariableCredentialsProvider();

// Initialize the SDK configuration.
$cfg = Oss\Config::loadDefault();
$cfg->setCredentialsProvider($credentialsProvider);
$cfg->setRegion($region);
if (isset($options["endpoint"])) {
    $cfg->setEndpoint($options["endpoint"]);
}

// Create an OSS client.
$client = new Oss\Client($cfg);

// Build the delete request.
$request = new Oss\Models\DeleteObjectTaggingRequest(
    bucket: $bucket,  // REQUIRED — the bucket name
    key: $key         // REQUIRED — the object name
);

// Delete all tags from the object.
$result = $client->deleteObjectTagging($request);

// Print the result.
printf(
    "status code: %s\n" .  // HTTP 204 indicates the tags were deleted successfully.
    "request ID:  %s\n",   // Use the request ID to trace or debug this operation.
    $result->statusCode,
    $result->requestId
);

Response fields

FieldDescription
statusCodeHTTP status code. 204 indicates all tags were deleted successfully.
requestIdUnique ID for this request. Use it when contacting support or debugging.

What's next