How to Delete a Container from an Azure Storage Account using PowerShell

Introduction 

In this blog, you will see how to delete a container from an Azure Storage Account using PowerShell.

Prerequisites:

Install Azure PowerShell Module to run the script.

PowerShell Script:

Open Notepad and paste the following script. Save the file as script.ps1.

  1. ################# Azure Blob Storage - PowerShell ####################  
  2.   
  3. ## Input Parameters  
  4. $resourceGroupName="azpractice"  
  5. $storageAccName="azstorageacc1122020"  
  6. $containerName="container003"  
  7.   
  8. ## Connect to Azure Account  
  9. Connect-AzAccount   
  10.   
  11. ## Function to delete a container  
  12. Function DeleteStorageContainer  
  13. {  
  14.     Write-Host -ForegroundColor Green "Deleting the storage container.."      
  15.     ## Get the storage account in which container has to be created  
  16.     $storageAcc=Get-AzStorageAccount -ResourceGroupName $resourceGroupName -Name $storageAccName      
  17.     ## Get the storage account context  
  18.     $ctx=$storageAcc.Context  
  19.     ## Delete a container  
  20.     Remove-AzStorageContainer -Container $containerName -Context $ctx -Force   
  21. }  
  22.   
  23. DeleteStorageContainer   
  24.   
  25. ## Disconnect from Azure Account  
  26. Disconnect-AzAccount  
Open a Windows PowerShell window and navigate to the location where the script file was saved.

Run the following command.

.\script.ps1

 

Result:

“container003” deleted successfully.

Reference:

https://docs.microsoft.com/en-us/powershell/module/az.storage/remove-azstoragecontainer?view=azps-3.3.0

Summary:

In this blog, you saw how to delete a container from an Azure Storage Account using PowerShell.