Reading Time: 2 minutes

Canceling a subscription with existing resources can be a bit tricky, but with the right automation tools, the process can be made much easier. Fortunately, Azure CLI provides an easy way to do it using the az rest command. In this blog post, I’ll walk you through how to use the az rest command to cancel subscriptions with existing resources, and compare it to other ways of canceling subscriptions, such as the az account subscription cancel command, Azure PowerShell, and using the Azure portal.

Before we dive in, it’s worth noting that canceling a subscription with existing resources is a serious step, and should only be done with caution. Canceling a subscription will cause all resources associated with that subscription to be deleted. If you have important data or applications running in the subscription, make sure to back them up or migrate them to another subscription before canceling. Microsoft retains the subscription data for a period of 30 – 90 days after the subscription is canceled. During this period, you can still reactivate the subscription if you change your mind.

Now, let’s take a look at how to automate the process of canceling a subscription with existing resources using the az rest command.

First, open a command prompt or terminal window and make sure you have the Azure CLI installed. If you don’t have it installed, you can download it from the Azure CLI website.

Next, create a script that runs the following command, replacing <SubscriptionId> with the ID of the subscription you want to cancel:

az rest --method POST --url "https://management.azure.com/subscriptions/<SubscriptionId>/providers/Microsoft.Subscription/cancel?IgnoreResourceCheck=true&api-version=2021-10-01" 

This command sends a POST request to the Azure management API to cancel the subscription. The IgnoreResourceCheck=true tells the Azure management API to ignore any resources associated with the subscription and delete them as part of the cancellation process.

Run the script to cancel the subscription. You should see a response from the Azure management API indicating that the subscription has been canceled. That’s it! Your subscription and any associated resources have been canceled and deleted.

Now, let’s compare the az rest command to other methods of canceling subscriptions.

  • az account subscription cancel: This command cancels a subscription, but it does not delete any associated resources. It is designed to stop billing for the subscription, rather than deleting the subscription and its resources.
  • Azure PowerShell: PowerShell provides several ways to cancel subscriptions, including the Remove-AzSubscription cmdlet and the Unregister-AzResourceGroupProvider cmdlet. These commands are more flexible than the az rest command, but cannot handle scenarios where resources are present within a subscription.
  • Azure portal: This method allows you to cancel a subscription and any associated resources, but it is not well-suited for automation. It is the easiest option to use manually, but it may not be the best choice if you need to automate the cancellation process.

In summary, the az rest command provides a simple and direct way to cancel subscriptions with associated resources and can be easily automated using scripts and workflows. However, it’s important to use it cautiously and ensure you have backed up or migrated any important data or applications before canceling.

Thanks for reading my blog!

Feel free to drop your comment or question below.