Enable soft delete for blobs

Blob soft delete protects an individual blob and its versions, snapshots, and metadata from accidental deletes or overwrites by maintaining the deleted data in the system for a specified period of time. During the retention period, you can restore the blob to its state at deletion. After the retention period has expired, the blob is permanently deleted. For more information about blob soft delete, see Soft delete for blobs.

Blob soft delete is part of a comprehensive data protection strategy for blob data. To learn more about Azure's recommendations for data protection, see Data protection overview.

Enable blob soft delete

You can enable or disable soft delete for a storage account at any time by using the Azure portal, PowerShell, or Azure CLI.

Blob soft delete is not enabled when you create a new storage account with PowerShell. You can enable blob soft delete after the new account has been created.

To enable blob soft delete for an existing storage account with PowerShell, call the Enable-AzStorageBlobDeleteRetentionPolicy command, specifying the retention period in days.

The following example enables blob soft delete and sets the retention period to seven days. Remember to replace the placeholder values in brackets with your own values:

Enable-AzStorageBlobDeleteRetentionPolicy -ResourceGroupName <resource-group> `
    -StorageAccountName <storage-account> `
    -RetentionDays 7

To check the current settings for blob soft delete, call the Get-AzStorageBlobServiceProperty command:

$properties = Get-AzStorageBlobServiceProperty -ResourceGroupName <resource-group> `
    -StorageAccountName <storage-account>
$properties.DeleteRetentionPolicy.Enabled
$properties.DeleteRetentionPolicy.Days

Enable blob soft delete (hierarchical namespace)

Blob soft delete can also protect blobs and directories in accounts that have the hierarchical namespace feature enabled on them.

  1. Install the latest PowershellGet module. Then, close and reopen the PowerShell console.

    Install-Module PowerShellGet -Repository PSGallery -Force
    
  2. Install Az.Storage preview module.

    Install-Module Az.Storage -Repository PsGallery -RequiredVersion 3.7.1-preview -AllowClobber -AllowPrerelease -Force
    

    For more information about how to install PowerShell modules, see Install the Azure PowerShell module

  3. Obtain storage account authorization by using either a storage account key, a connection string, or Microsoft Entra ID. For more information, see Connect to the account.

    The following example obtains authorization by using a storage account key.

    $ctx = New-AzStorageContext -StorageAccountName '<storage-account-name>' -StorageAccountKey '<storage-account-key>'
    
  4. To enable blob soft delete with PowerShell, use the Enable-AzStorageDeleteRetentionPolicy command, and specify the retention period in days.

    The following example enables soft delete for an account, and sets the retention period to 4 days.

    Enable-AzStorageDeleteRetentionPolicy -RetentionDays 4  -Context $ctx
    
  5. To check the current settings for blob soft delete, use the Get-AzStorageServiceProperty command:

     Get-AzStorageServiceProperty -ServiceType Blob -Context $ctx
    

Next steps