How to enable soft delete for Azure Storage blobs using PowerShell?

PowerShellMicrosoft TechnologiesSoftware Testing

To enable soft delete for Azure Storage blobs in Storage account properties using PowerShell, we can use the Enable-AzureStorageDeleteRetentionPolicy command. From the Azure portal, we need to access the Data Protection property of the Azure Storage account.

You can also provide the retention days to keep the soft-deleted data. To perform the same with the Azure PowerShell we need to use the Enable-AzureStorageDeleteRetentionPolicy command. Before running this command you need to make sure that you are connected to the Azure Cloud account (if not then use Connect-AzAccount) and the proper azure subscription (if not then use Set-AzContext) command.

To use the Enable-AzureStorageDeleteRetentionPolicy you first need to set the Azure Storage context to work with the storage account. You can set the context using the below command.

$rg = "az204"
$storageaccount = "az204storage05june"
$key = (Get-AzStorageAccountKey -ResourceGroupName $rg - Name $storageaccount)[0].Value
$context = New-AzStorageContext -StorageAccountName $storageaccount - StorageAccountKey $key

In the above code, we are using resource group “az204” and the storage account name “az204storage05june” to get the azure storage context. Once we have the context available then we can use the below command to enable soft delete with 10 days of the retention period.

Enable-AzStorageDeleteRetentionPolicy -RetentionDays 10 -Context $context -


Enabled RetentionDays
------- -------------
True       10

Same you can check from the Azure Portal,

Published on 01-Sep-2021 07:54:20