How to enable soft delete in Azure Storage Services
Hello, today I want to talk to you about soft delete, this feature allows you to recover files or blobs that were previously marked for deletion. When the feature is enabled, you can recover your data after deletion, within a retention period that you must specify when enabling this feature. Once explained this feature, we can begin this tutorial.
Prerequisites
- This tutorial assumes that you already have a Microsoft Azure account configured.
- You can use an existing Storage Account, or you can create a new one. If you want to know how to create a Storage Account using PowerShell, check out this link.
Azure PowerShell Workaround #
If you want to know how to install the PowerShell Azure module on your machine, check out this link. The simplest way to get started is to sign in interactively at the command line.
Connect-AzAccount
This cmdlet will bring up a dialog box prompting you for your email address and password associated with your Azure account. If you have more than one subscription associated with your mail account, you can choose the default subscription. To perform this task, we will use the following commands:
Get-AzSubscription
Select-AzSubscription -Subscription "My Subscription"
Once you set your default subscription, you’re ready to start.
Set the variables #
Here, we define the characteristics of our environment and the resource’s properties.
$resourceGroupName = "RG-DEMO-WE"
$stoAccountName = "stoaccdemowe"
$stoaccount = Get-AzStorageAccount `
-Name $stoAccountName `
-ResourceGroupName $resourceGroupName
Enable soft delete for blobs #
first, you can view the current soft delete retention policy, using the following command.
Get-AzStorageServiceProperty `
-Context $stoaccount.Context `
-ServiceType Blob
To enable soft delete for blobs in your storage account, you should use the Enable-AzStorageDeleteRetentionPolicy cmdlet with the following syntax.
Enable-AzStorageDeleteRetentionPolicy `
-Context $stoaccount.Context `
-RetentionDays 30
You can verify that soft delete has been enabled by using the Get-AzStorageServiceProperty cmdlet.
Get-AzStorageServiceProperty `
-ServiceType Blob `
-Context $stoaccount.Context `
| Select-Object -ExpandProperty DeleteRetentionPolicy
Enable soft delete for file shares #
Note that the following Soft Delete cmdlets are available in version 3.0.0 of the Az.Storage module, please check and update the version before continuing. To view the current soft delete retention policy, use the following command.
Get-AzStorageFileServiceProperty `
-StorageAccountName $stoAccountName `
-ResourceGroupName $resourceGroupName `
| Select-Object ShareDeleteRetentionPolicy `
| Format-Custom
To enable soft delete for all file shares in your storage account, you should use the **Update-AzStorageFileServiceProperty **cmdlet with the following syntax.
Update-AzStorageFileServiceProperty `
-StorageAccountName $stoAccountName `
-ResourceGroupName $resourceGroupName `
-EnableShareDeleteRetentionPolicy $true `
-ShareRetentionDays 30
You can verify that soft delete has been enabled by using the Get-AzStorageFileServiceProperty cmdlet.
Get-AzStorageFileServiceProperty `
-StorageAccountName $stoAccountName `
-ResourceGroupName $resourceGroupName `
| Select-Object ShareDeleteRetentionPolicy `
| Format-Custom
Azure CLI Workaround #
In this case, we will use Azure Cloud Shell, a browser-based shell built into Azure Portal. This allows us to use the Azure command-line tools (Azure CLI and Azure PowerShell) directly from a browser. If you want to know more about Azure Cloud Shell, check out this link. First, we define the characteristics of our environment and store the values in variables.
resourceGroupName="RG-DEMO-WE"
stoAccountName="stoaccdemowe"
Enable soft delete for blobs #
To enable soft delete for blobs in your storage account, you should use the following command.
az storage blob service-properties delete-policy update \
--account-name $stoAccountName \
--enable true \
--days-retained 30
And using the following command you can verify that the parameters have been set correctly.
az storage blob service-properties delete-policy show \
--account-name $stoAccountName
Enable soft delete for file shares #
To enable soft delete for all file shares in your storage account, you should use the following command.
az storage account file-service-properties update \
--account-name $stoAccountName \
--enable-delete-retention $true \
--delete-retention-days 30
You can verify that soft delete has been enabled by using the following command.
az storage account file-service-properties show \
--account-name $stoAccountName
This functionality is currently in preview for containers in the storage account. As soon as it is generally available, I will write a post about this topic.
Thanks for reading my post. I hope you find it useful.
If you want to know more about soft delete in Azure Storage Services, check out this link.