Manage and restore soft-deleted 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 Microsoft's recommendations for data protection, see Data protection overview.

Manage soft-deleted blobs

Manage soft-deleted blobs with the Azure portal

You can use the Azure portal to view and restore soft-deleted blobs and snapshots.

View deleted blobs

When blobs are soft-deleted, they are invisible in the Azure portal by default. To view soft-deleted blobs, navigate to the Overview page for the container and toggle the Show deleted blobs setting. Soft-deleted blobs are displayed with a status of Deleted.

Screenshot showing how to list soft-deleted blobs in Azure portal

Next, select the deleted blob from the list of blobs to display its properties. Under the Overview tab, notice that the blob's status is set to Deleted. The portal also displays the number of days until the blob is permanently deleted.

Screenshot showing properties of soft-deleted blob in Azure portal

View deleted snapshots

Deleting a blob also deletes any snapshots associated with the blob. If a soft-deleted blob has snapshots, the deleted snapshots can also be displayed in the portal. Display the soft-deleted blob's properties, then navigate to the Snapshots tab, and toggle Show deleted snapshots.

Screenshot showing

Restore soft-deleted objects when versioning is disabled

To restore a soft-deleted blob in the Azure portal when blob versioning is not enabled, first display the blob's properties, then select the Undelete button on the Overview tab. Restoring a blob also restores any snapshots that were deleted during the soft-delete retention period.

Screenshot showing how to restore a soft-deleted blob in Azure portal

To promote a soft-deleted snapshot to the base blob, first make sure that the blob's soft-deleted snapshots have been restored. Select the Undelete button to restore the blob's soft-deleted snapshots, even if the base blob itself has not been soft-deleted. Next, select the snapshot to promote and use the Promote snapshot button to overwrite the base blob with the contents of the snapshot.

Screenshot showing how to promote a snapshot to the base blob

Restore soft-deleted blobs when versioning is enabled

To restore a soft-deleted blob in the Azure portal when versioning is enabled, select the soft-deleted blob to display its properties, then select the Versions tab. Select the version that you want to promote to be the current version, then select Make current version.

Screenshot showing how to promote a version to restore a blob in Azure portal

To restore deleted versions or snapshots when versioning is enabled, display the blob's properties, then select the Undelete button on the Overview tab.

Note

When versioning is enabled, selecting the Undelete button on a deleted blob restores any soft-deleted versions or snapshots, but does not restore the base blob. To restore the base blob, you must promote a previous version.

Manage soft-deleted blobs with code

You can use the Azure Storage client libraries to restore a soft-deleted blob or snapshot. The following examples show how to use the .NET client library.

Restore soft-deleted objects when versioning is disabled

To restore deleted blobs when versioning is not enabled, call the Undelete Blob operation on those blobs. The Undelete Blob operation restores soft-deleted blobs and any deleted snapshots associated with those blobs.

Calling Undelete Blob on a blob that has not been deleted has no effect. The following example calls Undelete Blob on all blobs in a container, and restores the soft-deleted blobs and their snapshots:


foreach (BlobItem blob in container.GetBlobs(BlobTraits.None, BlobStates.Deleted))
{
    await container.GetBlockBlobClient(blob.Name).UndeleteAsync();
}

To restore a specific soft-deleted snapshot, first call the Undelete Blob operation on the base blob, then copy the desired snapshot over the base blob. The following example restores a block blob to the most recently generated snapshot:

// Restore the deleted blob.
await blockBlob.UndeleteAsync();

// List blobs in this container that match prefix.
// Include snapshots in listing.
Pageable<BlobItem> blobItems = container.GetBlobs
                (BlobTraits.None, BlobStates.Snapshots, prefix: blockBlob.Name);

// Get the URI for the most recent snapshot.
BlobUriBuilder blobSnapshotUri = new BlobUriBuilder(blockBlob.Uri)
{
    Snapshot = blobItems
               .OrderByDescending(snapshot => snapshot.Snapshot)
               .ElementAtOrDefault(1)?.Snapshot
};

// Restore the most recent snapshot by copying it to the blob.
blockBlob.StartCopyFromUri(blobSnapshotUri.ToUri());

Restore soft-deleted blobs when versioning is enabled

To restore a soft-deleted blob when versioning is enabled, copy a previous version over the base blob with a Copy Blob or Copy Blob From URL operation.

// List blobs in this container that match prefix.
// Include versions in listing.
Pageable<BlobItem> blobItems = container.GetBlobs
                (BlobTraits.None, BlobStates.Version, prefix: blockBlob.Name);

// Get the URI for the most recent version.
BlobUriBuilder blobVersionUri = new BlobUriBuilder(blockBlob.Uri)
{
    VersionId = blobItems
                .OrderByDescending(version => version.VersionId)
                .ElementAtOrDefault(1)?.VersionId
};

// Restore the most recently generated version by copying it to the base blob.
blockBlob.StartCopyFromUri(blobVersionUri.ToUri());

Manage soft-deleted blobs and directories (hierarchical namespace)

You can restore soft deleted blobs and directories in accounts that have a hierarchical namespace.

Important

Soft delete in accounts that have the hierarchical namespace feature enabled is currently in PREVIEW, and is available globally in all Azure regions. See the Supplemental Terms of Use for Microsoft Azure Previews for legal terms that apply to Azure features that are in beta, preview, or otherwise not yet released into general availability.

To enroll in the preview, see this form.

Manage soft-deleted blobs with the Azure portal

You can use the Azure portal to view and restore soft-deleted blobs and directories.

View deleted blobs and directories

When blobs or directories are soft-deleted, they are invisible in the Azure portal by default. To view soft-deleted blobs and directories, navigate to the Overview page for the container and toggle the Show deleted blobs setting. Soft-deleted blobs and directories are displayed with a status of Deleted. The following image shows a soft-deleted directory.

Screenshot showing how to list soft-deleted blobs in Azure portal (hierarchical namespace enabled accounts)

Note

If you rename a directory that contains soft deleted items (subdirectories and blobs), those soft deleted items become disconnected from the directory, so they won't appear in the Azure portal when you toggle the Show deleted blobs setting. If you want to view them in the Azure portal, you'll have to revert the name of the directory back to it's original name or create a separate directory that uses the original directory name.

Next, select the deleted directory or blob from the list display its properties. Under the Overview tab, notice that the status is set to Deleted. The portal also displays the number of days until the blob is permanently deleted.

Screenshot showing properties of soft-deleted blob in Azure portal (hierarchical namespace enabled accounts)

Restore soft-delete blobs and directories

To restore a soft-deleted blob or directory in the Azure portal, first display the blob or directory's properties, then select the Undelete button on the Overview tab. The following image shows the Undelete button on a soft-deleted directory.

Screenshot showing how to restore a soft-deleted blob in Azure portal (hierarchical namespace enabled accounts)

Restore soft deleted blobs and directories by using PowerShell

Important

This section section applies only to accounts that have a hierarchical namespace.

  1. Ensure that you have the Az.Storage preview module installed. See Enable blob soft delete by using PowerShell.

  2. Obtain storage account authorization by using either a storage account key, a connection string, or Azure Active Directory (Azure AD). 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>'
    
  3. To restore soft deleted item, use the Restore-AzDataLakeGen2DeletedItem command.

    $filesystemName = "my-file-system"
    $dirName="my-directory"
    $deletedItems = Get-AzDataLakeGen2DeletedItem -Context $ctx -FileSystem $filesystemName -Path $dirName
    $deletedItems | Restore-AzDataLakeGen2DeletedItem
    

    If you rename the directory that contains the soft deleted items, those items become disconnected from the directory. If you want to restore those items, you'll have to revert the name of the directory back to it's original name or create a separate directory that uses the original directory name. Otherwise, you'll receive an error when you attempt to restore those soft deleted items.

Restore soft deleted blobs and directories by using Azure CLI

Important

This section section applies only to accounts that have a hierarchical namespace.

  1. Make sure that you have the storage-preview extension installed. See Enable blob soft delete by using PowerShell.

  2. Get a list of deleted items.

    $filesystemName = "my-file-system"
    az storage fs list-deleted-path -f $filesystemName --auth-mode login
    
  3. To restore an item, use the az storage fs undelete-path command.

    $dirName="my-directory"
    az storage fs undelete-path -f $filesystemName --deleted-path-name $dirName —deletion-id "<deletionId>" --auth-mode login
    

    If you rename the directory that contains the soft deleted items, those items become disconnected from the directory. If you want to restore those items, you'll have to revert the name of the directory back to it's original name or create a separate directory that uses the original directory name. Otherwise, you'll receive an error when you attempt to restore those soft deleted items.

Restore soft deleted blobs and directories by using .NET

Important

This section section applies only to accounts that have a hierarchical namespace.

  1. Open a command prompt and change directory (cd) into your project folder For example:

    cd myProject
    
  2. Install the Azure.Storage.Files.DataLake -v 12.7.0 version of the Azure.Storage.Files.DataLake NuGet package by using the dotnet add package command.

    dotnet add package Azure.Storage.Files.DataLake -v -v 12.7.0 -s https://pkgs.dev.azure.com/azure-sdk/public/_packaging/azure-sdk-for-net/nuget/v3/index.json
    
  3. Then, add these using statements to the top of your code file.

    using Azure;
    using Azure.Storage;
    using Azure.Storage.Files.DataLake;
    using Azure.Storage.Files.DataLake.Models;
    using NUnit.Framework;
    using System;
    using System.Collections.Generic;
    using System.Threading.Tasks;
    
  4. The following code deletes a directory, and then restores a soft deleted directory.

    This method assumes that you've created a DataLakeServiceClient instance. To learn how to create a DataLakeServiceClient instance, see Connect to the account.

       public void RestoreDirectory(DataLakeServiceClient serviceClient)
       {
           DataLakeFileSystemClient fileSystemClient = 
              serviceClient.GetFileSystemClient("my-container");
    
           DataLakeDirectoryClient directory = 
               fileSystem.GetDirectoryClient("my-directory");
    
           // Delete the Directory
           await directory.DeleteAsync();
    
           // List Deleted Paths
           List<PathHierarchyDeletedItem> deletedItems = new List<PathHierarchyDeletedItem>();
           await foreach (PathHierarchyDeletedItem deletedItem in fileSystemClient.GetDeletedPathsAsync())
           {
             deletedItems.Add(deletedItem);
           }
    
           Assert.AreEqual(1, deletedItems.Count);
           Assert.AreEqual("my-directory", deletedItems[0].Path.Name);
           Assert.IsTrue(deletedItems[0].IsPath);
    
           // Restore deleted directory.
           Response<DataLakePathClient> restoreResponse = await fileSystemClient.RestorePathAsync(
           deletedItems[0].Path.Name,
           deletedItems[0].Path.DeletionId);
    
       }
    
    

    If you rename the directory that contains the soft deleted items, those items become disconnected from the directory. If you want to restore those items, you'll have to revert the name of the directory back to it's original name or create a separate directory that uses the original directory name. Otherwise, you'll receive an error when you attempt to restore those soft deleted items.

Restore soft deleted blobs and directories by using Java

Important

This section section applies only to accounts that have a hierarchical namespace.

  1. To get started, open the pom.xml file in your text editor. Add the following dependency element to the group of dependencies.

    <dependency>
      <groupId>com.azure</groupId>
      <artifactId>azure-storage-file-datalake</artifactId>
      <version>12.6.0</version>
    </dependency>
    
  2. Then, add these imports statements to your code file.

    Put imports here
    
  3. The following snippet restores a soft deleted file named my-file.

    This method assumes that you've created a DataLakeServiceClient instance. To learn how to create a DataLakeServiceClient instance, see Connect to the account.

    
    public void RestoreFile(DataLakeServiceClient serviceClient){
    
        DataLakeFileSystemClient fileSystemClient = 
            serviceClient.getFileSystemClient("my-container");
    
        DataLakeFileClient fileClient = 
            fileSystemClient.getFileClient("my-file");
    
        String deletionId = null;
    
        for (PathDeletedItem item : fileSystemClient.listDeletedPaths()) {
    
            if (item.getName().equals(fileClient.getFilePath())) {
               deletionId = item.getDeletionId();
            }
        }
    
        fileSystemClient.restorePath(fileClient.getFilePath(), deletionId);
     }
    
    

    If you rename the directory that contains the soft deleted items, those items become disconnected from the directory. If you want to restore those items, you'll have to revert the name of the directory back to it's original name or create a separate directory that uses the original directory name. Otherwise, you'll receive an error when you attempt to restore those soft deleted items.

Restore soft deleted blobs and directories by using Python

Important

This section section applies only to accounts that have a hierarchical namespace.

  1. Install version 12.4.0 or higher of the Azure Data Lake Storage client library for Python by using pip. This command installs the latest version of the Azure Data Lake Storage client library for Python.

    pip install azure-storage-file-datalake
    
  2. Add these import statements to the top of your code file.

    import os, uuid, sys
    from azure.storage.filedatalake import DataLakeServiceClient
    from azure.storage.filedatalake import FileSystemClient
    
  3. The following code deletes a directory, and then restores a soft deleted directory.

    The code example below contains an object named service_client of type DataLakeServiceClient. To see examples of how to create a DataLakeServiceClient instance, see Connect to the account.

    def restoreDirectory():
    
        try:
            global file_system_client
    
            file_system_client = service_client.create_file_system(file_system="my-file-system")
    
            directory_path = 'my-directory'
            directory_client = file_system_client.create_directory(directory_path)
            resp = directory_client.delete_directory()
    
            restored_directory_client = file_system_client.undelete_path(directory_client, resp['deletion_id'])
            props = restored_directory_client.get_directory_properties()
    
            print(props)
    
        except Exception as e:
            print(e)
    
    

    If you rename the directory that contains the soft deleted items, those items become disconnected from the directory. If you want to restore those items, you'll have to revert the name of the directory back to it's original name or create a separate directory that uses the original directory name. Otherwise, you'll receive an error when you attempt to restore those soft deleted items.

Next steps