All Products
Search
Document Center

Object Storage Service:Delete files (PHP SDK V1)

Last Updated:Oct 12, 2025

This topic describes how to delete one or more files (objects) and files with a specified prefix from a versioning-enabled 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 files, you must have the oss:DeleteObject permission. For more information, see Grant custom permissions to a RAM user.

Delete behaviors in versioning-enabled buckets

  • Without a specified versionId (soft delete):

    If you perform a delete operation without specifying a versionId, a delete marker is inserted for the current version of the object instead of the object being deleted. When you call the GetObject operation, OSS detects the delete marker and returns a 404 Not Found error. The response also includes the x-oss-delete-marker: true header and the version ID of the new delete marker in the x-oss-version-id header.

  • With a specified versionId (permanent delete):

    If you perform a delete operation and specify a versionId, OSS permanently deletes the object version that corresponds to the versionId parameter in params. To delete a version with the ID "null", add params['versionId'] = 'null' to the params array. OSS treats the string "null" as the versionId and deletes the object version that has the "null" versionId.

Delete a single file

The following examples show how to permanently and softly delete a single object.

  • Permanent delete

    The following code shows how to permanently delete an object by specifying its versionId:

    <?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 running this sample code, make sure that the OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET environment variables are set.
    $provider = new EnvironmentVariableCredentialsProvider();
    // The endpoint is set to China (Hangzhou) in this example. Specify the endpoint based on your region.
    $endpoint = "https://oss-cn-hangzhou.aliyuncs.com";
    $bucket= "<yourBucketName>";
    // Specify the full path of the object. Do not include the bucket name. 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 versionId.
        $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");        
  • Soft delete

    The following code shows how to soft-delete an object without specifying its versionId:

    <?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 running this sample code, make sure that the OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET environment variables are set.
    $provider = new EnvironmentVariableCredentialsProvider();
    // The endpoint is set to China (Hangzhou) in this example. Specify the endpoint based on your region.
    $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{
        // Softly delete the object without specifying a versionId. 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 about deleting a single file, see DeleteObject.

Delete multiple files

The following examples show how to permanently and softly delete multiple objects.

  • Permanent delete

    The following code shows how to permanently delete multiple objects by specifying their versionIds:

    <?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 running this sample code, make sure that the OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET environment variables are set.
    $provider = new EnvironmentVariableCredentialsProvider();
    // The endpoint is set to China (Hangzhou) in this example. Specify the endpoint based on your region.
    $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 versionIds.
        $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 softly delete multiple objects without specifying their versionIds:

    <?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 running this sample code, make sure that the OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET environment variables are set.
    $provider = new EnvironmentVariableCredentialsProvider();
    // The endpoint is set to China (Hangzhou) in this example. Specify the endpoint based on your region.
    $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{
        // Softly delete multiple objects without specifying versionIds. This operation adds delete markers to the objects.
            $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 about deleting multiple files, see DeleteMultipleObjects.