All Products
Search
Document Center

Object Storage Service:Delete objects

Last Updated:Mar 11, 2024

This topic describes how to delete a single object, multiple objects, or objects whose names contain a specified prefix from a versioned Object Storage Service (OSS) bucket.

Usage notes

  • In this topic, the public endpoint of the China (Hangzhou) region is used. If you want to access OSS from other Alibaba Cloud services in the same region as OSS, use an internal endpoint. For more information about OSS 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:DeleteObject permission. For more information, see Attach a custom policy to a RAM user.

Delete operations on objects in a versioned bucket

  • Delete an object without specifying a version ID (temporary deletion)

    By default, if you do not specify the version ID of the object that you want to delete in the request, OSS does not delete the current version of the object but adds a delete marker to the object as the new current version. If you perform the GetObject operation on the object, OSS identifies the current version of the object as a delete marker and returns 404 Not Found. In addition, header:x-oss-delete-marker = true and x-oss-version-id that indicates the version ID of the delete marker are included in the response.

  • Delete an object by specifying a version ID (permanent deletion)

    If you specify the version ID of the object that you want to delete in the request, OSS permanently deletes the specified version of the object based on the versionId parameter specified in params. To delete an object version whose version ID is null, include params['versionId'] = "null" in params in the request. OSS identifies the string "null" as the ID of the version to delete and deletes the version whose ID is null.

Delete an object at a time

The following examples describe how to permanently or temporarily delete a single object from a versioned bucket.

  • Permanent deletion

    The following sample code provides an example on how to permanently delete an object with a specified version ID from a versioned bucket:

    <?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();
    // In this example, the endpoint of the China (Hangzhou) region is used. Specify 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,
        );
        $ossClient = new OssClient($config);
    
    try{
        // Delete the specified version of the object. 
        $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 sample code provides an example on how to temporarily delete an object from a versioned bucket by sending a request in which no version ID is specified:

    <?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();
    // In this example, the endpoint of the China (Hangzhou) region is used. Specify your actual endpoint. 
    $endpoint = "https://oss-cn-hangzhou.aliyuncs.com";
    $bucket= "<yourBucketName>";
    $object = "<yourObjectName>";
    
    $config = array(
            "provider" => $provider,
            "endpoint" => $endpoint,
        );
        $ossClient = new OssClient($config);
    
    try{
        // Temporarily delete an object without specifying its version ID. A delete marker is added 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 how to delete an object at a time, see DeleteObject.

Delete multiple objects at a time

The following examples describe how to permanently or temporarily delete multiple objects at a time from a versioned bucket.

  • Permanent deletion

    The following sample code provides an example on how to permanently delete multiple objects with specified version IDs from a versioned bucket:

    <?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();
    // In this example, the endpoint of the China (Hangzhou) region is used. Specify your actual endpoint. 
    $endpoint = "https://oss-cn-hangzhou.aliyuncs.com";
    $bucket= "<yourBucketName>";
    
    $config = array(
            "provider" => $provider,
            "endpoint" => $endpoint,
        );
        $ossClient = new OssClient($config);
    
    try{
        // Delete the objects with the specified version IDs. 
        $objects = array(); 
        $objects[] = new DeleteObjectInfo("<yourObject1Name>", "<object1VersionId>");
        $objects[] = new DeleteObjectInfo("<yourObject2Name>", "<object2VersionId>");
        
        $deletedObjectList = $ossClient->deleteObjectVersions($bucket, $objects);
    
        // Display the deleted objects. 
        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");
  • Temporary deletion

    The following sample code provides an example on how to temporarily delete multiple objects from a versioned bucket by sending a request in which no version IDs are specified:

    <?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();
    // In this example, the endpoint of the China (Hangzhou) region is used. Specify your actual endpoint. 
    $endpoint = "https://oss-cn-hangzhou.aliyuncs.com";
    $bucket= "<yourBucketName>";
    
    $config = array(
            "provider" => $provider,
            "endpoint" => $endpoint,
        );
        $ossClient = new OssClient($config);
    
    try{
        // Temporarily delete multiple objects without specifying their version IDs. Delete markers are added to the objects. 
            $objects = array(); 
        $objects[] = new DeleteObjectInfo("<yourObject1Name>");
        $objects[] = new DeleteObjectInfo("<yourObject2Name>");
        
        $deletedObjectList = $ossClient->deleteObjectVersions($bucket, $objects);
    
        // Display the deleted objects. 
        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 how to delete multiple objects at a time, see DeleteMultipleObjects.