Skip to main content
Jorge Bernhardt Jorge Bernhardt
  1. Posts/

How to enable soft delete in Azure Storage Services

·713 words·4 mins· 100 views · 5 likes ·
Azure CLI Azure Cloud Shell Azure PowerShell Connect-AzAccount

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>

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>

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>

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

 Get-AzStorageServiceProperty
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

soft delete storage

Enable soft delete for file shares>

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

Get-AzStorageFileServiceProperty
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

soft delete storage

Azure CLI Workaround>

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>

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

soft delete storage

Enable soft delete for file shares>

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

soft delete storage

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.