This is the full example for Emptying an Azure Container with PowerShell.

Read the full post about all the Azure features here.

## Get Parameters
Param(
    [string] $ResourceGroupName,
    [string] $StorageAccountName,
    [string] $StorageContainerNames
)

## Static Parameters
#Login-AzureRmAccount
#Set-AzureRmContext -SubscriptionID 11111111-1111-1111-1111-111111111111
#$ResourceGroupName = "GroupName"
#$StorageAccountName = "AccountName"
#$StorageContainerNames = "ContainerName1, ContainerName2, ContainerName3"

try{

    ## Get Storage Details
    Write-Output ("Get Storage Account $StorageAccountName Keys")
    $Keys = Get-AzureRmStorageAccountKey -ResourceGroupName $ResourceGroupName -Name $StorageAccountName;

    Write-Output ("Get Storage Account $StorageAccountName Context")
    $StorageContext = New-AzureStorageContext -StorageAccountName $StorageAccountName -StorageAccountKey $Keys[0].Value;

    ## Proccess Each Container
    $StorageContainerNames.Split(",") | ForEach {       

        $currentContainer = $_

        Write-Output ("Start Remove for Container $currentContainer")

        ## Remove Containers Blobs
        if ((Get-AzureStorageContainer -Context $StorageContext | Where-Object { $_.Name -eq $currentContainer })){    

            Write-Output ("Get all $currentContainer Blobs")
            $blobs = Get-AzureStorageBlob -Container $currentContainer -Context $StorageContext           

            ## Remove all Blobs
            Write-Output ("Remove all $currentContainer Blobs")
            if ($blobs -ne $null) {    

                foreach ($blob in $blobs){

                    Write-Output ("Removing Blob: {0}" -f $blob.Name)
                    Remove-AzureStorageBlob -Blob $blob.Name -Container $currentContainer -Context $StorageContext

                }
            }
            else {

                Write-Warning "Container $currentContainer was already empty"

            }
        }
        else {

            Write-Warning "Container $currentContainer doesn't exists."

        }

    }

}catch {

    Write-Error "$_.Exception.Message"

}

Advertisements