Azure Blob: Delete a Blob Deliberately
See more Azure Cloud Storage Examples
Destructive; disabled by default. Verify the target before enabling applyChange. Deleting the base blob can fail when snapshots exist; this example does not silently add x-ms-delete-snapshots: include. Handle snapshot/version deletion deliberately. Supply an active lease ID only if needed. Soft-delete, versioning, retention, immutability and legal-hold settings affect recovery and deletion; HTTP 202 is acceptance, not proof that recovery is impossible.
Uses direct Chilkat REST calls with HTTPS certificate verification. Shared Key examples load a private qa_data/azure/storage.json file containing string fields account and access_key (the base64 account key). SAS consumers need only account there. Create local input/output directories before running. Examples target Azure public-cloud endpoints; adapt hostnames for other clouds or custom endpoints.
REST examples pin x-ms-version: 2026-04-06, and SAS generation uses the same service version. Complete-URL downloads use the supplied SAS version. This is a fully deployed version documented by Microsoft at review time. Older supported versions are not automatically obsolete. Transport completion and HTTP success are checked separately; writes are not automatically replayed after a lost response. Keep diagnostic dumps, keys and SAS URLs private.
Shared Key remains supported where allowed by account policy. Microsoft recommends Microsoft Entra ID, preferably managed identities where available, and user delegation SAS for delegated Blob access. Account-key SAS and Shared Key requests can be rejected when Shared Key access is disabled; these samples do not bypass that policy. See Azure data authorization.
Microsoft delete blob reference.
Chilkat Tcl Downloads
load ./chilkat.dll
set success 0
# StringBuilder compares text by value in every generated language.
set textCompare [new_CkStringBuilder]
set success 0
set applyChange 0
if {$applyChange != 1} then {
puts "Deletion disabled. Review the target and set applyChange to true deliberately."
delete_CkStringBuilder $textCompare
exit
}
# Assumes Chilkat is unlocked. See Global Unlock Sample.
set config [new_CkJsonObject]
CkJsonObject_put_Strict $config 1
set success [CkJsonObject_LoadFile $config "qa_data/azure/storage.json"]
if {$success != 1} then {
puts "Could not load private Azure configuration."
delete_CkStringBuilder $textCompare
delete_CkJsonObject $config
exit
}
CkStringBuilder_Clear $textCompare
CkStringBuilder_SetString $textCompare [CkJsonObject_stringOf $config "account"]
set sameText1 [CkStringBuilder_ContentsEqual $textCompare "" 1]
if {expr [[CkJsonObject_JsonTypeOf $config "account"] != 1] || [$sameText1 == 1]} then {
puts "Supply the storage account name."
delete_CkStringBuilder $textCompare
delete_CkJsonObject $config
exit
}
# access_key is the base64 account key, not a connection string or SAS.
CkStringBuilder_Clear $textCompare
CkStringBuilder_SetString $textCompare [CkJsonObject_stringOf $config "access_key"]
set sameText2 [CkStringBuilder_ContentsEqual $textCompare "" 1]
if {expr [[CkJsonObject_JsonTypeOf $config "access_key"] != 1] || [$sameText2 == 1]} then {
puts "Supply the base64 account access key."
delete_CkStringBuilder $textCompare
delete_CkJsonObject $config
exit
}
set endpoint [new_CkStringBuilder]
set success [CkStringBuilder_Append $endpoint [CkJsonObject_stringOf $config "account"]]
if {$success != 1} then {
puts "Could not prepare the request."
delete_CkStringBuilder $textCompare
delete_CkJsonObject $config
delete_CkStringBuilder $endpoint
exit
}
set success [CkStringBuilder_Append $endpoint ".blob.core.windows.net"]
if {$success != 1} then {
puts "Could not prepare the request."
delete_CkStringBuilder $textCompare
delete_CkJsonObject $config
delete_CkStringBuilder $endpoint
exit
}
# resourcePath is already URL-encoded. Encode each path segment once when adapting it.
# Keep slash separators; names and paths are case-sensitive.
set resourcePath "/examplecontainer/example.bin"
set rest [new_CkRest]
CkRest_put_ConnectTimeoutMs $rest 20000
CkRest_put_IdleTimeoutMs $rest 60000
set azAuth [new_CkAuthAzureStorage]
CkAuthAzureStorage_put_Account $azAuth [CkJsonObject_stringOf $config "account"]
CkAuthAzureStorage_put_AccessKey $azAuth [CkJsonObject_stringOf $config "access_key"]
CkAuthAzureStorage_put_Service $azAuth "Blob"
CkAuthAzureStorage_put_Scheme $azAuth "SharedKey"
CkAuthAzureStorage_put_XMsVersion $azAuth "2026-04-06"
set success [CkRest_SetAuthAzureStorage $rest $azAuth]
if {$success != 1} then {
puts "Could not configure Shared Key signing."
delete_CkStringBuilder $textCompare
delete_CkJsonObject $config
delete_CkStringBuilder $endpoint
delete_CkRest $rest
delete_CkAuthAzureStorage $azAuth
exit
}
set connection [new_CkSocket]
CkSocket_put_RequireSslCertVerify $connection 1
CkSocket_put_MaxReadIdleMs $connection 60000
CkSocket_put_MaxSendIdleMs $connection 60000
set success [CkSocket_Connect $connection [CkStringBuilder_getAsString $endpoint] 443 1 20000]
if {$success != 1} then {
puts "TLS connection failed; inspect diagnostics privately."
delete_CkStringBuilder $textCompare
delete_CkJsonObject $config
delete_CkStringBuilder $endpoint
delete_CkRest $rest
delete_CkAuthAzureStorage $azAuth
delete_CkSocket $connection
exit
}
# Do not replay a write automatically after losing its response.
set success [CkRest_UseConnection $rest $connection 0]
if {$success != 1} then {
puts "Could not prepare the request."
delete_CkStringBuilder $textCompare
delete_CkJsonObject $config
delete_CkStringBuilder $endpoint
delete_CkRest $rest
delete_CkAuthAzureStorage $azAuth
delete_CkSocket $connection
exit
}
set result [new_CkXml]
CkXml_put_Strict $result 1
# Supply a real active lease ID only when the target has a lease.
set leaseId ""
CkStringBuilder_Clear $textCompare
CkStringBuilder_SetString $textCompare $leaseId
set sameText3 [CkStringBuilder_ContentsEqual $textCompare "" 1]
if {$sameText3 != 1} then {
set success [CkRest_AddHeader $rest "x-ms-lease-id" $leaseId]
if {$success != 1} then {
puts "Could not prepare the request."
delete_CkStringBuilder $textCompare
delete_CkJsonObject $config
delete_CkStringBuilder $endpoint
delete_CkRest $rest
delete_CkAuthAzureStorage $azAuth
delete_CkSocket $connection
delete_CkXml $result
exit
}
}
set responseText [CkRest_fullRequestNoBody $rest "DELETE" $resourcePath]
if {[CkRest_get_LastMethodSuccess $rest] != 1} then {
puts "Request did not complete; outcome may be uncertain."
delete_CkStringBuilder $textCompare
delete_CkJsonObject $config
delete_CkStringBuilder $endpoint
delete_CkRest $rest
delete_CkAuthAzureStorage $azAuth
delete_CkSocket $connection
delete_CkXml $result
exit
}
if {[CkRest_get_ResponseStatusCode $rest] != 202} then {
puts "HTTP status: [CkRest_get_ResponseStatusCode $rest]"
puts "Azure error code: [CkRest_responseHdrByName $rest x-ms-error-code]"
puts "Request ID: [CkRest_responseHdrByName $rest x-ms-request-id]"
# Inspect any error body privately; do not log credentials, SAS URLs or payloads.
delete_CkStringBuilder $textCompare
delete_CkJsonObject $config
delete_CkStringBuilder $endpoint
delete_CkRest $rest
delete_CkAuthAzureStorage $azAuth
delete_CkSocket $connection
delete_CkXml $result
exit
}
puts "Deletion accepted."
delete_CkStringBuilder $textCompare
delete_CkJsonObject $config
delete_CkStringBuilder $endpoint
delete_CkRest $rest
delete_CkAuthAzureStorage $azAuth
delete_CkSocket $connection
delete_CkXml $result