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 Visual FoxPro Downloads
LOCAL lnSuccess
LOCAL loStringCompare
LOCAL loCredentials
LOCAL lnStringMatch1
LOCAL lnStringMatch2
LOCAL loHttp
LOCAL lnStringMatch3
LOCAL lcRegion
LOCAL loEndpoint
LOCAL lcBucketName
LOCAL loDeleteRequest
LOCAL lcRequestBody
LOCAL loHost
LOCAL loNow
LOCAL loTimestamp
LOCAL lnRemovedDashes
LOCAL lnRemovedColons
LOCAL lcAmzDate
LOCAL lcDateStamp
LOCAL loCrypt
LOCAL lcContentMd5
LOCAL lcPayloadHash
LOCAL loSignedHeaders
LOCAL loCanonical
LOCAL lcCanonicalHash
LOCAL loScope
LOCAL loStringToSign
LOCAL loInitialKey
LOCAL lcDerivedKey
LOCAL lcSignature
LOCAL loAuthorization
LOCAL loRequestUrl
LOCAL loResponse
LOCAL loXml
LOCAL lnStringMatch4
LOCAL lnFailures
LOCAL i
lnSuccess = 0
loStringCompare = CreateObject('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.
loCredentials = CreateObject('Chilkat.JsonObject')
loCredentials.Strict = 1
lnSuccess = loCredentials.LoadFile("qa_data/aws/s3.json")
IF (lnSuccess <> 1) THEN
? "Could not load private S3 credentials."
RELEASE loStringCompare
RELEASE loCredentials
CANCEL
ENDIF
loStringCompare.SetString(loCredentials.StringOf("access_key"))
lnStringMatch1 = loStringCompare.ContentsEqual("",1)
IF ((loCredentials.JsonTypeOf("access_key") <> 1) OR (lnStringMatch1 = 1)) THEN
? "Missing or invalid access_key."
RELEASE loStringCompare
RELEASE loCredentials
CANCEL
ENDIF
loStringCompare.SetString(loCredentials.StringOf("secret_key"))
lnStringMatch2 = loStringCompare.ContentsEqual("",1)
IF ((loCredentials.JsonTypeOf("secret_key") <> 1) OR (lnStringMatch2 = 1)) THEN
? "Missing or invalid secret_key."
RELEASE loStringCompare
RELEASE loCredentials
CANCEL
ENDIF
loHttp = CreateObject('Chilkat.Http')
loHttp.AwsAccessKey = loCredentials.StringOf("access_key")
loHttp.AwsSecretKey = loCredentials.StringOf("secret_key")
IF (loCredentials.HasMember("session_token") = 1) THEN
loStringCompare.SetString(loCredentials.StringOf("session_token"))
lnStringMatch3 = loStringCompare.ContentsEqual("",1)
IF ((loCredentials.JsonTypeOf("session_token") <> 1) OR (lnStringMatch3 = 1)) THEN
? "session_token must be a nonempty string when supplied."
RELEASE loStringCompare
RELEASE loCredentials
RELEASE loHttp
CANCEL
ENDIF
loHttp.AwsSessionToken = loCredentials.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.
lcRegion = "us-west-2"
loHttp.AwsRegion = lcRegion
loEndpoint = CreateObject('Chilkat.StringBuilder')
loEndpoint.Append("s3.")
loEndpoint.Append(lcRegion)
loEndpoint.Append(".amazonaws.com")
loHttp.AwsEndpoint = loEndpoint.GetAsString()
loHttp.RequireSslCertVerify = 1
loHttp.FollowRedirects = 0
* A region redirect is not success. Correct the endpoint and signing region.
lcBucketName = "your-bucket-name"
loDeleteRequest = CreateObject('Chilkat.Xml')
loDeleteRequest.Tag = "Delete"
loDeleteRequest.AddAttribute("xmlns","http://s3.amazonaws.com/doc/2006-03-01/")
loDeleteRequest.UpdateChildContent("Quiet","true")
* XML APIs escape key text. Do NOT URL-encode keys in this XML body.
loDeleteRequest.UpdateChildContent("Object[0]|Key","images/seahorse.jpg")
loDeleteRequest.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 ((loDeleteRequest.NumChildrenHavingTag("Object") < 1) OR (loDeleteRequest.NumChildrenHavingTag("Object") > 1000)) THEN
? "Use a batch of 1 to 1,000 object identifiers."
RELEASE loStringCompare
RELEASE loCredentials
RELEASE loHttp
RELEASE loEndpoint
RELEASE loDeleteRequest
CANCEL
ENDIF
lcRequestBody = loDeleteRequest.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.
loHost = CreateObject('Chilkat.StringBuilder')
loHost.Append(lcBucketName)
loHost.Append(".")
loHost.Append(loEndpoint.GetAsString())
loNow = CreateObject('Chilkat.CkDateTime')
lnSuccess = loNow.SetFromCurrentSystemTime()
IF (lnSuccess <> 1) THEN
? "Could not read system time."
RELEASE loStringCompare
RELEASE loCredentials
RELEASE loHttp
RELEASE loEndpoint
RELEASE loDeleteRequest
RELEASE loHost
RELEASE loNow
CANCEL
ENDIF
loTimestamp = CreateObject('Chilkat.StringBuilder')
loTimestamp.Append(loNow.GetAsTimestamp(0))
lnRemovedDashes = loTimestamp.Replace("-","")
lnRemovedColons = loTimestamp.Replace(":","")
lcAmzDate = loTimestamp.GetAsString()
lcDateStamp = loTimestamp.GetRange(0,8,0)
loCrypt = CreateObject('Chilkat.Crypt2')
loCrypt.Charset = "utf-8"
loCrypt.HashAlgorithm = "md5"
loCrypt.EncodingMode = "base64"
lcContentMd5 = loCrypt.HashStringENC(lcRequestBody)
IF (loCrypt.LastMethodSuccess <> 1) THEN
? "Could not hash delete body."
RELEASE loStringCompare
RELEASE loCredentials
RELEASE loHttp
RELEASE loEndpoint
RELEASE loDeleteRequest
RELEASE loHost
RELEASE loNow
RELEASE loTimestamp
RELEASE loCrypt
CANCEL
ENDIF
loCrypt.HashAlgorithm = "sha256"
loCrypt.EncodingMode = "hexlower"
lcPayloadHash = loCrypt.HashStringENC(lcRequestBody)
IF (loCrypt.LastMethodSuccess <> 1) THEN
? "Could not hash delete body."
RELEASE loStringCompare
RELEASE loCredentials
RELEASE loHttp
RELEASE loEndpoint
RELEASE loDeleteRequest
RELEASE loHost
RELEASE loNow
RELEASE loTimestamp
RELEASE loCrypt
CANCEL
ENDIF
loSignedHeaders = CreateObject('Chilkat.StringBuilder')
loSignedHeaders.Append("content-md5;host;x-amz-content-sha256;x-amz-date")
loCanonical = CreateObject('Chilkat.StringBuilder')
loCanonical.Append("POST" + CHR(10) + "/" + CHR(10) + "delete=" + CHR(10) + "content-md5:")
loCanonical.Append(lcContentMd5)
loCanonical.Append(CHR(10) + "host:")
loCanonical.Append(loHost.GetAsString())
loCanonical.Append(CHR(10) + "x-amz-content-sha256:")
loCanonical.Append(lcPayloadHash)
loCanonical.Append(CHR(10) + "x-amz-date:")
loCanonical.Append(lcAmzDate)
loCanonical.Append(CHR(10))
IF (loCredentials.HasMember("session_token") = 1) THEN
loSignedHeaders.Append(";x-amz-security-token")
loCanonical.Append("x-amz-security-token:")
loCanonical.Append(loCredentials.StringOf("session_token"))
loCanonical.Append(CHR(10))
loHttp.SetRequestHeader("x-amz-security-token",loCredentials.StringOf("session_token"))
ENDIF
loCanonical.Append(CHR(10))
loCanonical.Append(loSignedHeaders.GetAsString())
loCanonical.Append(CHR(10))
loCanonical.Append(lcPayloadHash)
lcCanonicalHash = loCrypt.HashStringENC(loCanonical.GetAsString())
IF (loCrypt.LastMethodSuccess <> 1) THEN
? "Could not hash canonical request."
RELEASE loStringCompare
RELEASE loCredentials
RELEASE loHttp
RELEASE loEndpoint
RELEASE loDeleteRequest
RELEASE loHost
RELEASE loNow
RELEASE loTimestamp
RELEASE loCrypt
RELEASE loSignedHeaders
RELEASE loCanonical
CANCEL
ENDIF
loScope = CreateObject('Chilkat.StringBuilder')
loScope.Append(lcDateStamp)
loScope.Append("/")
loScope.Append(lcRegion)
loScope.Append("/s3/aws4_request")
loStringToSign = CreateObject('Chilkat.StringBuilder')
loStringToSign.Append("AWS4-HMAC-SHA256" + CHR(10))
loStringToSign.Append(lcAmzDate)
loStringToSign.Append(CHR(10))
loStringToSign.Append(loScope.GetAsString())
loStringToSign.Append(CHR(10))
loStringToSign.Append(lcCanonicalHash)
loInitialKey = CreateObject('Chilkat.StringBuilder')
loInitialKey.Append("AWS4")
loInitialKey.Append(loCredentials.StringOf("secret_key"))
loCrypt.MacAlgorithm = "hmac"
lnSuccess = loCrypt.SetMacKeyString(loInitialKey.GetAsString())
IF (lnSuccess <> 1) THEN
? "Could not set signing key."
RELEASE loStringCompare
RELEASE loCredentials
RELEASE loHttp
RELEASE loEndpoint
RELEASE loDeleteRequest
RELEASE loHost
RELEASE loNow
RELEASE loTimestamp
RELEASE loCrypt
RELEASE loSignedHeaders
RELEASE loCanonical
RELEASE loScope
RELEASE loStringToSign
RELEASE loInitialKey
CANCEL
ENDIF
lcDerivedKey = loCrypt.MacStringENC(lcDateStamp)
IF (loCrypt.LastMethodSuccess <> 1) THEN
? "Could not derive date signing key."
RELEASE loStringCompare
RELEASE loCredentials
RELEASE loHttp
RELEASE loEndpoint
RELEASE loDeleteRequest
RELEASE loHost
RELEASE loNow
RELEASE loTimestamp
RELEASE loCrypt
RELEASE loSignedHeaders
RELEASE loCanonical
RELEASE loScope
RELEASE loStringToSign
RELEASE loInitialKey
CANCEL
ENDIF
lnSuccess = loCrypt.SetMacKeyEncoded(lcDerivedKey,"hex")
IF (lnSuccess <> 1) THEN
? "Could not set derived signing key."
RELEASE loStringCompare
RELEASE loCredentials
RELEASE loHttp
RELEASE loEndpoint
RELEASE loDeleteRequest
RELEASE loHost
RELEASE loNow
RELEASE loTimestamp
RELEASE loCrypt
RELEASE loSignedHeaders
RELEASE loCanonical
RELEASE loScope
RELEASE loStringToSign
RELEASE loInitialKey
CANCEL
ENDIF
lcDerivedKey = loCrypt.MacStringENC(lcRegion)
IF (loCrypt.LastMethodSuccess <> 1) THEN
? "Could not derive signing key."
RELEASE loStringCompare
RELEASE loCredentials
RELEASE loHttp
RELEASE loEndpoint
RELEASE loDeleteRequest
RELEASE loHost
RELEASE loNow
RELEASE loTimestamp
RELEASE loCrypt
RELEASE loSignedHeaders
RELEASE loCanonical
RELEASE loScope
RELEASE loStringToSign
RELEASE loInitialKey
CANCEL
ENDIF
lnSuccess = loCrypt.SetMacKeyEncoded(lcDerivedKey,"hex")
IF (lnSuccess <> 1) THEN
? "Could not set derived signing key."
RELEASE loStringCompare
RELEASE loCredentials
RELEASE loHttp
RELEASE loEndpoint
RELEASE loDeleteRequest
RELEASE loHost
RELEASE loNow
RELEASE loTimestamp
RELEASE loCrypt
RELEASE loSignedHeaders
RELEASE loCanonical
RELEASE loScope
RELEASE loStringToSign
RELEASE loInitialKey
CANCEL
ENDIF
lcDerivedKey = loCrypt.MacStringENC("s3")
IF (loCrypt.LastMethodSuccess <> 1) THEN
? "Could not derive signing key."
RELEASE loStringCompare
RELEASE loCredentials
RELEASE loHttp
RELEASE loEndpoint
RELEASE loDeleteRequest
RELEASE loHost
RELEASE loNow
RELEASE loTimestamp
RELEASE loCrypt
RELEASE loSignedHeaders
RELEASE loCanonical
RELEASE loScope
RELEASE loStringToSign
RELEASE loInitialKey
CANCEL
ENDIF
lnSuccess = loCrypt.SetMacKeyEncoded(lcDerivedKey,"hex")
IF (lnSuccess <> 1) THEN
? "Could not set derived signing key."
RELEASE loStringCompare
RELEASE loCredentials
RELEASE loHttp
RELEASE loEndpoint
RELEASE loDeleteRequest
RELEASE loHost
RELEASE loNow
RELEASE loTimestamp
RELEASE loCrypt
RELEASE loSignedHeaders
RELEASE loCanonical
RELEASE loScope
RELEASE loStringToSign
RELEASE loInitialKey
CANCEL
ENDIF
lcDerivedKey = loCrypt.MacStringENC("aws4_request")
IF (loCrypt.LastMethodSuccess <> 1) THEN
? "Could not derive signing key."
RELEASE loStringCompare
RELEASE loCredentials
RELEASE loHttp
RELEASE loEndpoint
RELEASE loDeleteRequest
RELEASE loHost
RELEASE loNow
RELEASE loTimestamp
RELEASE loCrypt
RELEASE loSignedHeaders
RELEASE loCanonical
RELEASE loScope
RELEASE loStringToSign
RELEASE loInitialKey
CANCEL
ENDIF
lnSuccess = loCrypt.SetMacKeyEncoded(lcDerivedKey,"hex")
IF (lnSuccess <> 1) THEN
? "Could not set final signing key."
RELEASE loStringCompare
RELEASE loCredentials
RELEASE loHttp
RELEASE loEndpoint
RELEASE loDeleteRequest
RELEASE loHost
RELEASE loNow
RELEASE loTimestamp
RELEASE loCrypt
RELEASE loSignedHeaders
RELEASE loCanonical
RELEASE loScope
RELEASE loStringToSign
RELEASE loInitialKey
CANCEL
ENDIF
lcSignature = loCrypt.MacStringENC(loStringToSign.GetAsString())
IF (loCrypt.LastMethodSuccess <> 1) THEN
? "Could not sign delete request."
RELEASE loStringCompare
RELEASE loCredentials
RELEASE loHttp
RELEASE loEndpoint
RELEASE loDeleteRequest
RELEASE loHost
RELEASE loNow
RELEASE loTimestamp
RELEASE loCrypt
RELEASE loSignedHeaders
RELEASE loCanonical
RELEASE loScope
RELEASE loStringToSign
RELEASE loInitialKey
CANCEL
ENDIF
loAuthorization = CreateObject('Chilkat.StringBuilder')
loAuthorization.Append("AWS4-HMAC-SHA256 Credential=")
loAuthorization.Append(loCredentials.StringOf("access_key"))
loAuthorization.Append("/")
loAuthorization.Append(loScope.GetAsString())
loAuthorization.Append(", SignedHeaders=")
loAuthorization.Append(loSignedHeaders.GetAsString())
loAuthorization.Append(", Signature=")
loAuthorization.Append(lcSignature)
loHttp.SetRequestHeader("Authorization",loAuthorization.GetAsString())
loHttp.SetRequestHeader("Content-MD5",lcContentMd5)
loHttp.SetRequestHeader("x-amz-content-sha256",lcPayloadHash)
loHttp.SetRequestHeader("x-amz-date",lcAmzDate)
loRequestUrl = CreateObject('Chilkat.StringBuilder')
loRequestUrl.Append("https://")
loRequestUrl.Append(loHost.GetAsString())
loRequestUrl.Append("/?delete=")
loResponse = CreateObject('Chilkat.HttpResponse')
lnSuccess = loHttp.HttpStr("POST",loRequestUrl.GetAsString(),lcRequestBody,"utf-8","application/xml",loResponse)
IF (lnSuccess <> 1) THEN
? "Request failed (transport or S3 error). HTTP status: " + STR(loHttp.LastStatus)
* Inspect LastErrorText privately. Do not log credentials or signed URLs.
RELEASE loStringCompare
RELEASE loCredentials
RELEASE loHttp
RELEASE loEndpoint
RELEASE loDeleteRequest
RELEASE loHost
RELEASE loNow
RELEASE loTimestamp
RELEASE loCrypt
RELEASE loSignedHeaders
RELEASE loCanonical
RELEASE loScope
RELEASE loStringToSign
RELEASE loInitialKey
RELEASE loAuthorization
RELEASE loRequestUrl
RELEASE loResponse
CANCEL
ENDIF
IF (loResponse.StatusCode <> 200) THEN
? "Unexpected HTTP status: " + STR(loResponse.StatusCode)
RELEASE loStringCompare
RELEASE loCredentials
RELEASE loHttp
RELEASE loEndpoint
RELEASE loDeleteRequest
RELEASE loHost
RELEASE loNow
RELEASE loTimestamp
RELEASE loCrypt
RELEASE loSignedHeaders
RELEASE loCanonical
RELEASE loScope
RELEASE loStringToSign
RELEASE loInitialKey
RELEASE loAuthorization
RELEASE loRequestUrl
RELEASE loResponse
CANCEL
ENDIF
* HTTP 200 can contain per-object errors. Inspect the actual XML body.
loXml = CreateObject('Chilkat.Xml')
lnSuccess = loXml.LoadXml(loResponse.BodyStr)
IF (lnSuccess <> 1) THEN
? "Invalid XML response."
RELEASE loStringCompare
RELEASE loCredentials
RELEASE loHttp
RELEASE loEndpoint
RELEASE loDeleteRequest
RELEASE loHost
RELEASE loNow
RELEASE loTimestamp
RELEASE loCrypt
RELEASE loSignedHeaders
RELEASE loCanonical
RELEASE loScope
RELEASE loStringToSign
RELEASE loInitialKey
RELEASE loAuthorization
RELEASE loRequestUrl
RELEASE loResponse
RELEASE loXml
CANCEL
ENDIF
loStringCompare.SetString(loXml.Tag)
lnStringMatch4 = loStringCompare.ContentsEqual("DeleteResult",1)
IF (lnStringMatch4 <> 1) THEN
? "Unexpected XML response root."
RELEASE loStringCompare
RELEASE loCredentials
RELEASE loHttp
RELEASE loEndpoint
RELEASE loDeleteRequest
RELEASE loHost
RELEASE loNow
RELEASE loTimestamp
RELEASE loCrypt
RELEASE loSignedHeaders
RELEASE loCanonical
RELEASE loScope
RELEASE loStringToSign
RELEASE loInitialKey
RELEASE loAuthorization
RELEASE loRequestUrl
RELEASE loResponse
RELEASE loXml
CANCEL
ENDIF
lnFailures = loXml.NumChildrenHavingTag("Error")
i = 0
DO WHILE (i < lnFailures)
loXml.I = i
? "Failed key: " + loXml.GetChildContent("Error[i]|Key")
? "Error code: " + loXml.GetChildContent("Error[i]|Code")
i = i + 1
ENDDO
IF (lnFailures <> 0) THEN
? "One or more objects were not deleted. Retry only appropriate failed identifiers."
RELEASE loStringCompare
RELEASE loCredentials
RELEASE loHttp
RELEASE loEndpoint
RELEASE loDeleteRequest
RELEASE loHost
RELEASE loNow
RELEASE loTimestamp
RELEASE loCrypt
RELEASE loSignedHeaders
RELEASE loCanonical
RELEASE loScope
RELEASE loStringToSign
RELEASE loInitialKey
RELEASE loAuthorization
RELEASE loRequestUrl
RELEASE loResponse
RELEASE loXml
CANCEL
ENDIF
* Quiet=true means success entries need not be returned.
? "Delete batch succeeded with no reported object errors."
RELEASE loStringCompare
RELEASE loCredentials
RELEASE loHttp
RELEASE loEndpoint
RELEASE loDeleteRequest
RELEASE loHost
RELEASE loNow
RELEASE loTimestamp
RELEASE loCrypt
RELEASE loSignedHeaders
RELEASE loCanonical
RELEASE loScope
RELEASE loStringToSign
RELEASE loInitialKey
RELEASE loAuthorization
RELEASE loRequestUrl
RELEASE loResponse
RELEASE loXml