In versioning-enabled buckets, OSS lets you soft-delete or permanently delete an object depending on whether you provide a versionId in the request. You can delete a single object, multiple objects, or objects that share a specific prefix.
Usage notes
-
The code examples in this topic use the region ID
cn-hangzhoufor the China (Hangzhou) region and demonstrate access over the public endpoint. If you access OSS from another Alibaba Cloud service in the same region, use the internal endpoint. For a complete list of OSS regions and endpoints, see Regions and endpoints. -
The examples in this topic read access credentials from environment variables. For information about how to configure access credentials, see Configure access credentials.
-
To delete an object, you must have the
oss:DeleteObjectpermission. For more information, see Attach a custom policy to a RAM user.
Deletion behavior in a versioning-enabled bucket
The deletion behavior in a versioning-enabled bucket depends on whether you specify a versionId.
-
Without specifying a
versionId(soft delete):If you send a delete request without a
versionId, OSS does not permanently delete the object. Instead, it creates a delete marker, which becomes the current version of the object. When you retrieve an object that has a delete marker as its current version, OSS returns a404 Not Founderror. The response includes thex-oss-delete-marker: trueheader and thex-oss-version-idheader, which contains the versionId of the new delete marker. A value oftrueforx-oss-delete-markerindicates that the version associated with the returnedx-oss-version-idis a delete marker.To learn how to restore a soft-deleted object, see Recover files.
-
Specifying a
versionId(permanent delete):If you specify a
versionIdin theparamsparameter during a delete operation, OSS permanently deletes that version. To delete the version whose ID is "null", addparams['versionId'] = "null"to theparamsparameter.
Examples
Related documentation
-
For more information about the DeleteObject API operation, see DeleteObject.
-
For more information about the DeleteMultipleObjects API operation, see DeleteMultipleObjects.