| title | Delete and restore a blob with Python |
|---|---|
| titleSuffix | Azure Storage |
| description | Learn how to delete and restore a blob in your Azure Storage account using the Python client library |
| services | storage |
| author | stevenmatthew |
| ms.author | shaas |
| ms.date | 08/12/2024 |
| ms.service | azure-blob-storage |
| ms.topic | how-to |
| ms.devlang | python |
| ms.custom | devx-track-python, devguide-python |
[!INCLUDE storage-dev-guide-selector-delete-blob]
This article shows how to delete blobs using the Azure Storage client library for Python, and how to restore soft-deleted blobs during the retention period.
To learn about deleting a blob using asynchronous APIs, see Delete a blob asynchronously.
[!INCLUDE storage-dev-guide-prereqs-python]
[!INCLUDE storage-dev-guide-project-setup-python]
Add the following import statements:
:::code language="python" source="~/azure-storage-snippets/blobs/howto/python/blob-devguide-py/blob_devguide_delete_blobs.py" id="Snippet_imports":::
The authorization mechanism must have the necessary permissions to delete a blob, or to restore a soft-deleted blob. For authorization with Microsoft Entra ID (recommended), you need Azure RBAC built-in role Storage Blob Data Contributor or higher. To learn more, see the authorization guidance for Delete Blob (REST API) and Undelete Blob (REST API).
[!INCLUDE storage-dev-guide-create-client-python]
[!INCLUDE storage-dev-guide-delete-blob-note]
To delete a blob, call the following method:
The following example deletes a blob:
:::code language="python" source="~/azure-storage-snippets/blobs/howto/python/blob-devguide-py/blob_devguide_delete_blobs.py" id="Snippet_delete_blob":::
If the blob has any associated snapshots, you must delete all of its snapshots to delete the blob. The following example deletes a blob and its snapshots:
:::code language="python" source="~/azure-storage-snippets/blobs/howto/python/blob-devguide-py/blob_devguide_delete_blobs.py" id="Snippet_delete_blob_snapshots":::
To delete only the snapshots and not the blob itself, you can pass the parameter delete_snapshots="only".
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.
You can use the Azure Storage client libraries to restore a soft-deleted blob or snapshot.
How you restore a soft-deleted blob depends on whether or not your storage account has blob versioning enabled. For more information on blob versioning, see Blob versioning. See one of the following sections, depending on your scenario:
To restore deleted blobs when versioning is disabled, call the following method:
This method restores the content and metadata of a soft-deleted blob and any associated soft-deleted snapshots. Calling this method for a blob that hasn't been deleted has no effect.
:::code language="python" source="~/azure-storage-snippets/blobs/howto/python/blob-devguide-py/blob_devguide_delete_blobs.py" id="Snippet_restore_blob":::
If a storage account is configured to enable blob versioning, deleting a blob causes the current version of the blob to become the previous version. To restore a soft-deleted blob when versioning is enabled, copy a previous version over the base blob. You can use the following method:
The following code example gets the latest version of a deleted blob, and restores the latest version by copying it to the base blob:
:::code language="python" source="~/azure-storage-snippets/blobs/howto/python/blob-devguide-py/blob_devguide_delete_blobs.py" id="Snippet_restore_blob_version":::
The Azure Blob Storage client library for Python supports deleting a blob asynchronously. To learn more about project setup requirements, see Asynchronous programming.
Follow these steps to delete a blob using asynchronous APIs:
-
Add the following import statements:
import asyncio from azure.identity.aio import DefaultAzureCredential from azure.storage.blob.aio import BlobServiceClient
-
Add code to run the program using
asyncio.run. This function runs the passed coroutine,main()in our example, and manages theasyncioevent loop. Coroutines are declared with the async/await syntax. In this example, themain()coroutine first creates the top levelBlobServiceClientusingasync with, then calls the method that deletes the blob. Note that only the top level client needs to useasync with, as other clients created from it share the same connection pool.:::code language="python" source="~/azure-storage-snippets/blobs/howto/python/blob-devguide-py/blob_devguide_delete_blobs_async.py" id="Snippet_create_client_async":::
-
Add code to delete the blob. The code is the same as the synchronous example, except that the method is declared with the
asynckeyword and theawaitkeyword is used when calling thedelete_blobmethod.:::code language="python" source="~/azure-storage-snippets/blobs/howto/python/blob-devguide-py/blob_devguide_delete_blobs_async.py" id="Snippet_delete_blob":::
With this basic setup in place, you can implement other examples in this article as coroutines using async/await syntax.
To learn more about how to delete blobs and restore soft-deleted blobs using the Azure Blob Storage client library for Python, see the following resources.
- View synchronous or asynchronous code samples from this article (GitHub)
The Azure SDK for Python contains libraries that build on top of the Azure REST API, allowing you to interact with REST API operations through familiar Python paradigms. The client library methods for deleting blobs and restoring deleted blobs use the following REST API operations:
- Delete Blob (REST API)
- Undelete Blob (REST API)
[!INCLUDE storage-dev-guide-resources-python]
[!INCLUDE storage-dev-guide-next-steps-python]