title | titleSuffix | description | services | author | ms.service | ms.topic | ms.date | ms.author | ms.devlang | ms.custom |
---|---|---|---|---|---|---|---|---|---|---|
Delete and restore a blob container with Python | Azure Storage | Learn how to delete and restore a blob container in your Azure Storage account using the Python client library. | storage | pauljewellmsft | azure-blob-storage | how-to | 08/05/2024 | pauljewell | python | devx-track-python, devguide-python |
[!INCLUDE storage-dev-guide-selector-delete-container]
This article shows how to delete containers with the Azure Storage client library for Python. If you've enabled container soft delete, you can restore deleted containers.
To learn about deleting a blob container using asynchronous APIs, see Delete a container 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_container.py" id="Snippet_imports":::
The authorization mechanism must have the necessary permissions to delete or restore a container. 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 Container (REST API) and Restore Container (REST API).
[!INCLUDE storage-dev-guide-create-client-python]
To delete a container in Python, use the following method from the BlobServiceClient class:
You can also delete a container using the following method from the ContainerClient class:
After you delete a container, you can't create a container with the same name for at least 30 seconds. Attempting to create a container with the same name will fail with HTTP error code 409 (Conflict)
. Any other operations on the container or the blobs it contains will fail with HTTP error code 404 (Not Found)
.
The following example uses a BlobServiceClient
object to delete the specified container:
:::code language="python" source="~/azure-storage-snippets/blobs/howto/python/blob-devguide-py/blob_devguide_delete_container.py" id="Snippet_delete_container":::
The following example shows how to delete all containers that start with a specified prefix:
:::code language="python" source="~/azure-storage-snippets/blobs/howto/python/blob-devguide-py/blob_devguide_delete_container.py" id="Snippet_delete_container_prefix":::
When container soft delete is enabled for a storage account, a deleted container and its contents may be recovered within a specified retention period. To learn more about container soft delete, see Enable and manage soft delete for containers. You can restore a soft-deleted container by calling the following method of the BlobServiceClient
class:
The following example finds a deleted container, gets the version of that deleted container, and then passes the version into the undelete_container
method to restore the container.
:::code language="python" source="~/azure-storage-snippets/blobs/howto/python/blob-devguide-py/blob_devguide_delete_container.py" id="Snippet_restore_container":::
The Azure Blob Storage client library for Python supports deleting a blob container asynchronously. To learn more about project setup requirements, see Asynchronous programming.
Follow these steps to delete a container using asynchronous APIs:
Add the following import statements:
importasynciofromazure.identity.aioimportDefaultAzureCredentialfromazure.storage.blob.aioimportBlobServiceClient
Add code to run the program using
asyncio.run
. This function runs the passed coroutine,main()
in our example, and manages theasyncio
event loop. Coroutines are declared with the async/await syntax. In this example, themain()
coroutine first creates the top levelBlobServiceClient
usingasync with
, then calls the method that deletes the container. 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_container_async.py" id="Snippet_create_client_async":::
Add code to delete the container. The code is the same as the synchronous example, except that the method is declared with the
async
keyword and theawait
keyword is used when calling thedelete_container
method.:::code language="python" source="~/azure-storage-snippets/blobs/howto/python/blob-devguide-py/blob_devguide_delete_container_async.py" id="Snippet_delete_container":::
With this basic setup in place, you can implement other examples in this article as coroutines using async/await syntax.
To learn more about deleting a container 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 or restoring a container use the following REST API operations:
- Delete Container (REST API)
- Restore Container (REST API)
[!INCLUDE storage-dev-guide-resources-python]
[!INCLUDE storage-dev-guide-next-steps-python]