S3: Delete Multiple Objects and Check Per-Object Errors
See more S3 - HTTP Examples
Uses Chilkat.Http with Amazon S3 general purpose buckets and Signature Version 4. These examples do not cover directory buckets/S3 Express, access-point ARNs, Outposts, or other specialized endpoints. They target the commercial AWS partition; other partitions need their documented endpoint suffix.
Load private qa_data/aws/s3.json containing nonempty string fields access_key and secret_key, plus session_token for temporary credentials. Prefer short-lived role credentials. These samples do not discover AWS profiles or refresh credentials. Set the bucket's actual region and replace sample bucket names and object keys. TLS certificate verification is explicitly enabled. Keep detailed HTTP diagnostics private because they can contain credentials or presigned URLs.
Sends an explicit DeleteObjects request with Http.HttpStr and receives the original HttpResponse. XML APIs safely serialize up to 1,000 object identifiers. The request uses quiet mode; successful entries may be absent. HTTP 200 can still contain per-object errors, so every Error element is inspected before reporting success.
The sample uses Crypt2 to sign this fixed POST /?delete= request with SigV4, including Content-MD5, the SHA-256 payload hash, date, and optional session token. Keep the system clock accurate. Do not alter signed components after signing. This signing block is specific to this operation, not a general URL canonicalizer. See AWS header-signing rules.
Requires s3:DeleteObject, and s3:DeleteObjectVersion for explicit versions. Version-specific deletion is permanent. Without a version ID, a versioning-enabled bucket normally gains delete markers instead. Missing keys count as successful deletes. MFA Delete, retention, and legal holds may affect results. Retry only failed identifiers when appropriate, rather than repeating successful versioned deletes.
Chilkat PowerBuilder Downloads
integer li_rc
long li_Success
oleobject loo_StringCompare
oleobject loo_Credentials
long li_StringMatch1
long li_StringMatch2
oleobject loo_Http
long li_StringMatch3
string ls_Region
oleobject loo_Endpoint
string ls_BucketName
oleobject loo_DeleteRequest
string ls_RequestBody
oleobject loo_Host
oleobject loo_Now
oleobject loo_Timestamp
long li_RemovedDashes
long li_RemovedColons
string ls_AmzDate
string ls_DateStamp
oleobject loo_Crypt
string ls_ContentMd5
string ls_PayloadHash
oleobject loo_SignedHeaders
oleobject loo_Canonical
string ls_CanonicalHash
oleobject loo_Scope
oleobject loo_StringToSign
oleobject loo_InitialKey
string ls_DerivedKey
string ls_Signature
oleobject loo_Authorization
oleobject loo_RequestUrl
oleobject loo_Response
oleobject loo_Xml
long li_StringMatch4
long li_Failures
long i
li_Success = 0
loo_StringCompare = create oleobject
li_rc = loo_StringCompare.ConnectToNewObject("Chilkat.StringBuilder")
if li_rc < 0 then
destroy loo_StringCompare
MessageBox("Error","Connecting to COM object failed")
return
end if
// This example requires the Chilkat API to have been previously unlocked.
// See Global Unlock Sample for sample code.
// Load private credentials; never put access keys or session tokens in source.
loo_Credentials = create oleobject
li_rc = loo_Credentials.ConnectToNewObject("Chilkat.JsonObject")
loo_Credentials.Strict = 1
li_Success = loo_Credentials.LoadFile("qa_data/aws/s3.json")
if li_Success <> 1 then
Write-Debug "Could not load private S3 credentials."
destroy loo_StringCompare
destroy loo_Credentials
return
end if
loo_StringCompare.SetString(loo_Credentials.StringOf("access_key"))
li_StringMatch1 = loo_StringCompare.ContentsEqual("",1)
if (loo_Credentials.JsonTypeOf("access_key") <> 1) OR (li_StringMatch1 = 1) then
Write-Debug "Missing or invalid access_key."
destroy loo_StringCompare
destroy loo_Credentials
return
end if
loo_StringCompare.SetString(loo_Credentials.StringOf("secret_key"))
li_StringMatch2 = loo_StringCompare.ContentsEqual("",1)
if (loo_Credentials.JsonTypeOf("secret_key") <> 1) OR (li_StringMatch2 = 1) then
Write-Debug "Missing or invalid secret_key."
destroy loo_StringCompare
destroy loo_Credentials
return
end if
loo_Http = create oleobject
li_rc = loo_Http.ConnectToNewObject("Chilkat.Http")
loo_Http.AwsAccessKey = loo_Credentials.StringOf("access_key")
loo_Http.AwsSecretKey = loo_Credentials.StringOf("secret_key")
if loo_Credentials.HasMember("session_token") = 1 then
loo_StringCompare.SetString(loo_Credentials.StringOf("session_token"))
li_StringMatch3 = loo_StringCompare.ContentsEqual("",1)
if (loo_Credentials.JsonTypeOf("session_token") <> 1) OR (li_StringMatch3 = 1) then
Write-Debug "session_token must be a nonempty string when supplied."
destroy loo_StringCompare
destroy loo_Credentials
destroy loo_Http
return
end if
loo_Http.AwsSessionToken = loo_Credentials.StringOf("session_token")
end if
// Current Chilkat uses SigV4 by default. Do not select deprecated SigV2.
// Set the actual bucket region, not the caller's location.
ls_Region = "us-west-2"
loo_Http.AwsRegion = ls_Region
loo_Endpoint = create oleobject
li_rc = loo_Endpoint.ConnectToNewObject("Chilkat.StringBuilder")
loo_Endpoint.Append("s3.")
loo_Endpoint.Append(ls_Region)
loo_Endpoint.Append(".amazonaws.com")
loo_Http.AwsEndpoint = loo_Endpoint.GetAsString()
loo_Http.RequireSslCertVerify = 1
loo_Http.FollowRedirects = 0
// A region redirect is not success. Correct the endpoint and signing region.
ls_BucketName = "your-bucket-name"
loo_DeleteRequest = create oleobject
li_rc = loo_DeleteRequest.ConnectToNewObject("Chilkat.Xml")
loo_DeleteRequest.Tag = "Delete"
loo_DeleteRequest.AddAttribute("xmlns","http://s3.amazonaws.com/doc/2006-03-01/")
loo_DeleteRequest.UpdateChildContent("Quiet","true")
// XML APIs escape key text. Do NOT URL-encode keys in this XML body.
loo_DeleteRequest.UpdateChildContent("Object[0]|Key","images/seahorse.jpg")
loo_DeleteRequest.UpdateChildContent("Object[1]|Key","images/starfish.jpg")
// For permanent deletion of a specific version, add Object[i]|VersionId
// deliberately. Use separate XML elements, not an embedded string convention.
if (loo_DeleteRequest.NumChildrenHavingTag("Object") < 1) OR (loo_DeleteRequest.NumChildrenHavingTag("Object") > 1000) then
Write-Debug "Use a batch of 1 to 1,000 object identifiers."
destroy loo_StringCompare
destroy loo_Credentials
destroy loo_Http
destroy loo_Endpoint
destroy loo_DeleteRequest
return
end if
ls_RequestBody = loo_DeleteRequest.GetXml()
// Sign this exact POST /?delete= request, including its UTF-8 body and MD5.
// Do not change signed headers, body, endpoint, or query after signing.
loo_Host = create oleobject
li_rc = loo_Host.ConnectToNewObject("Chilkat.StringBuilder")
loo_Host.Append(ls_BucketName)
loo_Host.Append(".")
loo_Host.Append(loo_Endpoint.GetAsString())
loo_Now = create oleobject
li_rc = loo_Now.ConnectToNewObject("Chilkat.CkDateTime")
li_Success = loo_Now.SetFromCurrentSystemTime()
if li_Success <> 1 then
Write-Debug "Could not read system time."
destroy loo_StringCompare
destroy loo_Credentials
destroy loo_Http
destroy loo_Endpoint
destroy loo_DeleteRequest
destroy loo_Host
destroy loo_Now
return
end if
loo_Timestamp = create oleobject
li_rc = loo_Timestamp.ConnectToNewObject("Chilkat.StringBuilder")
loo_Timestamp.Append(loo_Now.GetAsTimestamp(0))
li_RemovedDashes = loo_Timestamp.Replace("-","")
li_RemovedColons = loo_Timestamp.Replace(":","")
ls_AmzDate = loo_Timestamp.GetAsString()
ls_DateStamp = loo_Timestamp.GetRange(0,8,0)
loo_Crypt = create oleobject
li_rc = loo_Crypt.ConnectToNewObject("Chilkat.Crypt2")
loo_Crypt.Charset = "utf-8"
loo_Crypt.HashAlgorithm = "md5"
loo_Crypt.EncodingMode = "base64"
ls_ContentMd5 = loo_Crypt.HashStringENC(ls_RequestBody)
if loo_Crypt.LastMethodSuccess <> 1 then
Write-Debug "Could not hash delete body."
destroy loo_StringCompare
destroy loo_Credentials
destroy loo_Http
destroy loo_Endpoint
destroy loo_DeleteRequest
destroy loo_Host
destroy loo_Now
destroy loo_Timestamp
destroy loo_Crypt
return
end if
loo_Crypt.HashAlgorithm = "sha256"
loo_Crypt.EncodingMode = "hexlower"
ls_PayloadHash = loo_Crypt.HashStringENC(ls_RequestBody)
if loo_Crypt.LastMethodSuccess <> 1 then
Write-Debug "Could not hash delete body."
destroy loo_StringCompare
destroy loo_Credentials
destroy loo_Http
destroy loo_Endpoint
destroy loo_DeleteRequest
destroy loo_Host
destroy loo_Now
destroy loo_Timestamp
destroy loo_Crypt
return
end if
loo_SignedHeaders = create oleobject
li_rc = loo_SignedHeaders.ConnectToNewObject("Chilkat.StringBuilder")
loo_SignedHeaders.Append("content-md5;host;x-amz-content-sha256;x-amz-date")
loo_Canonical = create oleobject
li_rc = loo_Canonical.ConnectToNewObject("Chilkat.StringBuilder")
loo_Canonical.Append("POST~n/~ndelete=~ncontent-md5:")
loo_Canonical.Append(ls_ContentMd5)
loo_Canonical.Append("~nhost:")
loo_Canonical.Append(loo_Host.GetAsString())
loo_Canonical.Append("~nx-amz-content-sha256:")
loo_Canonical.Append(ls_PayloadHash)
loo_Canonical.Append("~nx-amz-date:")
loo_Canonical.Append(ls_AmzDate)
loo_Canonical.Append("~n")
if loo_Credentials.HasMember("session_token") = 1 then
loo_SignedHeaders.Append(";x-amz-security-token")
loo_Canonical.Append("x-amz-security-token:")
loo_Canonical.Append(loo_Credentials.StringOf("session_token"))
loo_Canonical.Append("~n")
loo_Http.SetRequestHeader("x-amz-security-token",loo_Credentials.StringOf("session_token"))
end if
loo_Canonical.Append("~n")
loo_Canonical.Append(loo_SignedHeaders.GetAsString())
loo_Canonical.Append("~n")
loo_Canonical.Append(ls_PayloadHash)
ls_CanonicalHash = loo_Crypt.HashStringENC(loo_Canonical.GetAsString())
if loo_Crypt.LastMethodSuccess <> 1 then
Write-Debug "Could not hash canonical request."
destroy loo_StringCompare
destroy loo_Credentials
destroy loo_Http
destroy loo_Endpoint
destroy loo_DeleteRequest
destroy loo_Host
destroy loo_Now
destroy loo_Timestamp
destroy loo_Crypt
destroy loo_SignedHeaders
destroy loo_Canonical
return
end if
loo_Scope = create oleobject
li_rc = loo_Scope.ConnectToNewObject("Chilkat.StringBuilder")
loo_Scope.Append(ls_DateStamp)
loo_Scope.Append("/")
loo_Scope.Append(ls_Region)
loo_Scope.Append("/s3/aws4_request")
loo_StringToSign = create oleobject
li_rc = loo_StringToSign.ConnectToNewObject("Chilkat.StringBuilder")
loo_StringToSign.Append("AWS4-HMAC-SHA256~n")
loo_StringToSign.Append(ls_AmzDate)
loo_StringToSign.Append("~n")
loo_StringToSign.Append(loo_Scope.GetAsString())
loo_StringToSign.Append("~n")
loo_StringToSign.Append(ls_CanonicalHash)
loo_InitialKey = create oleobject
li_rc = loo_InitialKey.ConnectToNewObject("Chilkat.StringBuilder")
loo_InitialKey.Append("AWS4")
loo_InitialKey.Append(loo_Credentials.StringOf("secret_key"))
loo_Crypt.MacAlgorithm = "hmac"
li_Success = loo_Crypt.SetMacKeyString(loo_InitialKey.GetAsString())
if li_Success <> 1 then
Write-Debug "Could not set signing key."
destroy loo_StringCompare
destroy loo_Credentials
destroy loo_Http
destroy loo_Endpoint
destroy loo_DeleteRequest
destroy loo_Host
destroy loo_Now
destroy loo_Timestamp
destroy loo_Crypt
destroy loo_SignedHeaders
destroy loo_Canonical
destroy loo_Scope
destroy loo_StringToSign
destroy loo_InitialKey
return
end if
ls_DerivedKey = loo_Crypt.MacStringENC(ls_DateStamp)
if loo_Crypt.LastMethodSuccess <> 1 then
Write-Debug "Could not derive date signing key."
destroy loo_StringCompare
destroy loo_Credentials
destroy loo_Http
destroy loo_Endpoint
destroy loo_DeleteRequest
destroy loo_Host
destroy loo_Now
destroy loo_Timestamp
destroy loo_Crypt
destroy loo_SignedHeaders
destroy loo_Canonical
destroy loo_Scope
destroy loo_StringToSign
destroy loo_InitialKey
return
end if
li_Success = loo_Crypt.SetMacKeyEncoded(ls_DerivedKey,"hex")
if li_Success <> 1 then
Write-Debug "Could not set derived signing key."
destroy loo_StringCompare
destroy loo_Credentials
destroy loo_Http
destroy loo_Endpoint
destroy loo_DeleteRequest
destroy loo_Host
destroy loo_Now
destroy loo_Timestamp
destroy loo_Crypt
destroy loo_SignedHeaders
destroy loo_Canonical
destroy loo_Scope
destroy loo_StringToSign
destroy loo_InitialKey
return
end if
ls_DerivedKey = loo_Crypt.MacStringENC(ls_Region)
if loo_Crypt.LastMethodSuccess <> 1 then
Write-Debug "Could not derive signing key."
destroy loo_StringCompare
destroy loo_Credentials
destroy loo_Http
destroy loo_Endpoint
destroy loo_DeleteRequest
destroy loo_Host
destroy loo_Now
destroy loo_Timestamp
destroy loo_Crypt
destroy loo_SignedHeaders
destroy loo_Canonical
destroy loo_Scope
destroy loo_StringToSign
destroy loo_InitialKey
return
end if
li_Success = loo_Crypt.SetMacKeyEncoded(ls_DerivedKey,"hex")
if li_Success <> 1 then
Write-Debug "Could not set derived signing key."
destroy loo_StringCompare
destroy loo_Credentials
destroy loo_Http
destroy loo_Endpoint
destroy loo_DeleteRequest
destroy loo_Host
destroy loo_Now
destroy loo_Timestamp
destroy loo_Crypt
destroy loo_SignedHeaders
destroy loo_Canonical
destroy loo_Scope
destroy loo_StringToSign
destroy loo_InitialKey
return
end if
ls_DerivedKey = loo_Crypt.MacStringENC("s3")
if loo_Crypt.LastMethodSuccess <> 1 then
Write-Debug "Could not derive signing key."
destroy loo_StringCompare
destroy loo_Credentials
destroy loo_Http
destroy loo_Endpoint
destroy loo_DeleteRequest
destroy loo_Host
destroy loo_Now
destroy loo_Timestamp
destroy loo_Crypt
destroy loo_SignedHeaders
destroy loo_Canonical
destroy loo_Scope
destroy loo_StringToSign
destroy loo_InitialKey
return
end if
li_Success = loo_Crypt.SetMacKeyEncoded(ls_DerivedKey,"hex")
if li_Success <> 1 then
Write-Debug "Could not set derived signing key."
destroy loo_StringCompare
destroy loo_Credentials
destroy loo_Http
destroy loo_Endpoint
destroy loo_DeleteRequest
destroy loo_Host
destroy loo_Now
destroy loo_Timestamp
destroy loo_Crypt
destroy loo_SignedHeaders
destroy loo_Canonical
destroy loo_Scope
destroy loo_StringToSign
destroy loo_InitialKey
return
end if
ls_DerivedKey = loo_Crypt.MacStringENC("aws4_request")
if loo_Crypt.LastMethodSuccess <> 1 then
Write-Debug "Could not derive signing key."
destroy loo_StringCompare
destroy loo_Credentials
destroy loo_Http
destroy loo_Endpoint
destroy loo_DeleteRequest
destroy loo_Host
destroy loo_Now
destroy loo_Timestamp
destroy loo_Crypt
destroy loo_SignedHeaders
destroy loo_Canonical
destroy loo_Scope
destroy loo_StringToSign
destroy loo_InitialKey
return
end if
li_Success = loo_Crypt.SetMacKeyEncoded(ls_DerivedKey,"hex")
if li_Success <> 1 then
Write-Debug "Could not set final signing key."
destroy loo_StringCompare
destroy loo_Credentials
destroy loo_Http
destroy loo_Endpoint
destroy loo_DeleteRequest
destroy loo_Host
destroy loo_Now
destroy loo_Timestamp
destroy loo_Crypt
destroy loo_SignedHeaders
destroy loo_Canonical
destroy loo_Scope
destroy loo_StringToSign
destroy loo_InitialKey
return
end if
ls_Signature = loo_Crypt.MacStringENC(loo_StringToSign.GetAsString())
if loo_Crypt.LastMethodSuccess <> 1 then
Write-Debug "Could not sign delete request."
destroy loo_StringCompare
destroy loo_Credentials
destroy loo_Http
destroy loo_Endpoint
destroy loo_DeleteRequest
destroy loo_Host
destroy loo_Now
destroy loo_Timestamp
destroy loo_Crypt
destroy loo_SignedHeaders
destroy loo_Canonical
destroy loo_Scope
destroy loo_StringToSign
destroy loo_InitialKey
return
end if
loo_Authorization = create oleobject
li_rc = loo_Authorization.ConnectToNewObject("Chilkat.StringBuilder")
loo_Authorization.Append("AWS4-HMAC-SHA256 Credential=")
loo_Authorization.Append(loo_Credentials.StringOf("access_key"))
loo_Authorization.Append("/")
loo_Authorization.Append(loo_Scope.GetAsString())
loo_Authorization.Append(", SignedHeaders=")
loo_Authorization.Append(loo_SignedHeaders.GetAsString())
loo_Authorization.Append(", Signature=")
loo_Authorization.Append(ls_Signature)
loo_Http.SetRequestHeader("Authorization",loo_Authorization.GetAsString())
loo_Http.SetRequestHeader("Content-MD5",ls_ContentMd5)
loo_Http.SetRequestHeader("x-amz-content-sha256",ls_PayloadHash)
loo_Http.SetRequestHeader("x-amz-date",ls_AmzDate)
loo_RequestUrl = create oleobject
li_rc = loo_RequestUrl.ConnectToNewObject("Chilkat.StringBuilder")
loo_RequestUrl.Append("https://")
loo_RequestUrl.Append(loo_Host.GetAsString())
loo_RequestUrl.Append("/?delete=")
loo_Response = create oleobject
li_rc = loo_Response.ConnectToNewObject("Chilkat.HttpResponse")
li_Success = loo_Http.HttpStr("POST",loo_RequestUrl.GetAsString(),ls_RequestBody,"utf-8","application/xml",loo_Response)
if li_Success <> 1 then
Write-Debug "Request failed (transport or S3 error). HTTP status: " + string(loo_Http.LastStatus)
// Inspect LastErrorText privately. Do not log credentials or signed URLs.
destroy loo_StringCompare
destroy loo_Credentials
destroy loo_Http
destroy loo_Endpoint
destroy loo_DeleteRequest
destroy loo_Host
destroy loo_Now
destroy loo_Timestamp
destroy loo_Crypt
destroy loo_SignedHeaders
destroy loo_Canonical
destroy loo_Scope
destroy loo_StringToSign
destroy loo_InitialKey
destroy loo_Authorization
destroy loo_RequestUrl
destroy loo_Response
return
end if
if loo_Response.StatusCode <> 200 then
Write-Debug "Unexpected HTTP status: " + string(loo_Response.StatusCode)
destroy loo_StringCompare
destroy loo_Credentials
destroy loo_Http
destroy loo_Endpoint
destroy loo_DeleteRequest
destroy loo_Host
destroy loo_Now
destroy loo_Timestamp
destroy loo_Crypt
destroy loo_SignedHeaders
destroy loo_Canonical
destroy loo_Scope
destroy loo_StringToSign
destroy loo_InitialKey
destroy loo_Authorization
destroy loo_RequestUrl
destroy loo_Response
return
end if
// HTTP 200 can contain per-object errors. Inspect the actual XML body.
loo_Xml = create oleobject
li_rc = loo_Xml.ConnectToNewObject("Chilkat.Xml")
li_Success = loo_Xml.LoadXml(loo_Response.BodyStr)
if li_Success <> 1 then
Write-Debug "Invalid XML response."
destroy loo_StringCompare
destroy loo_Credentials
destroy loo_Http
destroy loo_Endpoint
destroy loo_DeleteRequest
destroy loo_Host
destroy loo_Now
destroy loo_Timestamp
destroy loo_Crypt
destroy loo_SignedHeaders
destroy loo_Canonical
destroy loo_Scope
destroy loo_StringToSign
destroy loo_InitialKey
destroy loo_Authorization
destroy loo_RequestUrl
destroy loo_Response
destroy loo_Xml
return
end if
loo_StringCompare.SetString(loo_Xml.Tag)
li_StringMatch4 = loo_StringCompare.ContentsEqual("DeleteResult",1)
if li_StringMatch4 <> 1 then
Write-Debug "Unexpected XML response root."
destroy loo_StringCompare
destroy loo_Credentials
destroy loo_Http
destroy loo_Endpoint
destroy loo_DeleteRequest
destroy loo_Host
destroy loo_Now
destroy loo_Timestamp
destroy loo_Crypt
destroy loo_SignedHeaders
destroy loo_Canonical
destroy loo_Scope
destroy loo_StringToSign
destroy loo_InitialKey
destroy loo_Authorization
destroy loo_RequestUrl
destroy loo_Response
destroy loo_Xml
return
end if
li_Failures = loo_Xml.NumChildrenHavingTag("Error")
i = 0
do while (i < li_Failures)
loo_Xml.I = i
Write-Debug "Failed key: " + loo_Xml.GetChildContent("Error[i]|Key")
Write-Debug "Error code: " + loo_Xml.GetChildContent("Error[i]|Code")
i = i + 1
loop
if li_Failures <> 0 then
Write-Debug "One or more objects were not deleted. Retry only appropriate failed identifiers."
destroy loo_StringCompare
destroy loo_Credentials
destroy loo_Http
destroy loo_Endpoint
destroy loo_DeleteRequest
destroy loo_Host
destroy loo_Now
destroy loo_Timestamp
destroy loo_Crypt
destroy loo_SignedHeaders
destroy loo_Canonical
destroy loo_Scope
destroy loo_StringToSign
destroy loo_InitialKey
destroy loo_Authorization
destroy loo_RequestUrl
destroy loo_Response
destroy loo_Xml
return
end if
// Quiet=true means success entries need not be returned.
Write-Debug "Delete batch succeeded with no reported object errors."
destroy loo_StringCompare
destroy loo_Credentials
destroy loo_Http
destroy loo_Endpoint
destroy loo_DeleteRequest
destroy loo_Host
destroy loo_Now
destroy loo_Timestamp
destroy loo_Crypt
destroy loo_SignedHeaders
destroy loo_Canonical
destroy loo_Scope
destroy loo_StringToSign
destroy loo_InitialKey
destroy loo_Authorization
destroy loo_RequestUrl
destroy loo_Response
destroy loo_Xml