Sample code for 30+ languages & platforms
PowerBuilder Requires Chilkat v11.6.0+

Amazon S3: Delete Multiple Objects and Check Each Result

See more S3 - REST Examples

Deletes the explicitly supplied objects. Load qa_data/aws/delete-objects.json with {"objects":[{"key":"documents/old.txt"},{"key":"documents/older.txt","version_id":"VERSION_ID"}]}. Supply 1–1000 unique entries per request. The example does not enumerate a prefix or automatically retry. Requires s3:DeleteObject and, for explicit versions, s3:DeleteObjectVersion.

Without versionId, an unversioned object is permanently removed; a versioned bucket normally creates a delete marker. Explicit data-version deletion is permanent. Retention, legal holds and MFA Delete can restrict deletion. Absent keys can still be acknowledged as deleted. HTTP 200 may contain per-object errors; each returned key/version is matched against the intended batch. Reconcile uncertain or missing results before any retry.

General purpose DeleteObjects uses Content-MD5. The tested AuthAws path omits that header, so this example explicitly constructs Signature Version 4 for the fixed POST /?delete= request and sends it through Rest. This is deliberately not a general URL signer. Do not change the body, hostname, query, or signed headers after signing, or attach AuthAws as a second signer.

For general purpose S3 buckets, using regional HTTPS endpoints and AWS Signature Version 4. Directory buckets (S3 Express), access points, Outposts, and other AWS partitions need their own endpoint/authentication rules. Use a bucket name without dots for these virtual-hosted TLS examples; see the path-style example for dotted names.

Provide current IAM credentials in a private qa_data/aws/s3.json file with string fields access_key, secret_key, and session_token for temporary credentials. Prefer short-lived role credentials. This file is explicitly loaded; these examples do not discover profiles or refresh credentials. Keep it outside public content and source control. The session token is included in the signed request. TLS certificate verification is enabled on the Socket used by Rest.

Transport success and HTTP success are different. Keep full diagnostics private; signed requests and presigned URLs can contain credentials or grant access. For retryable reads use bounded backoff with jitter. Before retrying a write with an uncertain outcome, check the resource state. Keep keys, upload IDs, version IDs, ETags, and large sizes as strings.

AWS DeleteObjects reference

Chilkat PowerBuilder Downloads

PowerBuilder
integer li_rc
long li_Success
oleobject loo_TextCompare
oleobject loo_Credentials
long li_SameText1
long li_SameText2
string ls_Region
string ls_BucketName
oleobject loo_Endpoint
oleobject loo_Rest
oleobject loo_Connection
oleobject loo_Xml
oleobject loo_Batch
long li_Count
oleobject loo_Requested
oleobject loo_Identity
long i
long li_SameText3
string ls_Key
string ls_Version
long li_SameText4
string ls_RequestIdentity
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
long li_SameText5
string ls_CanonicalHash
oleobject loo_Scope
oleobject loo_StringToSign
oleobject loo_InitialKey
string ls_DerivedKey
string ls_Signature
oleobject loo_Authorization
string ls_ResponseBody
long li_SameText6
long li_SameText7
long li_Deleted
long li_Failed
oleobject loo_Received
long li_ResultType
string ls_ItemTag
long li_ResultCount
long j
oleobject loo_ItemPath
oleobject loo_Item
string ls_ResultKey
string ls_ResultVersion
string ls_ResponseIdentity

li_Success = 0

//  StringBuilder compares text by value in every generated language.
loo_TextCompare = create oleobject
li_rc = loo_TextCompare.ConnectToNewObject("Chilkat.StringBuilder")
if li_rc < 0 then
    destroy loo_TextCompare
    MessageBox("Error","Connecting to COM object failed")
    return
end if
//  Assumes Chilkat is unlocked. See Global Unlock Sample.
li_Success = 0
//  Private configuration, not a file in public web content or source control.
//  Load current IAM credentials authorized for the S3 operation.
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 AWS credentials."
    destroy loo_TextCompare
    destroy loo_Credentials
    return
end if

loo_TextCompare.Clear()
loo_TextCompare.SetString(loo_Credentials.StringOf("access_key"))
li_SameText1 = loo_TextCompare.ContentsEqual("",1)
if (loo_Credentials.JsonTypeOf("access_key") <> 1) OR (li_SameText1 = 1) then
    Write-Debug "Missing or invalid access_key."
    destroy loo_TextCompare
    destroy loo_Credentials
    return
end if

loo_TextCompare.Clear()
loo_TextCompare.SetString(loo_Credentials.StringOf("secret_key"))
li_SameText2 = loo_TextCompare.ContentsEqual("",1)
if (loo_Credentials.JsonTypeOf("secret_key") <> 1) OR (li_SameText2 = 1) then
    Write-Debug "Missing or invalid secret_key."
    destroy loo_TextCompare
    destroy loo_Credentials
    return
end if

//  General purpose bucket in the commercial AWS partition.
ls_Region = "us-west-2"
ls_BucketName = "amzn-s3-demo-bucket"
loo_Endpoint = create oleobject
li_rc = loo_Endpoint.ConnectToNewObject("Chilkat.StringBuilder")

li_Success = loo_Endpoint.Append(ls_BucketName)
if li_Success <> 1 then
    Write-Debug "Could not build text."
    destroy loo_TextCompare
    destroy loo_Credentials
    destroy loo_Endpoint
    return
end if

li_Success = loo_Endpoint.Append(".")
if li_Success <> 1 then
    Write-Debug "Could not build text."
    destroy loo_TextCompare
    destroy loo_Credentials
    destroy loo_Endpoint
    return
end if

li_Success = loo_Endpoint.Append("s3.")
if li_Success <> 1 then
    Write-Debug "Could not build text."
    destroy loo_TextCompare
    destroy loo_Credentials
    destroy loo_Endpoint
    return
end if

li_Success = loo_Endpoint.Append(ls_Region)
if li_Success <> 1 then
    Write-Debug "Could not build text."
    destroy loo_TextCompare
    destroy loo_Credentials
    destroy loo_Endpoint
    return
end if

li_Success = loo_Endpoint.Append(".amazonaws.com")
if li_Success <> 1 then
    Write-Debug "Could not build text."
    destroy loo_TextCompare
    destroy loo_Credentials
    destroy loo_Endpoint
    return
end if

loo_Rest = create oleobject
li_rc = loo_Rest.ConnectToNewObject("Chilkat.Rest")

loo_Rest.ConnectTimeoutMs = 20000
loo_Rest.IdleTimeoutMs = 60000
//  Connect to the SAME hostname used for TLS verification and request signing.
loo_Connection = create oleobject
li_rc = loo_Connection.ConnectToNewObject("Chilkat.Socket")

loo_Connection.RequireSslCertVerify = 1
loo_Connection.MaxReadIdleMs = 60000
loo_Connection.MaxSendIdleMs = 60000
li_Success = loo_Connection.Connect(loo_Endpoint.GetAsString(),443,1,20000)
if li_Success <> 1 then
    Write-Debug "TLS connection failed; inspect diagnostics privately."
    destroy loo_TextCompare
    destroy loo_Credentials
    destroy loo_Endpoint
    destroy loo_Rest
    destroy loo_Connection
    return
end if

li_Success = loo_Rest.UseConnection(loo_Connection,1)
if li_Success <> 1 then
    Write-Debug "Could not attach the TLS connection."
    destroy loo_TextCompare
    destroy loo_Credentials
    destroy loo_Endpoint
    destroy loo_Rest
    destroy loo_Connection
    return
end if

loo_Xml = create oleobject
li_rc = loo_Xml.ConnectToNewObject("Chilkat.Xml")

loo_Xml.Strict = 1
loo_Rest.Host = loo_Endpoint.GetAsString()
if loo_Credentials.HasMember("session_token") = 1 then
    if loo_Credentials.JsonTypeOf("session_token") <> 1 then
        Write-Debug "session_token must be a string."
        destroy loo_TextCompare
        destroy loo_Credentials
        destroy loo_Endpoint
        destroy loo_Rest
        destroy loo_Connection
        destroy loo_Xml
        return
    end if

end if

//  One explicit batch, at most 1000 objects. No prefix expansion or auto-retry.
loo_Batch = create oleobject
li_rc = loo_Batch.ConnectToNewObject("Chilkat.JsonObject")

loo_Batch.Strict = 1
li_Success = loo_Batch.LoadFile("qa_data/aws/delete-objects.json")
if li_Success <> 1 then
    Write-Debug "Could not load the intended deletion batch."
    destroy loo_TextCompare
    destroy loo_Credentials
    destroy loo_Endpoint
    destroy loo_Rest
    destroy loo_Connection
    destroy loo_Xml
    destroy loo_Batch
    return
end if

li_Count = loo_Batch.SizeOfArray("objects")
if (li_Count < 1) OR (li_Count > 1000) then
    Write-Debug "Supply 1 to 1000 objects in the batch."
    destroy loo_TextCompare
    destroy loo_Credentials
    destroy loo_Endpoint
    destroy loo_Rest
    destroy loo_Connection
    destroy loo_Xml
    destroy loo_Batch
    return
end if

loo_Xml.Tag = "Delete"
li_Success = loo_Xml.AddAttribute("xmlns","http://s3.amazonaws.com/doc/2006-03-01/")
if li_Success <> 1 then
    Write-Debug "Could not prepare the request."
    destroy loo_TextCompare
    destroy loo_Credentials
    destroy loo_Endpoint
    destroy loo_Rest
    destroy loo_Connection
    destroy loo_Xml
    destroy loo_Batch
    return
end if

loo_Xml.UpdateChildContent("Quiet","false")
loo_Requested = create oleobject
li_rc = loo_Requested.ConnectToNewObject("Chilkat.Hashtable")

loo_Identity = create oleobject
li_rc = loo_Identity.ConnectToNewObject("Chilkat.JsonObject")

i = 0
do while (i < li_Count)
    loo_Batch.I = i
    loo_Xml.I = i
    loo_TextCompare.Clear()
    loo_TextCompare.SetString(loo_Batch.StringOf("objects[i].key"))
    li_SameText3 = loo_TextCompare.ContentsEqual("",1)
    if (loo_Batch.JsonTypeOf("objects[i].key") <> 1) OR (li_SameText3 = 1) then
        Write-Debug "Each object needs a nonempty key."
        destroy loo_TextCompare
        destroy loo_Credentials
        destroy loo_Endpoint
        destroy loo_Rest
        destroy loo_Connection
        destroy loo_Xml
        destroy loo_Batch
        destroy loo_Requested
        destroy loo_Identity
        return
    end if

    ls_Key = loo_Batch.StringOf("objects[i].key")
    ls_Version = ""
    if loo_Batch.HasMember("objects[i].version_id") = 1 then
        loo_TextCompare.Clear()
        loo_TextCompare.SetString(loo_Batch.StringOf("objects[i].version_id"))
        li_SameText4 = loo_TextCompare.ContentsEqual("",1)
        if (loo_Batch.JsonTypeOf("objects[i].version_id") <> 1) OR (li_SameText4 = 1) then
            Write-Debug "version_id must be a nonempty string when present."
            destroy loo_TextCompare
            destroy loo_Credentials
            destroy loo_Endpoint
            destroy loo_Rest
            destroy loo_Connection
            destroy loo_Xml
            destroy loo_Batch
            destroy loo_Requested
            destroy loo_Identity
            return
        end if

        ls_Version = loo_Batch.StringOf("objects[i].version_id")
        loo_Xml.UpdateChildContent("Object[i]|VersionId",ls_Version)
    end if

    loo_Xml.UpdateChildContent("Object[i]|Key",ls_Key)
    li_Success = loo_Identity.UpdateString("key",ls_Key)
    if li_Success <> 1 then
        Write-Debug "Could not build saved state."
        destroy loo_TextCompare
        destroy loo_Credentials
        destroy loo_Endpoint
        destroy loo_Rest
        destroy loo_Connection
        destroy loo_Xml
        destroy loo_Batch
        destroy loo_Requested
        destroy loo_Identity
        return
    end if

    li_Success = loo_Identity.UpdateString("version",ls_Version)
    if li_Success <> 1 then
        Write-Debug "Could not build saved state."
        destroy loo_TextCompare
        destroy loo_Credentials
        destroy loo_Endpoint
        destroy loo_Rest
        destroy loo_Connection
        destroy loo_Xml
        destroy loo_Batch
        destroy loo_Requested
        destroy loo_Identity
        return
    end if

    ls_RequestIdentity = loo_Identity.Emit()
    if loo_Requested.Contains(ls_RequestIdentity) = 1 then
        Write-Debug "Duplicate key/version in deletion batch."
        destroy loo_TextCompare
        destroy loo_Credentials
        destroy loo_Endpoint
        destroy loo_Rest
        destroy loo_Connection
        destroy loo_Xml
        destroy loo_Batch
        destroy loo_Requested
        destroy loo_Identity
        return
    end if

    li_Success = loo_Requested.AddInt(ls_RequestIdentity,1)
    if li_Success <> 1 then
        Write-Debug "Could not prepare the request."
        destroy loo_TextCompare
        destroy loo_Credentials
        destroy loo_Endpoint
        destroy loo_Rest
        destroy loo_Connection
        destroy loo_Xml
        destroy loo_Batch
        destroy loo_Requested
        destroy loo_Identity
        return
    end if

    i = i + 1
loop
ls_RequestBody = loo_Xml.GetXml()
//  Explicit signing is used only for this fixed request so Content-MD5 survives.
//  Rest sends it; AuthAws must NOT also be configured on this Rest instance.
//  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(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_TextCompare
    destroy loo_Credentials
    destroy loo_Endpoint
    destroy loo_Rest
    destroy loo_Connection
    destroy loo_Xml
    destroy loo_Batch
    destroy loo_Requested
    destroy loo_Identity
    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_TextCompare
    destroy loo_Credentials
    destroy loo_Endpoint
    destroy loo_Rest
    destroy loo_Connection
    destroy loo_Xml
    destroy loo_Batch
    destroy loo_Requested
    destroy loo_Identity
    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_TextCompare
    destroy loo_Credentials
    destroy loo_Endpoint
    destroy loo_Rest
    destroy loo_Connection
    destroy loo_Xml
    destroy loo_Batch
    destroy loo_Requested
    destroy loo_Identity
    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_TextCompare.Clear()
    loo_TextCompare.SetString(loo_Credentials.StringOf("session_token"))
    li_SameText5 = loo_TextCompare.ContentsEqual("",1)
    if li_SameText5 <> 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")
        li_Success = loo_Rest.AddHeader("x-amz-security-token",loo_Credentials.StringOf("session_token"))
        if li_Success <> 1 then
            Write-Debug "Could not prepare the request."
            destroy loo_TextCompare
            destroy loo_Credentials
            destroy loo_Endpoint
            destroy loo_Rest
            destroy loo_Connection
            destroy loo_Xml
            destroy loo_Batch
            destroy loo_Requested
            destroy loo_Identity
            destroy loo_Host
            destroy loo_Now
            destroy loo_Timestamp
            destroy loo_Crypt
            destroy loo_SignedHeaders
            destroy loo_Canonical
            return
        end if

    end if

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_TextCompare
    destroy loo_Credentials
    destroy loo_Endpoint
    destroy loo_Rest
    destroy loo_Connection
    destroy loo_Xml
    destroy loo_Batch
    destroy loo_Requested
    destroy loo_Identity
    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_TextCompare
    destroy loo_Credentials
    destroy loo_Endpoint
    destroy loo_Rest
    destroy loo_Connection
    destroy loo_Xml
    destroy loo_Batch
    destroy loo_Requested
    destroy loo_Identity
    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_TextCompare
    destroy loo_Credentials
    destroy loo_Endpoint
    destroy loo_Rest
    destroy loo_Connection
    destroy loo_Xml
    destroy loo_Batch
    destroy loo_Requested
    destroy loo_Identity
    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_TextCompare
    destroy loo_Credentials
    destroy loo_Endpoint
    destroy loo_Rest
    destroy loo_Connection
    destroy loo_Xml
    destroy loo_Batch
    destroy loo_Requested
    destroy loo_Identity
    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_TextCompare
    destroy loo_Credentials
    destroy loo_Endpoint
    destroy loo_Rest
    destroy loo_Connection
    destroy loo_Xml
    destroy loo_Batch
    destroy loo_Requested
    destroy loo_Identity
    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_TextCompare
    destroy loo_Credentials
    destroy loo_Endpoint
    destroy loo_Rest
    destroy loo_Connection
    destroy loo_Xml
    destroy loo_Batch
    destroy loo_Requested
    destroy loo_Identity
    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_TextCompare
    destroy loo_Credentials
    destroy loo_Endpoint
    destroy loo_Rest
    destroy loo_Connection
    destroy loo_Xml
    destroy loo_Batch
    destroy loo_Requested
    destroy loo_Identity
    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_TextCompare
    destroy loo_Credentials
    destroy loo_Endpoint
    destroy loo_Rest
    destroy loo_Connection
    destroy loo_Xml
    destroy loo_Batch
    destroy loo_Requested
    destroy loo_Identity
    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_TextCompare
    destroy loo_Credentials
    destroy loo_Endpoint
    destroy loo_Rest
    destroy loo_Connection
    destroy loo_Xml
    destroy loo_Batch
    destroy loo_Requested
    destroy loo_Identity
    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_TextCompare
    destroy loo_Credentials
    destroy loo_Endpoint
    destroy loo_Rest
    destroy loo_Connection
    destroy loo_Xml
    destroy loo_Batch
    destroy loo_Requested
    destroy loo_Identity
    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_TextCompare
    destroy loo_Credentials
    destroy loo_Endpoint
    destroy loo_Rest
    destroy loo_Connection
    destroy loo_Xml
    destroy loo_Batch
    destroy loo_Requested
    destroy loo_Identity
    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)
li_Success = loo_Rest.AddHeader("Authorization",loo_Authorization.GetAsString())
if li_Success <> 1 then
    Write-Debug "Could not prepare the request."
    destroy loo_TextCompare
    destroy loo_Credentials
    destroy loo_Endpoint
    destroy loo_Rest
    destroy loo_Connection
    destroy loo_Xml
    destroy loo_Batch
    destroy loo_Requested
    destroy loo_Identity
    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
    return
end if

li_Success = loo_Rest.AddHeader("Content-MD5",ls_ContentMd5)
if li_Success <> 1 then
    Write-Debug "Could not prepare the request."
    destroy loo_TextCompare
    destroy loo_Credentials
    destroy loo_Endpoint
    destroy loo_Rest
    destroy loo_Connection
    destroy loo_Xml
    destroy loo_Batch
    destroy loo_Requested
    destroy loo_Identity
    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
    return
end if

li_Success = loo_Rest.AddHeader("x-amz-content-sha256",ls_PayloadHash)
if li_Success <> 1 then
    Write-Debug "Could not prepare the request."
    destroy loo_TextCompare
    destroy loo_Credentials
    destroy loo_Endpoint
    destroy loo_Rest
    destroy loo_Connection
    destroy loo_Xml
    destroy loo_Batch
    destroy loo_Requested
    destroy loo_Identity
    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
    return
end if

li_Success = loo_Rest.AddHeader("x-amz-date",ls_AmzDate)
if li_Success <> 1 then
    Write-Debug "Could not prepare the request."
    destroy loo_TextCompare
    destroy loo_Credentials
    destroy loo_Endpoint
    destroy loo_Rest
    destroy loo_Connection
    destroy loo_Xml
    destroy loo_Batch
    destroy loo_Requested
    destroy loo_Identity
    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
    return
end if

li_Success = loo_Rest.AddQueryParam("delete","")
if li_Success <> 1 then
    Write-Debug "Could not prepare the request."
    destroy loo_TextCompare
    destroy loo_Credentials
    destroy loo_Endpoint
    destroy loo_Rest
    destroy loo_Connection
    destroy loo_Xml
    destroy loo_Batch
    destroy loo_Requested
    destroy loo_Identity
    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
    return
end if

ls_ResponseBody = loo_Rest.FullRequestString("POST","/",ls_RequestBody)
if loo_Rest.LastMethodSuccess <> 1 then
    Write-Debug "Request did not complete; inspect diagnostics privately."
    destroy loo_TextCompare
    destroy loo_Credentials
    destroy loo_Endpoint
    destroy loo_Rest
    destroy loo_Connection
    destroy loo_Xml
    destroy loo_Batch
    destroy loo_Requested
    destroy loo_Identity
    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
    return
end if

if loo_Rest.ResponseStatusCode <> 200 then
    Write-Debug "S3 HTTP status: " + string(loo_Rest.ResponseStatusCode)
    li_Success = loo_Xml.LoadXml(ls_ResponseBody)
    loo_TextCompare.Clear()
    loo_TextCompare.SetString(loo_Xml.Tag)
    li_SameText6 = loo_TextCompare.ContentsEqual("Error",1)
    if (li_Success = 1) AND (li_SameText6 = 1) then
        Write-Debug "S3 error code: " + loo_Xml.GetChildContent("Code")
    end if

    //  Keep signed headers, credentials, URLs, and full diagnostics private.
    //  A timeout may leave a write's outcome uncertain. Check before retrying.
    destroy loo_TextCompare
    destroy loo_Credentials
    destroy loo_Endpoint
    destroy loo_Rest
    destroy loo_Connection
    destroy loo_Xml
    destroy loo_Batch
    destroy loo_Requested
    destroy loo_Identity
    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
    return
end if

li_Success = loo_Xml.LoadXml(ls_ResponseBody)
if li_Success <> 1 then
    Write-Debug "Expected well-formed XML."
    destroy loo_TextCompare
    destroy loo_Credentials
    destroy loo_Endpoint
    destroy loo_Rest
    destroy loo_Connection
    destroy loo_Xml
    destroy loo_Batch
    destroy loo_Requested
    destroy loo_Identity
    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
    return
end if

loo_TextCompare.Clear()
loo_TextCompare.SetString(loo_Xml.Tag)
li_SameText7 = loo_TextCompare.ContentsEqual("DeleteResult",1)
if li_SameText7 <> 1 then
    Write-Debug "Unexpected S3 response element; this is not a successful result."
    destroy loo_TextCompare
    destroy loo_Credentials
    destroy loo_Endpoint
    destroy loo_Rest
    destroy loo_Connection
    destroy loo_Xml
    destroy loo_Batch
    destroy loo_Requested
    destroy loo_Identity
    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
    return
end if

li_Deleted = loo_Xml.NumChildrenHavingTag("Deleted")
li_Failed = loo_Xml.NumChildrenHavingTag("Error")
if li_Deleted + li_Failed <> li_Count then
    Write-Debug "Incomplete result: reconcile every requested object before retrying."
    destroy loo_TextCompare
    destroy loo_Credentials
    destroy loo_Endpoint
    destroy loo_Rest
    destroy loo_Connection
    destroy loo_Xml
    destroy loo_Batch
    destroy loo_Requested
    destroy loo_Identity
    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
    return
end if

loo_Received = create oleobject
li_rc = loo_Received.ConnectToNewObject("Chilkat.Hashtable")

li_ResultType = 0
do while (li_ResultType < 2)
    ls_ItemTag = "Deleted"
    li_ResultCount = li_Deleted
    if li_ResultType = 1 then
        ls_ItemTag = "Error"
        li_ResultCount = li_Failed
    end if

    j = 0
    do while (j < li_ResultCount)
        loo_Xml.I = j
        loo_ItemPath = create oleobject
        li_rc = loo_ItemPath.ConnectToNewObject("Chilkat.StringBuilder")

        li_Success = loo_ItemPath.Append(ls_ItemTag)
        if li_Success <> 1 then
            Write-Debug "Could not build text."
            destroy loo_TextCompare
            destroy loo_Credentials
            destroy loo_Endpoint
            destroy loo_Rest
            destroy loo_Connection
            destroy loo_Xml
            destroy loo_Batch
            destroy loo_Requested
            destroy loo_Identity
            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_Received
            destroy loo_ItemPath
            return
        end if

        li_Success = loo_ItemPath.Append("[i]")
        if li_Success <> 1 then
            Write-Debug "Could not build text."
            destroy loo_TextCompare
            destroy loo_Credentials
            destroy loo_Endpoint
            destroy loo_Rest
            destroy loo_Connection
            destroy loo_Xml
            destroy loo_Batch
            destroy loo_Requested
            destroy loo_Identity
            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_Received
            destroy loo_ItemPath
            return
        end if

        loo_Item = loo_Xml.FindChild(loo_ItemPath.GetAsString())
        if loo_Xml.LastMethodSuccess <> 1 then
            Write-Debug "Missing deletion result entry."
            destroy loo_TextCompare
            destroy loo_Credentials
            destroy loo_Endpoint
            destroy loo_Rest
            destroy loo_Connection
            destroy loo_Xml
            destroy loo_Batch
            destroy loo_Requested
            destroy loo_Identity
            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_Received
            destroy loo_ItemPath
            return
        end if

        ls_ResultKey = loo_Item.GetChildContent("Key")
        ls_ResultVersion = ""
        if loo_Item.HasChildWithTag("VersionId") = 1 then
            ls_ResultVersion = loo_Item.GetChildContent("VersionId")
        end if

        li_Success = loo_Identity.UpdateString("key",ls_ResultKey)
        if li_Success <> 1 then
            Write-Debug "Could not build saved state."
            destroy loo_TextCompare
            destroy loo_Credentials
            destroy loo_Endpoint
            destroy loo_Rest
            destroy loo_Connection
            destroy loo_Xml
            destroy loo_Batch
            destroy loo_Requested
            destroy loo_Identity
            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_Received
            destroy loo_ItemPath
            return
        end if

        li_Success = loo_Identity.UpdateString("version",ls_ResultVersion)
        if li_Success <> 1 then
            Write-Debug "Could not build saved state."
            destroy loo_TextCompare
            destroy loo_Credentials
            destroy loo_Endpoint
            destroy loo_Rest
            destroy loo_Connection
            destroy loo_Xml
            destroy loo_Batch
            destroy loo_Requested
            destroy loo_Identity
            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_Received
            destroy loo_ItemPath
            return
        end if

        ls_ResponseIdentity = loo_Identity.Emit()
        if (loo_Requested.Contains(ls_ResponseIdentity) <> 1) OR (loo_Received.Contains(ls_ResponseIdentity) = 1) then
            Write-Debug "Unexpected or duplicated deletion result; reconcile manually."
            destroy loo_TextCompare
            destroy loo_Credentials
            destroy loo_Endpoint
            destroy loo_Rest
            destroy loo_Connection
            destroy loo_Xml
            destroy loo_Batch
            destroy loo_Requested
            destroy loo_Identity
            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_Received
            destroy loo_ItemPath
            return
        end if

        li_Success = loo_Received.AddInt(ls_ResponseIdentity,1)
        if li_Success <> 1 then
            Write-Debug "Could not prepare the request."
            destroy loo_TextCompare
            destroy loo_Credentials
            destroy loo_Endpoint
            destroy loo_Rest
            destroy loo_Connection
            destroy loo_Xml
            destroy loo_Batch
            destroy loo_Requested
            destroy loo_Identity
            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_Received
            destroy loo_ItemPath
            return
        end if

        Write-Debug ls_ItemTag + ": " + ls_ResultKey
        if li_ResultType = 1 then
            Write-Debug "Error code: " + loo_Item.GetChildContent("Code")
        end if

        destroy loo_Item
        j = j + 1
    loop
    li_ResultType = li_ResultType + 1
loop
if li_Failed > 0 then
    Write-Debug "Batch has per-object failures. Do not retry successful entries."
    destroy loo_TextCompare
    destroy loo_Credentials
    destroy loo_Endpoint
    destroy loo_Rest
    destroy loo_Connection
    destroy loo_Xml
    destroy loo_Batch
    destroy loo_Requested
    destroy loo_Identity
    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_Received
    destroy loo_ItemPath
    return
end if

Write-Debug "All requested deletions acknowledged."


destroy loo_TextCompare
destroy loo_Credentials
destroy loo_Endpoint
destroy loo_Rest
destroy loo_Connection
destroy loo_Xml
destroy loo_Batch
destroy loo_Requested
destroy loo_Identity
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_Received
destroy loo_ItemPath