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 AutoIt Downloads
Local $bSuccess = False
$oStringCompare = ObjCreate("Chilkat.StringBuilder")
; 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.
$oCredentials = ObjCreate("Chilkat.JsonObject")
$oCredentials.Strict = True
$bSuccess = $oCredentials.LoadFile("qa_data/aws/s3.json")
If ($bSuccess <> True) Then
ConsoleWrite("Could not load private S3 credentials." & @CRLF)
Exit
EndIf
$oStringCompare.SetString($oCredentials.StringOf("access_key"))
Local $bStringMatch1 = $oStringCompare.ContentsEqual("",True)
If (($oCredentials.JsonTypeOf("access_key") <> 1) Or ($bStringMatch1 = True)) Then
ConsoleWrite("Missing or invalid access_key." & @CRLF)
Exit
EndIf
$oStringCompare.SetString($oCredentials.StringOf("secret_key"))
Local $bStringMatch2 = $oStringCompare.ContentsEqual("",True)
If (($oCredentials.JsonTypeOf("secret_key") <> 1) Or ($bStringMatch2 = True)) Then
ConsoleWrite("Missing or invalid secret_key." & @CRLF)
Exit
EndIf
$oHttp = ObjCreate("Chilkat.Http")
$oHttp.AwsAccessKey = $oCredentials.StringOf("access_key")
$oHttp.AwsSecretKey = $oCredentials.StringOf("secret_key")
If ($oCredentials.HasMember("session_token") = True) Then
$oStringCompare.SetString($oCredentials.StringOf("session_token"))
Local $bStringMatch3 = $oStringCompare.ContentsEqual("",True)
If (($oCredentials.JsonTypeOf("session_token") <> 1) Or ($bStringMatch3 = True)) Then
ConsoleWrite("session_token must be a nonempty string when supplied." & @CRLF)
Exit
EndIf
$oHttp.AwsSessionToken = $oCredentials.StringOf("session_token")
EndIf
; Current Chilkat uses SigV4 by default. Do not select deprecated SigV2.
; Set the actual bucket region, not the caller's location.
Local $sRegion = "us-west-2"
$oHttp.AwsRegion = $sRegion
$oEndpoint = ObjCreate("Chilkat.StringBuilder")
$oEndpoint.Append("s3.")
$oEndpoint.Append($sRegion)
$oEndpoint.Append(".amazonaws.com")
$oHttp.AwsEndpoint = $oEndpoint.GetAsString()
$oHttp.RequireSslCertVerify = True
$oHttp.FollowRedirects = False
; A region redirect is not success. Correct the endpoint and signing region.
Local $sBucketName = "your-bucket-name"
$oDeleteRequest = ObjCreate("Chilkat.Xml")
$oDeleteRequest.Tag = "Delete"
$oDeleteRequest.AddAttribute("xmlns","http://s3.amazonaws.com/doc/2006-03-01/")
$oDeleteRequest.UpdateChildContent "Quiet","true"
; XML APIs escape key text. Do NOT URL-encode keys in this XML body.
$oDeleteRequest.UpdateChildContent "Object[0]|Key","images/seahorse.jpg"
$oDeleteRequest.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 (($oDeleteRequest.NumChildrenHavingTag("Object") < 1) Or ($oDeleteRequest.NumChildrenHavingTag("Object") > 1000)) Then
ConsoleWrite("Use a batch of 1 to 1,000 object identifiers." & @CRLF)
Exit
EndIf
Local $sRequestBody = $oDeleteRequest.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.
$oHost = ObjCreate("Chilkat.StringBuilder")
$oHost.Append($sBucketName)
$oHost.Append(".")
$oHost.Append($oEndpoint.GetAsString())
$oNow = ObjCreate("Chilkat.CkDateTime")
$bSuccess = $oNow.SetFromCurrentSystemTime()
If ($bSuccess <> True) Then
ConsoleWrite("Could not read system time." & @CRLF)
Exit
EndIf
$oTimestamp = ObjCreate("Chilkat.StringBuilder")
$oTimestamp.Append($oNow.GetAsTimestamp(False))
Local $iRemovedDashes = $oTimestamp.Replace("-","")
Local $iRemovedColons = $oTimestamp.Replace(":","")
Local $sAmzDate = $oTimestamp.GetAsString()
Local $sDateStamp = $oTimestamp.GetRange(0,8,False)
$oCrypt = ObjCreate("Chilkat.Crypt2")
$oCrypt.Charset = "utf-8"
$oCrypt.HashAlgorithm = "md5"
$oCrypt.EncodingMode = "base64"
Local $sContentMd5 = $oCrypt.HashStringENC($sRequestBody)
If ($oCrypt.LastMethodSuccess <> True) Then
ConsoleWrite("Could not hash delete body." & @CRLF)
Exit
EndIf
$oCrypt.HashAlgorithm = "sha256"
$oCrypt.EncodingMode = "hexlower"
Local $sPayloadHash = $oCrypt.HashStringENC($sRequestBody)
If ($oCrypt.LastMethodSuccess <> True) Then
ConsoleWrite("Could not hash delete body." & @CRLF)
Exit
EndIf
$oSignedHeaders = ObjCreate("Chilkat.StringBuilder")
$oSignedHeaders.Append("content-md5;host;x-amz-content-sha256;x-amz-date")
$oCanonical = ObjCreate("Chilkat.StringBuilder")
$oCanonical.Append("POST" & @LF & "/" & @LF & "delete=" & @LF & "content-md5:")
$oCanonical.Append($sContentMd5)
$oCanonical.Append(@LF & "host:")
$oCanonical.Append($oHost.GetAsString())
$oCanonical.Append(@LF & "x-amz-content-sha256:")
$oCanonical.Append($sPayloadHash)
$oCanonical.Append(@LF & "x-amz-date:")
$oCanonical.Append($sAmzDate)
$oCanonical.Append(@LF)
If ($oCredentials.HasMember("session_token") = True) Then
$oSignedHeaders.Append(";x-amz-security-token")
$oCanonical.Append("x-amz-security-token:")
$oCanonical.Append($oCredentials.StringOf("session_token"))
$oCanonical.Append(@LF)
$oHttp.SetRequestHeader "x-amz-security-token",$oCredentials.StringOf("session_token")
EndIf
$oCanonical.Append(@LF)
$oCanonical.Append($oSignedHeaders.GetAsString())
$oCanonical.Append(@LF)
$oCanonical.Append($sPayloadHash)
Local $sCanonicalHash = $oCrypt.HashStringENC($oCanonical.GetAsString())
If ($oCrypt.LastMethodSuccess <> True) Then
ConsoleWrite("Could not hash canonical request." & @CRLF)
Exit
EndIf
$oScope = ObjCreate("Chilkat.StringBuilder")
$oScope.Append($sDateStamp)
$oScope.Append("/")
$oScope.Append($sRegion)
$oScope.Append("/s3/aws4_request")
$oStringToSign = ObjCreate("Chilkat.StringBuilder")
$oStringToSign.Append("AWS4-HMAC-SHA256" & @LF)
$oStringToSign.Append($sAmzDate)
$oStringToSign.Append(@LF)
$oStringToSign.Append($oScope.GetAsString())
$oStringToSign.Append(@LF)
$oStringToSign.Append($sCanonicalHash)
$oInitialKey = ObjCreate("Chilkat.StringBuilder")
$oInitialKey.Append("AWS4")
$oInitialKey.Append($oCredentials.StringOf("secret_key"))
$oCrypt.MacAlgorithm = "hmac"
$bSuccess = $oCrypt.SetMacKeyString($oInitialKey.GetAsString())
If ($bSuccess <> True) Then
ConsoleWrite("Could not set signing key." & @CRLF)
Exit
EndIf
Local $sDerivedKey = $oCrypt.MacStringENC($sDateStamp)
If ($oCrypt.LastMethodSuccess <> True) Then
ConsoleWrite("Could not derive date signing key." & @CRLF)
Exit
EndIf
$bSuccess = $oCrypt.SetMacKeyEncoded($sDerivedKey,"hex")
If ($bSuccess <> True) Then
ConsoleWrite("Could not set derived signing key." & @CRLF)
Exit
EndIf
$sDerivedKey = $oCrypt.MacStringENC($sRegion)
If ($oCrypt.LastMethodSuccess <> True) Then
ConsoleWrite("Could not derive signing key." & @CRLF)
Exit
EndIf
$bSuccess = $oCrypt.SetMacKeyEncoded($sDerivedKey,"hex")
If ($bSuccess <> True) Then
ConsoleWrite("Could not set derived signing key." & @CRLF)
Exit
EndIf
$sDerivedKey = $oCrypt.MacStringENC("s3")
If ($oCrypt.LastMethodSuccess <> True) Then
ConsoleWrite("Could not derive signing key." & @CRLF)
Exit
EndIf
$bSuccess = $oCrypt.SetMacKeyEncoded($sDerivedKey,"hex")
If ($bSuccess <> True) Then
ConsoleWrite("Could not set derived signing key." & @CRLF)
Exit
EndIf
$sDerivedKey = $oCrypt.MacStringENC("aws4_request")
If ($oCrypt.LastMethodSuccess <> True) Then
ConsoleWrite("Could not derive signing key." & @CRLF)
Exit
EndIf
$bSuccess = $oCrypt.SetMacKeyEncoded($sDerivedKey,"hex")
If ($bSuccess <> True) Then
ConsoleWrite("Could not set final signing key." & @CRLF)
Exit
EndIf
Local $signature = $oCrypt.MacStringENC($oStringToSign.GetAsString())
If ($oCrypt.LastMethodSuccess <> True) Then
ConsoleWrite("Could not sign delete request." & @CRLF)
Exit
EndIf
$oAuthorization = ObjCreate("Chilkat.StringBuilder")
$oAuthorization.Append("AWS4-HMAC-SHA256 Credential=")
$oAuthorization.Append($oCredentials.StringOf("access_key"))
$oAuthorization.Append("/")
$oAuthorization.Append($oScope.GetAsString())
$oAuthorization.Append(", SignedHeaders=")
$oAuthorization.Append($oSignedHeaders.GetAsString())
$oAuthorization.Append(", Signature=")
$oAuthorization.Append($signature)
$oHttp.SetRequestHeader "Authorization",$oAuthorization.GetAsString()
$oHttp.SetRequestHeader "Content-MD5",$sContentMd5
$oHttp.SetRequestHeader "x-amz-content-sha256",$sPayloadHash
$oHttp.SetRequestHeader "x-amz-date",$sAmzDate
$oRequestUrl = ObjCreate("Chilkat.StringBuilder")
$oRequestUrl.Append("https://")
$oRequestUrl.Append($oHost.GetAsString())
$oRequestUrl.Append("/?delete=")
$oResponse = ObjCreate("Chilkat.HttpResponse")
$bSuccess = $oHttp.HttpStr("POST",$oRequestUrl.GetAsString(),$sRequestBody,"utf-8","application/xml",$oResponse)
If ($bSuccess <> True) Then
ConsoleWrite("Request failed (transport or S3 error). HTTP status: " & $oHttp.LastStatus & @CRLF)
; Inspect LastErrorText privately. Do not log credentials or signed URLs.
Exit
EndIf
If ($oResponse.StatusCode <> 200) Then
ConsoleWrite("Unexpected HTTP status: " & $oResponse.StatusCode & @CRLF)
Exit
EndIf
; HTTP 200 can contain per-object errors. Inspect the actual XML body.
$oXml = ObjCreate("Chilkat.Xml")
$bSuccess = $oXml.LoadXml($oResponse.BodyStr)
If ($bSuccess <> True) Then
ConsoleWrite("Invalid XML response." & @CRLF)
Exit
EndIf
$oStringCompare.SetString($oXml.Tag)
Local $bStringMatch4 = $oStringCompare.ContentsEqual("DeleteResult",True)
If ($bStringMatch4 <> True) Then
ConsoleWrite("Unexpected XML response root." & @CRLF)
Exit
EndIf
Local $iFailures = $oXml.NumChildrenHavingTag("Error")
Local $i = 0
While ($i < $iFailures)
$oXml.I = $i
ConsoleWrite("Failed key: " & $oXml.GetChildContent("Error[i]|Key") & @CRLF)
ConsoleWrite("Error code: " & $oXml.GetChildContent("Error[i]|Code") & @CRLF)
$i = $i + 1
Wend
If ($iFailures <> 0) Then
ConsoleWrite("One or more objects were not deleted. Retry only appropriate failed identifiers." & @CRLF)
Exit
EndIf
; Quiet=true means success entries need not be returned.
ConsoleWrite("Delete batch succeeded with no reported object errors." & @CRLF)