Sample code for 30+ languages & platforms
B4X 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 B4X Downloads

B4X
Dim success As Boolean = 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.
Dim token As ChilkatJsonObject
token.Initialize
token.Strict = True
success = token.LoadFile("qa_data/tokens/azureToken.json")
If success <> True Then
    Log("Could not load the private Azure Resource Manager token file.")
    Return
End If

If (token.JsonTypeOf("access_token") <> 1) Or (token.StringOfEquals("access_token", "", True) = True) Then
    Log("A nonempty access_token string is required.")
    Return
End If


'  Use your subscription ID as a string. The all-zero value is only a placeholder.
Dim subscriptionId As String = "00000000-0000-0000-0000-000000000000"
Dim http As ChilkatHttp
http.Initialize("http")
http.AuthToken = token.StringOf("access_token")
http.Accept = "application/json"
http.RequireSslCertVerify = True
http.FollowRedirects = False
http.ConnectTimeout = 30
http.ReadTimeout = 60
success = http.SetUrlVar("subscriptionId", subscriptionId)
If success <> True Then
    Log("Could not encode subscription ID.")
    Return
End If


Dim response As ChilkatHttpResponse
response.Initialize
Dim result As ChilkatJsonObject
result.Initialize
result.Strict = True
Dim parsed As ChilkatUrl
parsed.Initialize
Dim resourceGroupName As String = "my-resource-group"
success = http.SetUrlVar("resourceGroupName", resourceGroupName)
If success <> True Then
    Log("Could not encode resource group.")
    Return
End If

Dim accountName As String = "youruniquestorageaccount"
success = http.SetUrlVar("accountName", accountName)
If success <> True Then
    Log("Could not encode account name.")
    Return
End If

Dim resourceUrl As String = "https://management.azure.com/subscriptions/{$subscriptionId}/resourceGroups/{$resourceGroupName}/providers/Microsoft.Storage/storageAccounts/{$accountName}?api-version=2026-09-01"
success = http.HttpNoBody("GET", resourceUrl, response)
If response.StatusCode = 0 Then
    Log("No HTTP response. Inspect LastErrorText privately; it may contain credentials.")
    Return
End If

If (response.StatusCode <> 200) Then
    Log("HTTP status: " & response.StatusCode)
    Log("Request ID: " & response.GetHeaderField("x-ms-request-id"))
    Log("Retry-After: " & response.GetHeaderField("Retry-After"))
    '  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.
    Return
End If

If success <> True Then
    Log("The HTTP response could not be read completely.")
    Return
End If

success = result.Load(response.BodyStr)
If success <> True Then
    Log("Expected a well-formed JSON response.")
    Return
End If


'  This demonstration targets standard StorageV2 without hierarchical namespace.
If (result.StringOfEquals("kind", "StorageV2", True) <> True) Or (result.StringOfEquals("sku.tier", "Standard", True) <> True) Or (result.BoolOf("properties.isHnsEnabled") = True) Then
    Log("This versioning example requires standard StorageV2 without hierarchical namespace.")
    Return
End If

Dim blobUrl As String = "https://management.azure.com/subscriptions/{$subscriptionId}/resourceGroups/{$resourceGroupName}/providers/Microsoft.Storage/storageAccounts/{$accountName}/blobServices/default?api-version=2026-09-01"
success = http.HttpNoBody("GET", blobUrl, response)
If response.StatusCode = 0 Then
    Log("No HTTP response. Inspect LastErrorText privately; it may contain credentials.")
    Return
End If

If (response.StatusCode <> 200) Then
    Log("HTTP status: " & response.StatusCode)
    Log("Request ID: " & response.GetHeaderField("x-ms-request-id"))
    Log("Retry-After: " & response.GetHeaderField("Retry-After"))
    '  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.
    Return
End If

If success <> True Then
    Log("The HTTP response could not be read completely.")
    Return
End If

success = result.Load(response.BodyStr)
If success <> True Then
    Log("Expected a well-formed JSON response.")
    Return
End If

If result.JsonTypeOf("properties") <> 3 Then
    Log("Missing or invalid properties.")
    Return
End If


Dim properties As ChilkatJsonObject
properties.Initialize
success = result.ObjectOf2("properties", properties)
If success <> True Then
    Log("Could not read blob-service properties.")
    Return
End If


'  Preserve CORS and unrelated service settings. Remove deprecated/read-only fields.
properties.Delete("automaticSnapshotPolicyEnabled")
Dim restore As ChilkatJsonObject
restore.Initialize
If properties.JsonTypeOf("restorePolicy") = 3 Then
    success = properties.ObjectOf2("restorePolicy", restore)
    If success <> True Then
        Log("Could not read restore policy.")
        Return
    End If


    restore.Delete("lastEnabledTime")
    restore.Delete("minRestoreTime")
    If (restore.BoolOf("enabled") = True) And (restore.IntOf("days") >= 14) Then
        Log("Existing point-in-time restore needs a longer soft-delete retention. Choose retention deliberately.")
        Return
    End If

End If

Dim accessTracking As ChilkatJsonObject
accessTracking.Initialize
If properties.JsonTypeOf("lastAccessTimeTrackingPolicy") = 3 Then
    success = properties.ObjectOf2("lastAccessTimeTrackingPolicy", accessTracking)
    If success <> True Then
        Log("Could not read access tracking settings.")
        Return
    End If


    accessTracking.Delete("name")
    accessTracking.Delete("trackingGranularityInDays")
    accessTracking.Delete("blobType")
End If

properties.UpdateBool("deleteRetentionPolicy.enabled", True)
properties.UpdateInt("deleteRetentionPolicy.days", 14)
properties.UpdateBool("containerDeleteRetentionPolicy.enabled", True)
properties.UpdateInt("containerDeleteRetentionPolicy.days", 14)
properties.UpdateBool("isVersioningEnabled", True)
Dim request As ChilkatJsonObject
request.Initialize
success = request.AppendObjectCopy("properties", properties)
If success <> True Then
    Log("Could not copy blob-service settings.")
    Return
End If


'  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.
success = http.HttpJson("PUT", blobUrl, request, "application/json", response)
If response.StatusCode = 0 Then
    Log("No HTTP response. Inspect LastErrorText privately; it may contain credentials.")
    Return
End If

If (response.StatusCode <> 200) Then
    Log("HTTP status: " & response.StatusCode)
    Log("Request ID: " & response.GetHeaderField("x-ms-request-id"))
    Log("Retry-After: " & response.GetHeaderField("Retry-After"))
    '  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.
    Return
End If

If success <> True Then
    Log("The HTTP response could not be read completely.")
    Return
End If

success = result.Load(response.BodyStr)
If success <> True Then
    Log("Expected a well-formed JSON response.")
    Return
End If

Log("Blob protection settings updated. Retention and versions can increase storage costs.")