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 DataFlex Downloads
Use ChilkatAx-win32.pkg
Procedure Test
Boolean iSuccess
Handle hoStringCompare
Handle hoCredentials
Boolean iStringMatch1
Boolean iStringMatch2
Handle hoHttp
Boolean iStringMatch3
String sRegion
Handle hoEndpoint
String sBucketName
Handle hoDeleteRequest
String sRequestBody
Handle hoHost
Handle hoNow
Handle hoTimestamp
Integer iRemovedDashes
Integer iRemovedColons
String sAmzDate
String sDateStamp
Handle hoCrypt
String sContentMd5
String sPayloadHash
Handle hoSignedHeaders
Handle hoCanonical
String sCanonicalHash
Handle hoScope
Handle hoStringToSign
Handle hoInitialKey
String sDerivedKey
String sSignature
Handle hoAuthorization
Handle hoRequestUrl
Variant vResponse
Handle hoResponse
Handle hoXml
Boolean iStringMatch4
Integer iFailures
Integer i
String sTemp1
Integer iTemp1
Integer iTemp2
Boolean bTemp1
Move False To iSuccess
Get Create (RefClass(cComChilkatStringBuilder)) To hoStringCompare
If (Not(IsComObjectCreated(hoStringCompare))) Begin
Send CreateComObject of hoStringCompare
End
// 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.
Get Create (RefClass(cComChilkatJsonObject)) To hoCredentials
If (Not(IsComObjectCreated(hoCredentials))) Begin
Send CreateComObject of hoCredentials
End
Set ComStrict Of hoCredentials To True
Get ComLoadFile Of hoCredentials "qa_data/aws/s3.json" To iSuccess
If (iSuccess <> True) Begin
Showln "Could not load private S3 credentials."
Procedure_Return
End
Get ComStringOf Of hoCredentials "access_key" To sTemp1
Get ComSetString Of hoStringCompare sTemp1 To iSuccess
Get ComContentsEqual Of hoStringCompare "" True To iStringMatch1
Get ComJsonTypeOf Of hoCredentials "access_key" To iTemp1
If ((iTemp1 <> 1) Or (iStringMatch1 = True)) Begin
Showln "Missing or invalid access_key."
Procedure_Return
End
Get ComStringOf Of hoCredentials "secret_key" To sTemp1
Get ComSetString Of hoStringCompare sTemp1 To iSuccess
Get ComContentsEqual Of hoStringCompare "" True To iStringMatch2
Get ComJsonTypeOf Of hoCredentials "secret_key" To iTemp1
If ((iTemp1 <> 1) Or (iStringMatch2 = True)) Begin
Showln "Missing or invalid secret_key."
Procedure_Return
End
Get Create (RefClass(cComChilkatHttp)) To hoHttp
If (Not(IsComObjectCreated(hoHttp))) Begin
Send CreateComObject of hoHttp
End
Get ComStringOf Of hoCredentials "access_key" To sTemp1
Set ComAwsAccessKey Of hoHttp To sTemp1
Get ComStringOf Of hoCredentials "secret_key" To sTemp1
Set ComAwsSecretKey Of hoHttp To sTemp1
Get ComHasMember Of hoCredentials "session_token" To bTemp1
If (bTemp1 = True) Begin
Get ComStringOf Of hoCredentials "session_token" To sTemp1
Get ComSetString Of hoStringCompare sTemp1 To iSuccess
Get ComContentsEqual Of hoStringCompare "" True To iStringMatch3
Get ComJsonTypeOf Of hoCredentials "session_token" To iTemp1
If ((iTemp1 <> 1) Or (iStringMatch3 = True)) Begin
Showln "session_token must be a nonempty string when supplied."
Procedure_Return
End
Get ComStringOf Of hoCredentials "session_token" To sTemp1
Set ComAwsSessionToken Of hoHttp To sTemp1
End
// Current Chilkat uses SigV4 by default. Do not select deprecated SigV2.
// Set the actual bucket region, not the caller's location.
Move "us-west-2" To sRegion
Set ComAwsRegion Of hoHttp To sRegion
Get Create (RefClass(cComChilkatStringBuilder)) To hoEndpoint
If (Not(IsComObjectCreated(hoEndpoint))) Begin
Send CreateComObject of hoEndpoint
End
Get ComAppend Of hoEndpoint "s3." To iSuccess
Get ComAppend Of hoEndpoint sRegion To iSuccess
Get ComAppend Of hoEndpoint ".amazonaws.com" To iSuccess
Get ComGetAsString Of hoEndpoint To sTemp1
Set ComAwsEndpoint Of hoHttp To sTemp1
Set ComRequireSslCertVerify Of hoHttp To True
Set ComFollowRedirects Of hoHttp To False
// A region redirect is not success. Correct the endpoint and signing region.
Move "your-bucket-name" To sBucketName
Get Create (RefClass(cComChilkatXml)) To hoDeleteRequest
If (Not(IsComObjectCreated(hoDeleteRequest))) Begin
Send CreateComObject of hoDeleteRequest
End
Set ComTag Of hoDeleteRequest To "Delete"
Get ComAddAttribute Of hoDeleteRequest "xmlns" "http://s3.amazonaws.com/doc/2006-03-01/" To iSuccess
Send ComUpdateChildContent To hoDeleteRequest "Quiet" "true"
// XML APIs escape key text. Do NOT URL-encode keys in this XML body.
Send ComUpdateChildContent To hoDeleteRequest "Object[0]|Key" "images/seahorse.jpg"
Send ComUpdateChildContent To hoDeleteRequest "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.
Get ComNumChildrenHavingTag Of hoDeleteRequest "Object" To iTemp1
Get ComNumChildrenHavingTag Of hoDeleteRequest "Object" To iTemp2
If ((iTemp1 < 1) Or (iTemp2 > 1000)) Begin
Showln "Use a batch of 1 to 1,000 object identifiers."
Procedure_Return
End
Get ComGetXml Of hoDeleteRequest To sRequestBody
// Sign this exact POST /?delete= request, including its UTF-8 body and MD5.
// Do not change signed headers, body, endpoint, or query after signing.
Get Create (RefClass(cComChilkatStringBuilder)) To hoHost
If (Not(IsComObjectCreated(hoHost))) Begin
Send CreateComObject of hoHost
End
Get ComAppend Of hoHost sBucketName To iSuccess
Get ComAppend Of hoHost "." To iSuccess
Get ComGetAsString Of hoEndpoint To sTemp1
Get ComAppend Of hoHost sTemp1 To iSuccess
Get Create (RefClass(cComCkDateTime)) To hoNow
If (Not(IsComObjectCreated(hoNow))) Begin
Send CreateComObject of hoNow
End
Get ComSetFromCurrentSystemTime Of hoNow To iSuccess
If (iSuccess <> True) Begin
Showln "Could not read system time."
Procedure_Return
End
Get Create (RefClass(cComChilkatStringBuilder)) To hoTimestamp
If (Not(IsComObjectCreated(hoTimestamp))) Begin
Send CreateComObject of hoTimestamp
End
Get ComGetAsTimestamp Of hoNow False To sTemp1
Get ComAppend Of hoTimestamp sTemp1 To iSuccess
Get ComReplace Of hoTimestamp "-" "" To iRemovedDashes
Get ComReplace Of hoTimestamp ":" "" To iRemovedColons
Get ComGetAsString Of hoTimestamp To sAmzDate
Get ComGetRange Of hoTimestamp 0 8 False To sDateStamp
Get Create (RefClass(cComChilkatCrypt2)) To hoCrypt
If (Not(IsComObjectCreated(hoCrypt))) Begin
Send CreateComObject of hoCrypt
End
Set ComCharset Of hoCrypt To "utf-8"
Set ComHashAlgorithm Of hoCrypt To "md5"
Set ComEncodingMode Of hoCrypt To "base64"
Get ComHashStringENC Of hoCrypt sRequestBody To sContentMd5
Get ComLastMethodSuccess Of hoCrypt To bTemp1
If (bTemp1 <> True) Begin
Showln "Could not hash delete body."
Procedure_Return
End
Set ComHashAlgorithm Of hoCrypt To "sha256"
Set ComEncodingMode Of hoCrypt To "hexlower"
Get ComHashStringENC Of hoCrypt sRequestBody To sPayloadHash
Get ComLastMethodSuccess Of hoCrypt To bTemp1
If (bTemp1 <> True) Begin
Showln "Could not hash delete body."
Procedure_Return
End
Get Create (RefClass(cComChilkatStringBuilder)) To hoSignedHeaders
If (Not(IsComObjectCreated(hoSignedHeaders))) Begin
Send CreateComObject of hoSignedHeaders
End
Get ComAppend Of hoSignedHeaders "content-md5;host;x-amz-content-sha256;x-amz-date" To iSuccess
Get Create (RefClass(cComChilkatStringBuilder)) To hoCanonical
If (Not(IsComObjectCreated(hoCanonical))) Begin
Send CreateComObject of hoCanonical
End
Get ComAppend Of hoCanonical "POST" + (character(10)) + "/" + (character(10)) + "delete=" + (character(10)) + "content-md5:" To iSuccess
Get ComAppend Of hoCanonical sContentMd5 To iSuccess
Get ComAppend Of hoCanonical (character(10)) + "host:" To iSuccess
Get ComGetAsString Of hoHost To sTemp1
Get ComAppend Of hoCanonical sTemp1 To iSuccess
Get ComAppend Of hoCanonical (character(10)) + "x-amz-content-sha256:" To iSuccess
Get ComAppend Of hoCanonical sPayloadHash To iSuccess
Get ComAppend Of hoCanonical (character(10)) + "x-amz-date:" To iSuccess
Get ComAppend Of hoCanonical sAmzDate To iSuccess
Get ComAppend Of hoCanonical (character(10)) To iSuccess
Get ComHasMember Of hoCredentials "session_token" To bTemp1
If (bTemp1 = True) Begin
Get ComAppend Of hoSignedHeaders ";x-amz-security-token" To iSuccess
Get ComAppend Of hoCanonical "x-amz-security-token:" To iSuccess
Get ComStringOf Of hoCredentials "session_token" To sTemp1
Get ComAppend Of hoCanonical sTemp1 To iSuccess
Get ComAppend Of hoCanonical (character(10)) To iSuccess
Get ComStringOf Of hoCredentials "session_token" To sTemp1
Send ComSetRequestHeader To hoHttp "x-amz-security-token" sTemp1
End
Get ComAppend Of hoCanonical (character(10)) To iSuccess
Get ComGetAsString Of hoSignedHeaders To sTemp1
Get ComAppend Of hoCanonical sTemp1 To iSuccess
Get ComAppend Of hoCanonical (character(10)) To iSuccess
Get ComAppend Of hoCanonical sPayloadHash To iSuccess
Get ComGetAsString Of hoCanonical To sTemp1
Get ComHashStringENC Of hoCrypt sTemp1 To sCanonicalHash
Get ComLastMethodSuccess Of hoCrypt To bTemp1
If (bTemp1 <> True) Begin
Showln "Could not hash canonical request."
Procedure_Return
End
Get Create (RefClass(cComChilkatStringBuilder)) To hoScope
If (Not(IsComObjectCreated(hoScope))) Begin
Send CreateComObject of hoScope
End
Get ComAppend Of hoScope sDateStamp To iSuccess
Get ComAppend Of hoScope "/" To iSuccess
Get ComAppend Of hoScope sRegion To iSuccess
Get ComAppend Of hoScope "/s3/aws4_request" To iSuccess
Get Create (RefClass(cComChilkatStringBuilder)) To hoStringToSign
If (Not(IsComObjectCreated(hoStringToSign))) Begin
Send CreateComObject of hoStringToSign
End
Get ComAppend Of hoStringToSign "AWS4-HMAC-SHA256" + (character(10)) To iSuccess
Get ComAppend Of hoStringToSign sAmzDate To iSuccess
Get ComAppend Of hoStringToSign (character(10)) To iSuccess
Get ComGetAsString Of hoScope To sTemp1
Get ComAppend Of hoStringToSign sTemp1 To iSuccess
Get ComAppend Of hoStringToSign (character(10)) To iSuccess
Get ComAppend Of hoStringToSign sCanonicalHash To iSuccess
Get Create (RefClass(cComChilkatStringBuilder)) To hoInitialKey
If (Not(IsComObjectCreated(hoInitialKey))) Begin
Send CreateComObject of hoInitialKey
End
Get ComAppend Of hoInitialKey "AWS4" To iSuccess
Get ComStringOf Of hoCredentials "secret_key" To sTemp1
Get ComAppend Of hoInitialKey sTemp1 To iSuccess
Set ComMacAlgorithm Of hoCrypt To "hmac"
Get ComGetAsString Of hoInitialKey To sTemp1
Get ComSetMacKeyString Of hoCrypt sTemp1 To iSuccess
If (iSuccess <> True) Begin
Showln "Could not set signing key."
Procedure_Return
End
Get ComMacStringENC Of hoCrypt sDateStamp To sDerivedKey
Get ComLastMethodSuccess Of hoCrypt To bTemp1
If (bTemp1 <> True) Begin
Showln "Could not derive date signing key."
Procedure_Return
End
Get ComSetMacKeyEncoded Of hoCrypt sDerivedKey "hex" To iSuccess
If (iSuccess <> True) Begin
Showln "Could not set derived signing key."
Procedure_Return
End
Get ComMacStringENC Of hoCrypt sRegion To sDerivedKey
Get ComLastMethodSuccess Of hoCrypt To bTemp1
If (bTemp1 <> True) Begin
Showln "Could not derive signing key."
Procedure_Return
End
Get ComSetMacKeyEncoded Of hoCrypt sDerivedKey "hex" To iSuccess
If (iSuccess <> True) Begin
Showln "Could not set derived signing key."
Procedure_Return
End
Get ComMacStringENC Of hoCrypt "s3" To sDerivedKey
Get ComLastMethodSuccess Of hoCrypt To bTemp1
If (bTemp1 <> True) Begin
Showln "Could not derive signing key."
Procedure_Return
End
Get ComSetMacKeyEncoded Of hoCrypt sDerivedKey "hex" To iSuccess
If (iSuccess <> True) Begin
Showln "Could not set derived signing key."
Procedure_Return
End
Get ComMacStringENC Of hoCrypt "aws4_request" To sDerivedKey
Get ComLastMethodSuccess Of hoCrypt To bTemp1
If (bTemp1 <> True) Begin
Showln "Could not derive signing key."
Procedure_Return
End
Get ComSetMacKeyEncoded Of hoCrypt sDerivedKey "hex" To iSuccess
If (iSuccess <> True) Begin
Showln "Could not set final signing key."
Procedure_Return
End
Get ComGetAsString Of hoStringToSign To sTemp1
Get ComMacStringENC Of hoCrypt sTemp1 To sSignature
Get ComLastMethodSuccess Of hoCrypt To bTemp1
If (bTemp1 <> True) Begin
Showln "Could not sign delete request."
Procedure_Return
End
Get Create (RefClass(cComChilkatStringBuilder)) To hoAuthorization
If (Not(IsComObjectCreated(hoAuthorization))) Begin
Send CreateComObject of hoAuthorization
End
Get ComAppend Of hoAuthorization "AWS4-HMAC-SHA256 Credential=" To iSuccess
Get ComStringOf Of hoCredentials "access_key" To sTemp1
Get ComAppend Of hoAuthorization sTemp1 To iSuccess
Get ComAppend Of hoAuthorization "/" To iSuccess
Get ComGetAsString Of hoScope To sTemp1
Get ComAppend Of hoAuthorization sTemp1 To iSuccess
Get ComAppend Of hoAuthorization ", SignedHeaders=" To iSuccess
Get ComGetAsString Of hoSignedHeaders To sTemp1
Get ComAppend Of hoAuthorization sTemp1 To iSuccess
Get ComAppend Of hoAuthorization ", Signature=" To iSuccess
Get ComAppend Of hoAuthorization sSignature To iSuccess
Get ComGetAsString Of hoAuthorization To sTemp1
Send ComSetRequestHeader To hoHttp "Authorization" sTemp1
Send ComSetRequestHeader To hoHttp "Content-MD5" sContentMd5
Send ComSetRequestHeader To hoHttp "x-amz-content-sha256" sPayloadHash
Send ComSetRequestHeader To hoHttp "x-amz-date" sAmzDate
Get Create (RefClass(cComChilkatStringBuilder)) To hoRequestUrl
If (Not(IsComObjectCreated(hoRequestUrl))) Begin
Send CreateComObject of hoRequestUrl
End
Get ComAppend Of hoRequestUrl "https://" To iSuccess
Get ComGetAsString Of hoHost To sTemp1
Get ComAppend Of hoRequestUrl sTemp1 To iSuccess
Get ComAppend Of hoRequestUrl "/?delete=" To iSuccess
Get Create (RefClass(cComChilkatHttpResponse)) To hoResponse
If (Not(IsComObjectCreated(hoResponse))) Begin
Send CreateComObject of hoResponse
End
Get ComGetAsString Of hoRequestUrl To sTemp1
Get pvComObject of hoResponse to vResponse
Get ComHttpStr Of hoHttp "POST" sTemp1 sRequestBody "utf-8" "application/xml" vResponse To iSuccess
If (iSuccess <> True) Begin
Get ComLastStatus Of hoHttp To iTemp1
Showln "Request failed (transport or S3 error). HTTP status: " iTemp1
// Inspect LastErrorText privately. Do not log credentials or signed URLs.
Procedure_Return
End
Get ComStatusCode Of hoResponse To iTemp1
If (iTemp1 <> 200) Begin
Get ComStatusCode Of hoResponse To iTemp1
Showln "Unexpected HTTP status: " iTemp1
Procedure_Return
End
// HTTP 200 can contain per-object errors. Inspect the actual XML body.
Get Create (RefClass(cComChilkatXml)) To hoXml
If (Not(IsComObjectCreated(hoXml))) Begin
Send CreateComObject of hoXml
End
Get ComBodyStr Of hoResponse To sTemp1
Get ComLoadXml Of hoXml sTemp1 To iSuccess
If (iSuccess <> True) Begin
Showln "Invalid XML response."
Procedure_Return
End
Get ComTag Of hoXml To sTemp1
Get ComSetString Of hoStringCompare sTemp1 To iSuccess
Get ComContentsEqual Of hoStringCompare "DeleteResult" True To iStringMatch4
If (iStringMatch4 <> True) Begin
Showln "Unexpected XML response root."
Procedure_Return
End
Get ComNumChildrenHavingTag Of hoXml "Error" To iFailures
Move 0 To i
While (i < iFailures)
Set ComI Of hoXml To i
Get ComGetChildContent Of hoXml "Error[i]|Key" To sTemp1
Showln "Failed key: " sTemp1
Get ComGetChildContent Of hoXml "Error[i]|Code" To sTemp1
Showln "Error code: " sTemp1
Move (i + 1) To i
Loop
If (iFailures <> 0) Begin
Showln "One or more objects were not deleted. Retry only appropriate failed identifiers."
Procedure_Return
End
// Quiet=true means success entries need not be returned.
Showln "Delete batch succeeded with no reported object errors."
End_Procedure