Sign in
agent:

Delete Secrets from Azure Key Vault

There was a problem that the LLM was not able to address. Please rephrase your prompt and try again.

This task permanently deletes specific secrets from an Azure Key Vault. It is crucial for managing the lifecycle of sensitive data, ensuring that outdated or unnecessary secrets are securely and efficiently discarded.

from azure.keyvault.secrets import SecretClient from azure.identity import DefaultAzureCredential from azure.core.exceptions import AzureError, ResourceNotFoundError # Key Vault details #keyVaultName = "vault-eastus-1" # Replace with your Key Vault name KVUri = f"https://{keyVaultName}.vault.azure.net" # List of secrets to delete #secrets_to_delete = ["test-sercet-3", "secret-2","test-secret-1","test-secret-2"] # Replace with the names of the secrets you want to delete secrets_to_delete = unused_secrets_names try: # Authenticate using default credentials credential = DefaultAzureCredential() client = SecretClient(vault_url=KVUri, credential=credential) secrets_found = False print("Deleting specified secrets...") for secret_name in secrets_to_delete: try: # Attempt to delete the secret print(f"Deleting secret: {secret_name}") delete_operation = client.begin_delete_secret(secret_name) delete_operation.wait() # Wait for the deletion to complete print(f"Secret '{secret_name}' deleted successfully.") secrets_found = True except ResourceNotFoundError: print(f"Secret '{secret_name}' not found. Skipping deletion.") except AzureError as e: print(f"Could not delete secret '{secret_name}': {e}") if not secrets_found: print("No secrets found to delete.") except Exception as e: print(f"An error occurred: {e}")
copied