This article shows how to delete containers with the Azure Storage client library for JavaScript. If you've enabled container soft delete, you can restore deleted containers.
PrerequisitesTo delete a container, 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 fails with HTTP error code 409 (Conflict)
. Any other operations on the container or the blobs it contains fail with HTTP error code 404 (Not Found)
.
The following example uses a BlobServiceClient
object to delete the specified container:
async function deleteContainer(blobServiceClient, containerName) {
return await blobServiceClient.deleteContainer(containerName);
}
async function deleteContainer(
blobServiceClient: BlobServiceClient,
containerName: string
): Promise<ContainerDeleteResponse> {
return await blobServiceClient.deleteContainer(containerName);
}
The following example shows how to delete all containers that start with a specified prefix:
async function deleteContainersWithPrefix(blobServiceClient, prefix) {
const containerOptions = {
includeDeleted: false,
includeMetadata: false,
includeSystem: true,
prefix
}
for await (const containerItem of blobServiceClient.listContainers(containerOptions)) {
try{
const containerClient = blobServiceClient.getContainerClient(containerItem.name);
await containerClient.delete();
console.log(`Deleted ${containerItem.name} container - success`);
}catch(ex){
console.log(`Deleted ${containerItem.name} container - failed - ${ex.message}`);
}
}
}
async function deleteContainersWithPrefix(
blobServiceClient: BlobServiceClient,
prefix: string
): Promise<void> {
const containerOptions: ServiceListContainersOptions = {
includeDeleted: false,
includeMetadata: false,
includeSystem: true,
prefix
};
for await (const containerItem of blobServiceClient.listContainers(
containerOptions
)) {
try {
const containerClient: ContainerClient =
blobServiceClient.getContainerClient(containerItem.name);
const containerDeleteMethodOptions: ContainerDeleteMethodOptions = {};
await containerClient.delete(containerDeleteMethodOptions);
console.log(`Deleted ${containerItem.name} container - success`);
} catch (err: unknown) {
if (err instanceof Error) {
console.log(
`Deleted ${containerItem.name} container - failed - ${err.message}`
);
}
}
}
}
Restore a deleted container
When container soft delete is enabled for a storage account, a container and its contents can be recovered after it has been deleted, within a retention period that you specify. You can restore a soft-deleted container using a BlobServiceClient object:
The following example finds a deleted container, gets the version ID of that deleted container, and then passes that ID into the undeleteContainer
method to restore the container.
async function undeleteContainer(blobServiceClient, containerName) {
// Version to restore
let containerVersion;
const containerOptions = {
includeDeleted: true,
prefix: containerName
}
// Find the deleted container and restore it
for await (const containerItem of blobServiceClient.listContainers(containerOptions)) {
if (containerItem.name === containerName) {
containerVersion = containerItem.version;
}
}
const containerClient = await blobServiceClient.undeleteContainer(
containerName,
containerVersion,
);
}
async function undeleteContainer(
blobServiceClient: BlobServiceClient,
containerName: string
): Promise<void> {
// version to undelete
let containerVersion: string | undefined;
const containerOptions: ServiceListContainersOptions = {
includeDeleted: true,
prefix: containerName
};
// Find the deleted container and restore it
for await (const containerItem of blobServiceClient.listContainers(
containerOptions
)) {
if (containerItem.name === containerName) {
containerVersion = containerItem.version as string;
}
}
if (containerVersion !== undefined) {
const serviceUndeleteContainerOptions: ServiceUndeleteContainerOptions = {};
const {
containerClient,
containerUndeleteResponse
}: {
containerClient: ContainerClient;
containerUndeleteResponse: ContainerUndeleteResponse;
} = await blobServiceClient.undeleteContainer(
containerName,
containerVersion,
serviceUndeleteContainerOptions
);
}
}
Resources
To learn more about deleting a container using the Azure Blob Storage client library for JavaScript, see the following resources.
Code samplesThe Azure SDK for JavaScript contains libraries that build on top of the Azure REST API, allowing you to interact with REST API operations through familiar JavaScript paradigms. The client library methods for deleting or restoring a container use the following REST API operations:
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4