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 Classic ASP Downloads
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
</head>
<body>
<%
success = 0
set stringCompare = Server.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.
set credentials = Server.CreateObject("Chilkat.JsonObject")
credentials.Strict = 1
success = credentials.LoadFile("qa_data/aws/s3.json")
If (success <> 1) Then
Response.Write "<pre>" & Server.HTMLEncode( "Could not load private S3 credentials.") & "</pre>"
Response.End
End If
success = stringCompare.SetString(credentials.StringOf("access_key"))
stringMatch1 = stringCompare.ContentsEqual("",1)
If ((credentials.JsonTypeOf("access_key") <> 1) Or (stringMatch1 = 1)) Then
Response.Write "<pre>" & Server.HTMLEncode( "Missing or invalid access_key.") & "</pre>"
Response.End
End If
success = stringCompare.SetString(credentials.StringOf("secret_key"))
stringMatch2 = stringCompare.ContentsEqual("",1)
If ((credentials.JsonTypeOf("secret_key") <> 1) Or (stringMatch2 = 1)) Then
Response.Write "<pre>" & Server.HTMLEncode( "Missing or invalid secret_key.") & "</pre>"
Response.End
End If
set http = Server.CreateObject("Chilkat.Http")
http.AwsAccessKey = credentials.StringOf("access_key")
http.AwsSecretKey = credentials.StringOf("secret_key")
If (credentials.HasMember("session_token") = 1) Then
success = stringCompare.SetString(credentials.StringOf("session_token"))
stringMatch3 = stringCompare.ContentsEqual("",1)
If ((credentials.JsonTypeOf("session_token") <> 1) Or (stringMatch3 = 1)) Then
Response.Write "<pre>" & Server.HTMLEncode( "session_token must be a nonempty string when supplied.") & "</pre>"
Response.End
End If
http.AwsSessionToken = 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.
region = "us-west-2"
http.AwsRegion = region
set endpoint = Server.CreateObject("Chilkat.StringBuilder")
success = endpoint.Append("s3.")
success = endpoint.Append(region)
success = endpoint.Append(".amazonaws.com")
http.AwsEndpoint = endpoint.GetAsString()
http.RequireSslCertVerify = 1
http.FollowRedirects = 0
' A region redirect is not success. Correct the endpoint and signing region.
bucketName = "your-bucket-name"
set deleteRequest = Server.CreateObject("Chilkat.Xml")
deleteRequest.Tag = "Delete"
success = deleteRequest.AddAttribute("xmlns","http://s3.amazonaws.com/doc/2006-03-01/")
deleteRequest.UpdateChildContent "Quiet","true"
' XML APIs escape key text. Do NOT URL-encode keys in this XML body.
deleteRequest.UpdateChildContent "Object[0]|Key","images/seahorse.jpg"
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 ((deleteRequest.NumChildrenHavingTag("Object") < 1) Or (deleteRequest.NumChildrenHavingTag("Object") > 1000)) Then
Response.Write "<pre>" & Server.HTMLEncode( "Use a batch of 1 to 1,000 object identifiers.") & "</pre>"
Response.End
End If
requestBody = 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.
set host = Server.CreateObject("Chilkat.StringBuilder")
success = host.Append(bucketName)
success = host.Append(".")
success = host.Append(endpoint.GetAsString())
set now = Server.CreateObject("Chilkat.CkDateTime")
success = now.SetFromCurrentSystemTime()
If (success <> 1) Then
Response.Write "<pre>" & Server.HTMLEncode( "Could not read system time.") & "</pre>"
Response.End
End If
set timestamp = Server.CreateObject("Chilkat.StringBuilder")
success = timestamp.Append(now.GetAsTimestamp(0))
removedDashes = timestamp.Replace("-","")
removedColons = timestamp.Replace(":","")
amzDate = timestamp.GetAsString()
dateStamp = timestamp.GetRange(0,8,0)
set crypt = Server.CreateObject("Chilkat.Crypt2")
crypt.Charset = "utf-8"
crypt.HashAlgorithm = "md5"
crypt.EncodingMode = "base64"
contentMd5 = crypt.HashStringENC(requestBody)
If (crypt.LastMethodSuccess <> 1) Then
Response.Write "<pre>" & Server.HTMLEncode( "Could not hash delete body.") & "</pre>"
Response.End
End If
crypt.HashAlgorithm = "sha256"
crypt.EncodingMode = "hexlower"
payloadHash = crypt.HashStringENC(requestBody)
If (crypt.LastMethodSuccess <> 1) Then
Response.Write "<pre>" & Server.HTMLEncode( "Could not hash delete body.") & "</pre>"
Response.End
End If
set signedHeaders = Server.CreateObject("Chilkat.StringBuilder")
success = signedHeaders.Append("content-md5;host;x-amz-content-sha256;x-amz-date")
set canonical = Server.CreateObject("Chilkat.StringBuilder")
success = canonical.Append("POST" & vbLf & "/" & vbLf & "delete=" & vbLf & "content-md5:")
success = canonical.Append(contentMd5)
success = canonical.Append(vbLf & "host:")
success = canonical.Append(host.GetAsString())
success = canonical.Append(vbLf & "x-amz-content-sha256:")
success = canonical.Append(payloadHash)
success = canonical.Append(vbLf & "x-amz-date:")
success = canonical.Append(amzDate)
success = canonical.Append(vbLf)
If (credentials.HasMember("session_token") = 1) Then
success = signedHeaders.Append(";x-amz-security-token")
success = canonical.Append("x-amz-security-token:")
success = canonical.Append(credentials.StringOf("session_token"))
success = canonical.Append(vbLf)
http.SetRequestHeader "x-amz-security-token",credentials.StringOf("session_token")
End If
success = canonical.Append(vbLf)
success = canonical.Append(signedHeaders.GetAsString())
success = canonical.Append(vbLf)
success = canonical.Append(payloadHash)
canonicalHash = crypt.HashStringENC(canonical.GetAsString())
If (crypt.LastMethodSuccess <> 1) Then
Response.Write "<pre>" & Server.HTMLEncode( "Could not hash canonical request.") & "</pre>"
Response.End
End If
set scope = Server.CreateObject("Chilkat.StringBuilder")
success = scope.Append(dateStamp)
success = scope.Append("/")
success = scope.Append(region)
success = scope.Append("/s3/aws4_request")
set stringToSign = Server.CreateObject("Chilkat.StringBuilder")
success = stringToSign.Append("AWS4-HMAC-SHA256" & vbLf)
success = stringToSign.Append(amzDate)
success = stringToSign.Append(vbLf)
success = stringToSign.Append(scope.GetAsString())
success = stringToSign.Append(vbLf)
success = stringToSign.Append(canonicalHash)
set initialKey = Server.CreateObject("Chilkat.StringBuilder")
success = initialKey.Append("AWS4")
success = initialKey.Append(credentials.StringOf("secret_key"))
crypt.MacAlgorithm = "hmac"
success = crypt.SetMacKeyString(initialKey.GetAsString())
If (success <> 1) Then
Response.Write "<pre>" & Server.HTMLEncode( "Could not set signing key.") & "</pre>"
Response.End
End If
derivedKey = crypt.MacStringENC(dateStamp)
If (crypt.LastMethodSuccess <> 1) Then
Response.Write "<pre>" & Server.HTMLEncode( "Could not derive date signing key.") & "</pre>"
Response.End
End If
success = crypt.SetMacKeyEncoded(derivedKey,"hex")
If (success <> 1) Then
Response.Write "<pre>" & Server.HTMLEncode( "Could not set derived signing key.") & "</pre>"
Response.End
End If
derivedKey = crypt.MacStringENC(region)
If (crypt.LastMethodSuccess <> 1) Then
Response.Write "<pre>" & Server.HTMLEncode( "Could not derive signing key.") & "</pre>"
Response.End
End If
success = crypt.SetMacKeyEncoded(derivedKey,"hex")
If (success <> 1) Then
Response.Write "<pre>" & Server.HTMLEncode( "Could not set derived signing key.") & "</pre>"
Response.End
End If
derivedKey = crypt.MacStringENC("s3")
If (crypt.LastMethodSuccess <> 1) Then
Response.Write "<pre>" & Server.HTMLEncode( "Could not derive signing key.") & "</pre>"
Response.End
End If
success = crypt.SetMacKeyEncoded(derivedKey,"hex")
If (success <> 1) Then
Response.Write "<pre>" & Server.HTMLEncode( "Could not set derived signing key.") & "</pre>"
Response.End
End If
derivedKey = crypt.MacStringENC("aws4_request")
If (crypt.LastMethodSuccess <> 1) Then
Response.Write "<pre>" & Server.HTMLEncode( "Could not derive signing key.") & "</pre>"
Response.End
End If
success = crypt.SetMacKeyEncoded(derivedKey,"hex")
If (success <> 1) Then
Response.Write "<pre>" & Server.HTMLEncode( "Could not set final signing key.") & "</pre>"
Response.End
End If
signature = crypt.MacStringENC(stringToSign.GetAsString())
If (crypt.LastMethodSuccess <> 1) Then
Response.Write "<pre>" & Server.HTMLEncode( "Could not sign delete request.") & "</pre>"
Response.End
End If
set authorization = Server.CreateObject("Chilkat.StringBuilder")
success = authorization.Append("AWS4-HMAC-SHA256 Credential=")
success = authorization.Append(credentials.StringOf("access_key"))
success = authorization.Append("/")
success = authorization.Append(scope.GetAsString())
success = authorization.Append(", SignedHeaders=")
success = authorization.Append(signedHeaders.GetAsString())
success = authorization.Append(", Signature=")
success = authorization.Append(signature)
http.SetRequestHeader "Authorization",authorization.GetAsString()
http.SetRequestHeader "Content-MD5",contentMd5
http.SetRequestHeader "x-amz-content-sha256",payloadHash
http.SetRequestHeader "x-amz-date",amzDate
set requestUrl = Server.CreateObject("Chilkat.StringBuilder")
success = requestUrl.Append("https://")
success = requestUrl.Append(host.GetAsString())
success = requestUrl.Append("/?delete=")
ERROR: ASP example cannot use request or response as a variable name.
success = http.HttpStr("POST",requestUrl.GetAsString(),requestBody,"utf-8","application/xml",response)
If (success <> 1) Then
Response.Write "<pre>" & Server.HTMLEncode( "Request failed (transport or S3 error). HTTP status: " & http.LastStatus) & "</pre>"
' Inspect LastErrorText privately. Do not log credentials or signed URLs.
Response.End
End If
If (response.StatusCode <> 200) Then
Response.Write "<pre>" & Server.HTMLEncode( "Unexpected HTTP status: " & response.StatusCode) & "</pre>"
Response.End
End If
' HTTP 200 can contain per-object errors. Inspect the actual XML body.
set xml = Server.CreateObject("Chilkat.Xml")
success = xml.LoadXml(response.BodyStr)
If (success <> 1) Then
Response.Write "<pre>" & Server.HTMLEncode( "Invalid XML response.") & "</pre>"
Response.End
End If
success = stringCompare.SetString(xml.Tag)
stringMatch4 = stringCompare.ContentsEqual("DeleteResult",1)
If (stringMatch4 <> 1) Then
Response.Write "<pre>" & Server.HTMLEncode( "Unexpected XML response root.") & "</pre>"
Response.End
End If
failures = xml.NumChildrenHavingTag("Error")
i = 0
Do While (i < failures)
xml.I = i
Response.Write "<pre>" & Server.HTMLEncode( "Failed key: " & xml.GetChildContent("Error[i]|Key")) & "</pre>"
Response.Write "<pre>" & Server.HTMLEncode( "Error code: " & xml.GetChildContent("Error[i]|Code")) & "</pre>"
i = i + 1
Loop
If (failures <> 0) Then
Response.Write "<pre>" & Server.HTMLEncode( "One or more objects were not deleted. Retry only appropriate failed identifiers.") & "</pre>"
Response.End
End If
' Quiet=true means success entries need not be returned.
Response.Write "<pre>" & Server.HTMLEncode( "Delete batch succeeded with no reported object errors.") & "</pre>"
%>
</body>
</html>