For system, network and cloud administrators

How to delete all Azure managed disks that start with a pattern using Azure CLI and Bash shell

In order to be able to delete all Azure managed disks that start with mydisk, for example, you’ll first need to run the Azure Bash shell instead of PowerShell and then type:

az disk list --query "[?starts_with(name,'mydisk')].[name]" | xargs -L1 bash -c 'az disk delete --resource-group yourresourcegroup --name $0 --yes'

Using the –yes parameter will allow the command to say yes every time the deletion of a disk is done.

2 opinions

  1. Ben says:

    Using Github Actions the command does not work:
    Errror:
    xargs: unrecognized option: L

    It works when using it directly within Azure CLI. Any idea?

    BR
    Ben

    1. Didn’t get a chance to test it using Github Actions, Ben. Sorry.

Leave a Reply

Your email address will not be published. Required fields are marked *