Paul's Blog

A collection of notes and stuff I find interesting

Purging Deleted Azure Key Vaults

2023-02-27 1 min read Code snippets

Do you constantly provision and delete Azure Key Vaults?

If so, you may have noticed attempts to recreate a recently deleted key vault will result in the following error:

The vault name is already in use.

This is because Azure Key Vaults are kept in a deleted state and not automatically purged. You must manually purge these key vaults to be able to reuse the name.

To confirm the key vault in question is in “deleted” state, you can run the following:

az keyvault list-deleted --query "[].name" -o tsv

Using Bash, we can loop through all deleted key vaults and purge them so that the name can be re-used:

az keyvault list-deleted --query "[].name" -o tsv | while read keyvault; do
  az keyvault purge -n $keyvault;