This topic describes how to delete a single object, multiple objects, or objects whose names contain a specified prefix from a versioning-enabled bucket.
Usage notes
In this topic, the public endpoint of the China (Hangzhou) region is used. If you want to access OSS by using other Alibaba Cloud services in the same region as OSS, use an internal endpoint. For more information about the regions and endpoints supported by OSS, 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 a versioning-enabled bucket
When you delete an object from a versioning-enabled bucket, you must determine whether to specify a version ID in the request.
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 latest 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
andx-oss-version-id
that indicates the version ID of the delete marker are included in the response.If the value of
x-oss-delete-marker
is true, the value ofx-oss-version-id
is the version ID of a delete marker.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 inparams
. To delete the version whose ID is null, addparams['versionId'] = "null"
toparams
. OSS identifies the string "null" as the ID of the version to delete and deletes the version whose ID is null.
Delete a single object
Permanently delete
The following sample code provides an example on how to permanently delete an object from a versioning-enabled bucket by specifying the version ID of the object in the request. You cannot restore the specified version of the object after it is permanently deleted.
#include <alibabacloud/oss/OssClient.h>
using namespace AlibabaCloud::OSS;
int main(void)
{
/* Initialize information about the account that is used to access OSS. */
/* Specify the endpoint of the region in which the bucket is located. For example, if the bucket is located in the China (Hangzhou) region, set the endpoint to https://oss-cn-hangzhou.aliyuncs.com. */
std::string Endpoint = "yourEndpoint";
/* Specify the name of the bucket. Example: examplebucket. */
std::string BucketName = "examplebucket";
/* Specify the full path of the object. Do not include the bucket name in the full path. Example: exampledir/exampleobject.txt. */
std::string ObjectName = "exampledir/exampleobject.txt";
/* Initialize resources such as network resources. */
InitializeSdk();
ClientConfiguration conf;
/* 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. */
auto credentialsProvider = std::make_shared<EnvironmentVariableCredentialsProvider>();
OssClient client(Endpoint, credentialsProvider, conf);
/* Delete the object with the specified version ID or the object whose current version is a delete marker with the specified version ID. */
auto outcome = client.DeleteObject(DeleteObjectRequest(BucketName, ObjectName, "yourObjectVersionIdOrDeleteMarkerVersionId"));
/* If the version ID of an object is specified in the request, the value of the delete_marker header in the response is None and the value of the versionId header in the response is the version ID specified in the request. */
/* If the version ID of a delete marker is specified in the request, the value of the delete_marker header in the response is True and the value of the versionId header in the response is the version ID specified in the request. */
if (!outcome.isSuccess()) {
/* Handle exceptions. */
std::cout << "DeleteObject fail" <<
",code:" << outcome.error().Code() <<
",message:" << outcome.error().Message() <<
",requestId:" << outcome.error().RequestId() << std::endl;
return -1;
}
/* Release resources such as network resources. */
ShutdownSdk();
return 0;
}
Temporarily delete
The following sample code provides an example on how to temporarily delete an object from a versioning-enabled bucket without specifying a version ID. You can restore the current version of the object after it is temporarily deleted.
#include <alibabacloud/oss/OssClient.h>
using namespace AlibabaCloud::OSS;
int main(void)
{
/* Initialize information about the account that is used to access OSS. */
/* Specify the endpoint of the region in which the bucket is located. For example, if the bucket is located in the China (Hangzhou) region, set the endpoint to https://oss-cn-hangzhou.aliyuncs.com. */
std::string Endpoint = "yourEndpoint";
/* Specify the name of the bucket. Example: examplebucket. */
std::string BucketName = "examplebucket";
/* Specify the full path of the object. Do not include the bucket name in the full path. Example: exampledir/exampleobject.txt. */
std::string ObjectName = "exampledir/exampleobject.txt";
/* Initialize resources such as network resources. */
InitializeSdk();
ClientConfiguration conf;
/* 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. */
auto credentialsProvider = std::make_shared<EnvironmentVariableCredentialsProvider>();
OssClient client(Endpoint, credentialsProvider, conf);
DeleteObjectRequest request(BucketName, ObjectName);
/* Temporarily delete an object without specifying its version ID. A delete marker is added to the object. */
auto outcome = client.DeleteObject(request);
/* Display the version ID of the added delete marker. */
if (outcome.isSuccess()) {
std::cout << "versionid:" << outcome.result().VersionId() << ",DeleteMarker:" << outcome.result().DeleteMarker() << std::endl;
}
else {
/* Handle exceptions. */
std::cout << "PutObject fail" <<
",code:" << outcome.error().Code() <<
",message:" << outcome.error().Message() <<
",requestId:" << outcome.error().RequestId() << std::endl;
return -1;
}
/* Release resources such as network resources. */
ShutdownSdk();
return 0;
}
Delete multiple objects
Permanently delete
The following sample code provides an example on how to permanently delete objects with specified version IDs or objects whose current versions are delete markers with specified version IDs. You cannot restore the specified versions of the objects after they are permanently deleted.
#include <alibabacloud/oss/OssClient.h>
using namespace AlibabaCloud::OSS;
int main(void)
{
/* Initialize information about the account that is used to access OSS. */
/* Specify the endpoint of the region in which the bucket is located. For example, if the bucket is located in the China (Hangzhou) region, set the endpoint to https://oss-cn-hangzhou.aliyuncs.com. */
std::string Endpoint = "yourEndpoint";
/* Specify the name of the bucket. Example: examplebucket. */
std::string BucketName = "examplebucket";
/* Initialize resources such as network resources. */
InitializeSdk();
ClientConfiguration conf;
/* 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. */
auto credentialsProvider = std::make_shared<EnvironmentVariableCredentialsProvider>();
OssClient client(Endpoint, credentialsProvider, conf);
DeleteObjectVersionsRequest request(BucketName);
/* Specify the version IDs of the objects or delete markers you want to delete. */
ObjectIdentifier obj1("yourObject1Name");
obj1.setVersionId("yourVersionId");
ObjectIdentifier obj2("yourObject2Name");
obj2.setVersionId("obj2_del_marker_versionid");
request.addObject(obj1);
request.addObject(obj2);
/* Delete the objects with specified version IDs or the objects whose current versions are delete markers with specified version IDs. */
auto outcome = client.DeleteObjectVersions(request);
if (!outcome.isSuccess()) {
/* Handle exceptions. */
std::cout << "DeleteObjectVersions fail" <<
",code:" << outcome.error().Code() <<
",message:" << outcome.error().Message() <<
",requestId:" << outcome.error().RequestId() << std::endl;
return -1;
}
/* Release resources such as network resources. */
ShutdownSdk();
return 0;
}
Temporarily delete
The following sample code provides an example on how to temporarily delete multiple objects from a versioning-enabled bucket without specifying version IDs. You can restore the current versions of the objects after they are temporarily deleted.
#include <alibabacloud/oss/OssClient.h>
using namespace AlibabaCloud::OSS;
int main(void)
{
/* Initialize information about the account that is used to access OSS. */
/* Specify the endpoint of the region in which the bucket is located. For example, if the bucket is located in the China (Hangzhou) region, set the endpoint to https://oss-cn-hangzhou.aliyuncs.com. */
std::string Endpoint = "yourEndpoint";
/* Specify the name of the bucket. Example: examplebucket. */
std::string BucketName = "examplebucket";
/* Initialize resources such as network resources. */
InitializeSdk();
ClientConfiguration conf;
/* 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. */
auto credentialsProvider = std::make_shared<EnvironmentVariableCredentialsProvider>();
OssClient client(Endpoint, credentialsProvider, conf);
DeleteObjectVersionsRequest request(BucketName);
/* Specify the names of the objects that you want to delete. */
ObjectIdentifier obj1("ObjectName1");
ObjectIdentifier obj2("ObjectName2");
ObjectIdentifier obj3("ObjectName3");
request.addObject(obj1);
request.addObject(obj2);
request.addObject(obj3);
/* Delete the objects. */
auto outcome = client.DeleteObjectVersions(request);
if (outcome.isSuccess()) {
for (auto const &obj : outcome.result().DeletedObjects()) {
std::cout << "versionid:" << obj.VersionId() << ",DeleteMarker:" << obj.DeleteMarker() << std::endl;
}
}
else {
/* Handle exceptions. */
std::cout << "DeleteObjectVersions fail" <<
",code:" << outcome.error().Code() <<
",message:" << outcome.error().Message() <<
",requestId:" << outcome.error().RequestId() << std::endl;
return -1;
}
/* Release resources such as network resources. */
ShutdownSdk();
return 0;
}
Delete objects whose names contain a specified prefix
The following sample code provides an example on how to delete objects whose names contain a specified prefix:
#include <alibabacloud/oss/OssClient.h>
using namespace AlibabaCloud::OSS;
int main(void)
{
/* Initialize information about the account that is used to access OSS. */
/* Specify the endpoint of the region in which the bucket is located. For example, if the bucket is located in the China (Hangzhou) region, set the endpoint to https://oss-cn-hangzhou.aliyuncs.com. */
std::string Endpoint = "yourEndpoint";
/* Specify the name of the bucket. Example: examplebucket. */
std::string BucketName = "examplebucket";
/* Initialize resources such as network resources. */
InitializeSdk();
ClientConfiguration conf;
/* 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. */
auto credentialsProvider = std::make_shared<EnvironmentVariableCredentialsProvider>();
OssClient client(Endpoint, credentialsProvider, conf);
ListObjectVersionsRequest request(BucketName);
bool IsTruncated = false;
do {
request.setPrefix("yourkeyPrefix");
auto outcome = client.ListObjectVersions(request);
if (outcome.isSuccess()) {
/* Display the versions of the listed objects and delete markers. */
for (auto const &marker : outcome.result().DeleteMarkerSummarys()) {
client.DeleteObject(DeleteObjectRequest(BucketName, marker.Key(), marker.VersionId()));
}
/* List all versions of the objects whose names contain the specified prefix and delete the versions. */
for (auto const &obj : outcome.result().ObjectVersionSummarys()) {
client.DeleteObject(DeleteObjectRequest(BucketName, obj.Key(), obj.VersionId()));
}
}
else {
std::cout << "ListObjectVersions fail" <<
",code:" << outcome.error().Code() <<
",message:" << outcome.error().Message() <<
",requestId:" << outcome.error().RequestId() << std::endl;
break;
}
request.setKeyMarker(outcome.result().NextKeyMarker());
request.setVersionIdMarker(outcome.result().NextVersionIdMarker());
IsTruncated = outcome.result().IsTruncated();
} while (IsTruncated);
/* Release resources such as network resources. */
ShutdownSdk();
return 0;
}
References
For more information about the API operation that you can call to delete a single object, see DeleteObject.
For more information about the API operation that you can call to delete multiple objects, see DeleteMultipleObjects.