Delete file
Updated at:2025-11-03
Delete file
Delete single file
The following code can be referenced to delete an object:
Cpp
1int deleteObject(Client& client,const std::string bucketName, const std::string objectKey) {
2 // Delete Object
3 return client.delete_object(bucketName, objectKey); //Specify the name of the bucket where the object to be deleted is located and the name of the object
4}
Delete multiple files
To delete multiple files, deleteMultiObject uses a concurrent API to improve the throughput of requests. The code is as follows:
C++
1int deleteMultiObject(Client& client, const& std::string bucketName, const std::vector<std::string>& objects) {
2 //Delete objects in batches
3 std::vector<BceRequestContext> ctx(objects.size());
4 int i = 0;
5 for (const std::string& objectKey : objects) {
6 //Construct context information
7 ctx[i].request = new DeleteObjectRequest(bucketName, objectKey);
8 ctx[i].response = new DeleteObjectResponse;
9 ctx[i].is_own = true;//Automatically destruct request and response
10 ++i;
11 }
12 // Concurrent requests
13 int ret = client.send_request((int)ctx.size(), &ctx.front());
14 if (ret != 0) return ret;
15 //Process response
16 for (size_t i = 0; i < ctx.size(); ++i) {
17 DeleteObjectRequest* request = (DeleteObjectRequest*)ctx[i].request;
18 DeleteObjectResponse* response = (DeleteObjectResponse*)ctx[i].response;
19 if (response->is_fail()) {//If there is a failure in deletion requests, handle this situation
20 std::cout << "delete objectKey=" << request->object_name() << " failed"
21 << " reason: " << response->error().message();
22 ret = RET_SERVICE_ERROR;
23 }
24 }
25 return ret;
26}
