Deleting Objects

Last updated: 2020-09-14 11:37:30

    Overview

    This document provides an overview of APIs and SDK sample codes related to deleting objects.

    API Operation Description
    DELETE Object Deleting a single object Deletes a specified object from a bucket
    DELETE Multiple Object Deleting multiple objects Deletes multiple objects from a bucket in a single request

    SDK API Reference

    For the parameters and method descriptions of all the APIs in the SDK, see Api Documentation.

    Deleting a Single Object

    API description

    This API is used to delete a specified object from a bucket.

    Sample code

    try
    {
      string bucket = "examplebucket-1250000000"; // Bucket in the format: BucketName-APPID
      string key = "exampleobject"; // Object key
      DeleteObjectRequest request = new DeleteObjectRequest(bucket, key);
      // Execute the request
      DeleteObjectResult result = cosXml.DeleteObject(request);
      // Request succeeded
      Console.WriteLine(result.GetResultInfo());
    }
    catch (COSXML.CosException.CosClientException clientEx)
    {
      // Request failed
      Console.WriteLine("CosClientException: " + clientEx);
    }
    catch (COSXML.CosException.CosServerException serverEx)
    {
      // Request failed
      Console.WriteLine("CosServerException: " + serverEx.GetInfo());
    }

    Note:

    For the complete sample, go to GitHub.

    Deleting Multiple Objects

    API description

    This API is used to delete multiple objects in a single request.

    Sample code

    try
    {
      string bucket = "examplebucket-1250000000"; // Bucket in the format: BucketName-APPID
      DeleteMultiObjectRequest request = new DeleteMultiObjectRequest(bucket);
      // Set the response mode
      request.SetDeleteQuiet(false);
      // Object key
      string key = "exampleobject"; // Object key
      List<string> objects = new List<string>();
      objects.Add(key);
      request.SetObjectKeys(objects);
      // Execute the request
      DeleteMultiObjectResult result = cosXml.DeleteMultiObjects(request);
      // Request succeeded
      Console.WriteLine(result.GetResultInfo());
    }
    catch (COSXML.CosException.CosClientException clientEx)
    {
      // Request failed
      Console.WriteLine("CosClientException: " + clientEx);
    }
    catch (COSXML.CosException.CosServerException serverEx)
    {
      // Request failed
      Console.WriteLine("CosServerException: " + serverEx.GetInfo());
    }

    Note:

    For the complete sample, go to GitHub.

    Was this page helpful?

    Was this page helpful?

    • Not at all
    • Not very helpful
    • Somewhat helpful
    • Very helpful
    • Extremely helpful
    Send Feedback
    Help