C# Google Cloud Storage: Delete a File (i.e. Object)

Back to Index

Deletes a fie (also known as an object) from a Google Cloud Storage bucket. The name of the bucket is specified in the URL's path. This example deletes the "starfish.jpg" object from the "chilkat-test" bucket. A successful DELETE is indicated by a 204 response status code with an empty response body.

Documentation: https://cloud.google.com/storage/docs/json_api/v1/objects/delete

CURL Command

curl -X DELETE https://www.googleapis.com/storage/v1/b/chilkat-bucket/o/starfish.jpg?project=MY_CLOUD_STORAGE_PROJECT \
    --header "Authorization: Bearer CLOUD_STORAGE_TOKEN"

C# Example

Chilkat.Rest rest = new Chilkat.Rest();
bool success;

//  URL: https://www.googleapis.com/storage/v1/b/chilkat-bucket/o/starfish.jpg?project=MY_CLOUD_STORAGE_PROJECT
bool bTls = true;
int port = 443;
bool bAutoReconnect = true;
success = rest.Connect("www.googleapis.com",port,bTls,bAutoReconnect);
if (success != true) {
    Debug.WriteLine("ConnectFailReason: " + Convert.ToString(rest.ConnectFailReason));
    Debug.WriteLine(rest.LastErrorText);
    return;
}

rest.AddHeader("Authorization","Bearer CLOUD_STORAGE_TOKEN");

Chilkat.StringBuilder sbResponseBody = new Chilkat.StringBuilder();
success = rest.FullRequestNoBodySb("DELETE","/storage/v1/b/chilkat-bucket/o/starfish.jpg?project=MY_CLOUD_STORAGE_PROJECT",sbResponseBody);
if (success != true) {
    Debug.WriteLine(rest.LastErrorText);
    return;
}

int respStatusCode = rest.ResponseStatusCode;
if (respStatusCode >= 400) {
    Debug.WriteLine("Response Status Code = " + Convert.ToString(respStatusCode));
    Debug.WriteLine("Response Header:");
    Debug.WriteLine(rest.ResponseHeader);
    Debug.WriteLine("Response Body:");
    Debug.WriteLine(sbResponseBody.GetAsString());
    return;
}