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

Azure Storage Accounts: Configure Blob Soft Delete and Versioning

See more Azure Storage Accounts Examples

Enables 14-day blob/container soft delete and blob versioning through ARM. The example checks for standard StorageV2 without hierarchical namespace, reads existing Blob service properties, and retains unrelated settings such as CORS. It refuses to shorten soft-delete retention below what an existing point-in-time restore policy requires.

Review retention, feature compatibility, existing policies, and storage costs before running. Versioning protects subsequent changes; it does not reconstruct overwritten historical data. Soft delete and versioning do not protect against deleting the entire storage account. Serialize competing service-configuration writers. See blob versioning.

These calls manage Azure resources at management.azure.com. They do not upload or download stored data. Supply a current Microsoft Entra access token for Azure Resource Manager in private qa_data/tokens/azureToken.json, with a nonempty access_token string. For v2 client credentials, request https://management.azure.com/.default; obtain a new token when it expires. A storage account key, SAS, Microsoft Graph token, or Storage data-plane token is not an ARM token. Never print the token or commit its file.

Azure RBAC must grant the caller the required management actions at the appropriate scope. An app registration alone does not grant resource access. Management permissions and blob/file data permissions are distinct. These examples target public Azure; sovereign clouds require the corresponding authority, ARM endpoint, token audience, and continuation-host validation.

Microsoft API reference

Chilkat AutoIt Downloads

AutoIt
Local $bSuccess = False

;  This example requires the Chilkat API to have been previously unlocked.
;  See Global.UnlockBundle for sample code.

;  This is Azure Resource Manager (the management plane), not a blob data request.
;  Load an unexpired Microsoft Entra token whose audience is Azure Resource Manager.
;  For the v2 token endpoint, request scope https://management.azure.com/.default.
;  Keep the token file private and out of source control and public web directories.
$oToken = ObjCreate("Chilkat.JsonObject")
$oToken.Strict = True
$bSuccess = $oToken.LoadFile("qa_data/tokens/azureToken.json")
If ($bSuccess <> True) Then
    ConsoleWrite("Could not load the private Azure Resource Manager token file." & @CRLF)
    Exit
EndIf

If (($oToken.JsonTypeOf("access_token") <> 1) Or ($oToken.StringOfEquals("access_token","",True) = True)) Then
    ConsoleWrite("A nonempty access_token string is required." & @CRLF)
    Exit
EndIf

;  Use your subscription ID as a string. The all-zero value is only a placeholder.
Local $subscriptionId = "00000000-0000-0000-0000-000000000000"
$oHttp = ObjCreate("Chilkat.Http")
$oHttp.AuthToken = $oToken.StringOf("access_token")
$oHttp.Accept = "application/json"
$oHttp.RequireSslCertVerify = True
$oHttp.FollowRedirects = False
$oHttp.ConnectTimeout = 30
$oHttp.ReadTimeout = 60
$bSuccess = $oHttp.SetUrlVar("subscriptionId",$subscriptionId)
If ($bSuccess <> True) Then
    ConsoleWrite("Could not encode subscription ID." & @CRLF)
    Exit
EndIf

$oResponse = ObjCreate("Chilkat.HttpResponse")
$oResult = ObjCreate("Chilkat.JsonObject")
$oResult.Strict = True
$oParsed = ObjCreate("Chilkat.Url")
Local $sResourceGroupName = "my-resource-group"
$bSuccess = $oHttp.SetUrlVar("resourceGroupName",$sResourceGroupName)
If ($bSuccess <> True) Then
    ConsoleWrite("Could not encode resource group." & @CRLF)
    Exit
EndIf

Local $sAccountName = "youruniquestorageaccount"
$bSuccess = $oHttp.SetUrlVar("accountName",$sAccountName)
If ($bSuccess <> True) Then
    ConsoleWrite("Could not encode account name." & @CRLF)
    Exit
EndIf

Local $sResourceUrl = "https://management.azure.com/subscriptions/{$subscriptionId}/resourceGroups/{$resourceGroupName}/providers/Microsoft.Storage/storageAccounts/{$accountName}?api-version=2026-09-01"
$bSuccess = $oHttp.HttpNoBody("GET",$sResourceUrl,$oResponse)
If ($oResponse.StatusCode = 0) Then
    ConsoleWrite("No HTTP response. Inspect LastErrorText privately; it may contain credentials." & @CRLF)
    Exit
EndIf

If (($oResponse.StatusCode <> 200)) Then
    ConsoleWrite("HTTP status: " & $oResponse.StatusCode & @CRLF)
    ConsoleWrite("Request ID: " & $oResponse.GetHeaderField("x-ms-request-id") & @CRLF)
    ConsoleWrite("Retry-After: " & $oResponse.GetHeaderField("Retry-After") & @CRLF)
    ;  Inspect the error body privately. Do not log tokens, headers, or full responses.
    ;  For 401 check the token/audience; for 403 check Azure RBAC and policy.
    ;  Honor Retry-After for 429 and use bounded backoff for transient failures.
    Exit
EndIf

If ($bSuccess <> True) Then
    ConsoleWrite("The HTTP response could not be read completely." & @CRLF)
    Exit
EndIf

$bSuccess = $oResult.Load($oResponse.BodyStr)
If ($bSuccess <> True) Then
    ConsoleWrite("Expected a well-formed JSON response." & @CRLF)
    Exit
EndIf

;  This demonstration targets standard StorageV2 without hierarchical namespace.
If (($oResult.StringOfEquals("kind","StorageV2",True) <> True) Or ($oResult.StringOfEquals("sku.tier","Standard",True) <> True) Or ($oResult.BoolOf("properties.isHnsEnabled") = True)) Then
    ConsoleWrite("This versioning example requires standard StorageV2 without hierarchical namespace." & @CRLF)
    Exit
EndIf

Local $sBlobUrl = "https://management.azure.com/subscriptions/{$subscriptionId}/resourceGroups/{$resourceGroupName}/providers/Microsoft.Storage/storageAccounts/{$accountName}/blobServices/default?api-version=2026-09-01"
$bSuccess = $oHttp.HttpNoBody("GET",$sBlobUrl,$oResponse)
If ($oResponse.StatusCode = 0) Then
    ConsoleWrite("No HTTP response. Inspect LastErrorText privately; it may contain credentials." & @CRLF)
    Exit
EndIf

If (($oResponse.StatusCode <> 200)) Then
    ConsoleWrite("HTTP status: " & $oResponse.StatusCode & @CRLF)
    ConsoleWrite("Request ID: " & $oResponse.GetHeaderField("x-ms-request-id") & @CRLF)
    ConsoleWrite("Retry-After: " & $oResponse.GetHeaderField("Retry-After") & @CRLF)
    ;  Inspect the error body privately. Do not log tokens, headers, or full responses.
    ;  For 401 check the token/audience; for 403 check Azure RBAC and policy.
    ;  Honor Retry-After for 429 and use bounded backoff for transient failures.
    Exit
EndIf

If ($bSuccess <> True) Then
    ConsoleWrite("The HTTP response could not be read completely." & @CRLF)
    Exit
EndIf

$bSuccess = $oResult.Load($oResponse.BodyStr)
If ($bSuccess <> True) Then
    ConsoleWrite("Expected a well-formed JSON response." & @CRLF)
    Exit
EndIf

If ($oResult.JsonTypeOf("properties") <> 3) Then
    ConsoleWrite("Missing or invalid properties." & @CRLF)
    Exit
EndIf

$oProperties = ObjCreate("Chilkat.JsonObject")
$bSuccess = $oResult.ObjectOf2("properties",$oProperties)
If ($bSuccess <> True) Then
    ConsoleWrite("Could not read blob-service properties." & @CRLF)
    Exit
EndIf

;  Preserve CORS and unrelated service settings. Remove deprecated/read-only fields.
$oProperties.Delete("automaticSnapshotPolicyEnabled")
$oRestore = ObjCreate("Chilkat.JsonObject")
If ($oProperties.JsonTypeOf("restorePolicy") = 3) Then
    $bSuccess = $oProperties.ObjectOf2("restorePolicy",$oRestore)
    If ($bSuccess <> True) Then
        ConsoleWrite("Could not read restore policy." & @CRLF)
        Exit
    EndIf

    $oRestore.Delete("lastEnabledTime")
    $oRestore.Delete("minRestoreTime")
    If (($oRestore.BoolOf("enabled") = True) And ($oRestore.IntOf("days") >= 14)) Then
        ConsoleWrite("Existing point-in-time restore needs a longer soft-delete retention. Choose retention deliberately." & @CRLF)
        Exit
    EndIf

EndIf

$oAccessTracking = ObjCreate("Chilkat.JsonObject")
If ($oProperties.JsonTypeOf("lastAccessTimeTrackingPolicy") = 3) Then
    $bSuccess = $oProperties.ObjectOf2("lastAccessTimeTrackingPolicy",$oAccessTracking)
    If ($bSuccess <> True) Then
        ConsoleWrite("Could not read access tracking settings." & @CRLF)
        Exit
    EndIf

    $oAccessTracking.Delete("name")
    $oAccessTracking.Delete("trackingGranularityInDays")
    $oAccessTracking.Delete("blobType")
EndIf

$oProperties.UpdateBool("deleteRetentionPolicy.enabled",True)
$oProperties.UpdateInt("deleteRetentionPolicy.days",14)
$oProperties.UpdateBool("containerDeleteRetentionPolicy.enabled",True)
$oProperties.UpdateInt("containerDeleteRetentionPolicy.days",14)
$oProperties.UpdateBool("isVersioningEnabled",True)
$oRequest = ObjCreate("Chilkat.JsonObject")
$bSuccess = $oRequest.AppendObjectCopy("properties",$oProperties)
If ($bSuccess <> True) Then
    ConsoleWrite("Could not copy blob-service settings." & @CRLF)
    Exit
EndIf

;  This PUT can replace service settings. Do not send only a few fields and assume
;  omitted CORS rules survive. Coordinate writers around the GET and PUT.
;  On a write timeout, read the resource before deciding whether to retry.
$bSuccess = $oHttp.HttpJson("PUT",$sBlobUrl,$oRequest,"application/json",$oResponse)
If ($oResponse.StatusCode = 0) Then
    ConsoleWrite("No HTTP response. Inspect LastErrorText privately; it may contain credentials." & @CRLF)
    Exit
EndIf

If (($oResponse.StatusCode <> 200)) Then
    ConsoleWrite("HTTP status: " & $oResponse.StatusCode & @CRLF)
    ConsoleWrite("Request ID: " & $oResponse.GetHeaderField("x-ms-request-id") & @CRLF)
    ConsoleWrite("Retry-After: " & $oResponse.GetHeaderField("Retry-After") & @CRLF)
    ;  Inspect the error body privately. Do not log tokens, headers, or full responses.
    ;  For 401 check the token/audience; for 403 check Azure RBAC and policy.
    ;  Honor Retry-After for 429 and use bounded backoff for transient failures.
    Exit
EndIf

If ($bSuccess <> True) Then
    ConsoleWrite("The HTTP response could not be read completely." & @CRLF)
    Exit
EndIf

$bSuccess = $oResult.Load($oResponse.BodyStr)
If ($bSuccess <> True) Then
    ConsoleWrite("Expected a well-formed JSON response." & @CRLF)
    Exit
EndIf

ConsoleWrite("Blob protection settings updated. Retention and versions can increase storage costs." & @CRLF)