How to Delete an Azure Storage Account using PowerShell

Introduction 

In this blog, you will see how to delete 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="azstorageacc1132020"  
  6.   
  7. ## Connect to Azure Account  
  8. Connect-AzAccount   
  9.   
  10. ## Function to delete a storage account  
  11.   
  12. Function DeleteStorageAccount  
  13. {  
  14.     Write-Host -ForegroundColor Green "Deleting the storage account.."  
  15.     ## Check if storage account exists  
  16.     if(Get-AzStorageAccount -ResourceGroupName $resourceGroupName -Name $storageAccName -ErrorAction SilentlyContinue)  
  17.     {  
  18.         ## Delete the storage account  
  19.         Remove-AzStorageAccount -ResourceGroupName $resourceGroupName  -Name $storageAccName -Force     
  20.     }  
  21.     else  
  22.     {  
  23.          Write-Host -ForegroundColor Magenta $storageAccName "- storage account does not exist."           
  24.     }      
  25. }   
  26.   
  27. DeleteStorageAccount   
  28.   
  29. ## Disconnect from Azure Account  
  30. 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:

The storage account was deleted successfully.

Reference:

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

Summary:

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