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

VBScript
Dim fso, outFile
Set fso = CreateObject("Scripting.FileSystemObject")
'Create a Unicode (utf-16) output text file.
Set outFile = fso.CreateTextFile("output.txt", True, True)

success = 0

'  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.
set token = CreateObject("Chilkat.JsonObject")
token.Strict = 1
success = token.LoadFile("qa_data/tokens/azureToken.json")
If (success <> 1) Then
    outFile.WriteLine("Could not load the private Azure Resource Manager token file.")
    WScript.Quit
End If

If ((token.JsonTypeOf("access_token") <> 1) Or (token.StringOfEquals("access_token","",1) = 1)) Then
    outFile.WriteLine("A nonempty access_token string is required.")
    WScript.Quit
End If

'  Use your subscription ID as a string. The all-zero value is only a placeholder.
subscriptionId = "00000000-0000-0000-0000-000000000000"
set http = CreateObject("Chilkat.Http")
http.AuthToken = token.StringOf("access_token")
http.Accept = "application/json"
http.RequireSslCertVerify = 1
http.FollowRedirects = 0
http.ConnectTimeout = 30
http.ReadTimeout = 60
success = http.SetUrlVar("subscriptionId",subscriptionId)
If (success <> 1) Then
    outFile.WriteLine("Could not encode subscription ID.")
    WScript.Quit
End If

set response = CreateObject("Chilkat.HttpResponse")
set result = CreateObject("Chilkat.JsonObject")
result.Strict = 1
set parsed = CreateObject("Chilkat.Url")
resourceGroupName = "my-resource-group"
success = http.SetUrlVar("resourceGroupName",resourceGroupName)
If (success <> 1) Then
    outFile.WriteLine("Could not encode resource group.")
    WScript.Quit
End If

accountName = "youruniquestorageaccount"
success = http.SetUrlVar("accountName",accountName)
If (success <> 1) Then
    outFile.WriteLine("Could not encode account name.")
    WScript.Quit
End If

resourceUrl = "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
    outFile.WriteLine("No HTTP response. Inspect LastErrorText privately; it may contain credentials.")
    WScript.Quit
End If

If ((response.StatusCode <> 200)) Then
    outFile.WriteLine("HTTP status: " & response.StatusCode)
    outFile.WriteLine("Request ID: " & response.GetHeaderField("x-ms-request-id"))
    outFile.WriteLine("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.
    WScript.Quit
End If

If (success <> 1) Then
    outFile.WriteLine("The HTTP response could not be read completely.")
    WScript.Quit
End If

success = result.Load(response.BodyStr)
If (success <> 1) Then
    outFile.WriteLine("Expected a well-formed JSON response.")
    WScript.Quit
End If

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

blobUrl = "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
    outFile.WriteLine("No HTTP response. Inspect LastErrorText privately; it may contain credentials.")
    WScript.Quit
End If

If ((response.StatusCode <> 200)) Then
    outFile.WriteLine("HTTP status: " & response.StatusCode)
    outFile.WriteLine("Request ID: " & response.GetHeaderField("x-ms-request-id"))
    outFile.WriteLine("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.
    WScript.Quit
End If

If (success <> 1) Then
    outFile.WriteLine("The HTTP response could not be read completely.")
    WScript.Quit
End If

success = result.Load(response.BodyStr)
If (success <> 1) Then
    outFile.WriteLine("Expected a well-formed JSON response.")
    WScript.Quit
End If

If (result.JsonTypeOf("properties") <> 3) Then
    outFile.WriteLine("Missing or invalid properties.")
    WScript.Quit
End If

set properties = CreateObject("Chilkat.JsonObject")
success = result.ObjectOf2("properties",properties)
If (success <> 1) Then
    outFile.WriteLine("Could not read blob-service properties.")
    WScript.Quit
End If

'  Preserve CORS and unrelated service settings. Remove deprecated/read-only fields.
success = properties.Delete("automaticSnapshotPolicyEnabled")
set restore = CreateObject("Chilkat.JsonObject")
If (properties.JsonTypeOf("restorePolicy") = 3) Then
    success = properties.ObjectOf2("restorePolicy",restore)
    If (success <> 1) Then
        outFile.WriteLine("Could not read restore policy.")
        WScript.Quit
    End If

    success = restore.Delete("lastEnabledTime")
    success = restore.Delete("minRestoreTime")
    If ((restore.BoolOf("enabled") = 1) And (restore.IntOf("days") >= 14)) Then
        outFile.WriteLine("Existing point-in-time restore needs a longer soft-delete retention. Choose retention deliberately.")
        WScript.Quit
    End If

End If

set accessTracking = CreateObject("Chilkat.JsonObject")
If (properties.JsonTypeOf("lastAccessTimeTrackingPolicy") = 3) Then
    success = properties.ObjectOf2("lastAccessTimeTrackingPolicy",accessTracking)
    If (success <> 1) Then
        outFile.WriteLine("Could not read access tracking settings.")
        WScript.Quit
    End If

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

success = properties.UpdateBool("deleteRetentionPolicy.enabled",1)
success = properties.UpdateInt("deleteRetentionPolicy.days",14)
success = properties.UpdateBool("containerDeleteRetentionPolicy.enabled",1)
success = properties.UpdateInt("containerDeleteRetentionPolicy.days",14)
success = properties.UpdateBool("isVersioningEnabled",1)
set request = CreateObject("Chilkat.JsonObject")
success = request.AppendObjectCopy("properties",properties)
If (success <> 1) Then
    outFile.WriteLine("Could not copy blob-service settings.")
    WScript.Quit
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
    outFile.WriteLine("No HTTP response. Inspect LastErrorText privately; it may contain credentials.")
    WScript.Quit
End If

If ((response.StatusCode <> 200)) Then
    outFile.WriteLine("HTTP status: " & response.StatusCode)
    outFile.WriteLine("Request ID: " & response.GetHeaderField("x-ms-request-id"))
    outFile.WriteLine("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.
    WScript.Quit
End If

If (success <> 1) Then
    outFile.WriteLine("The HTTP response could not be read completely.")
    WScript.Quit
End If

success = result.Load(response.BodyStr)
If (success <> 1) Then
    outFile.WriteLine("Expected a well-formed JSON response.")
    WScript.Quit
End If

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

outFile.Close