AWS

How to use the “delete-stack” command in AWS CLI?

CloudFromation is a web service that is used for managing and auditing resources. The user can manage the AWS resources by creating templates in JSON or YAML formats. This file contains the necessary configurations for the AWS resources. In CloudFormation, there are multiple stacks within a template.

Quick Outline

In this article, we will learn about:

A stack contains information about the resources required for the deployment of applications. These stacks can be managed via the Console or AWS CLI. One of the administrative actions for managing these tasks via the AWS CLI is deleting a stack. AWS CLI supports the “delete-stack” command of CloudFormation.

Read more: How to Get Started With AWS CloudFormation?

What is the “delete-stack” Command in AWS CLI?

The “delete-stack” command in AWS CLI is used for removing a specified stack from the CloudFormation’s dashboard. Upon the successful execution of this command, no output is returned. However, the user can verify the deletion of the stack by visiting the CloudFormation dashboard.

How to use the “delete-stack” Command in AWS CLI?

The “delete-stack” command is simple to use and easy to understand. The user can specify the name and the region of the stack to be deleted by using the different parameters.

Syntax

aws cloudformation delete-stack --stack-name <Options>

Option

Various options are supported by the “delete-stack” command. The following is a brief description of the options:

Options Description
–stack-name This parameter inputs the name or the unique ID of the stack.
–retain-resources This parameter is for the stack that is in the “DELETE_FAILED” state. It accepts a list of the names of the resources that the user wishes to retain while the CloudFormation deletes the stack.
–role-arn The CloudFormation also deletes a stack by the IAM role ARN.

The –role-arn parameter is used for this purpose.

–client-request-token This parameter is used to re-attempt to delete a stack from the CloudFormation dashboard.
–cli-input-json This parameter is used to provide multiple JSON instructions to the AWS service at once. The template used by this parameter is generated by “–generate-cli-token”.
–generate-cli-skeleton The –generate-cli-token is used to create a template or skeleton for providing multiple JSON instructions to the service.

There are also Global options available to use with this command. Such options can be used with multiple commands and hence, named as Global Options. The AWS documentation discusses these options in detail.

Examples

In this section of the article, we will explore different methods of using the “delete-stack” command in AWS CLI:

Example 1: How to Delete a Stack using AWS CLI?

To delete a stack, first create a stack by visiting the “CloudFormation” service from the AWS Management Console. To learn more about creating a stack, refer to this article: “ How to Get Started With AWS CloudFormation?”.

For this demo, we have already created a stack named “stack01”:

The command to delete a stack using AWS CLI is given as follows:

aws cloudformation delete-stack --stack-name <stackname>

Note: Replace the <stackname> with the name of your stack in AWS CloudFormation.

Output

To verify if the stack has been successfully deleted or not, visit the Stacks dashboard in AWS CloudFormation. Here, the stack has been deleted successfully:

Example 2: How to Delete a Stack in a Specific Region using AWS CLI?

Oftentimes, the applications and stacks are running in regions other than default AWS regions. The “delete-stack” command also helps the user delete the stack in the different regions by specifying the AWS region name in the “–region” parameter. The command is given as follows:

aws cloud formation delete-stack --stack-name stack03 --region ap-southeast-1

Note: Replace the “stack03” with the name of your CloudFormation stack’s name. Similarly, replace the “ap-southeast-1” with the AWS region name.

Output

Example 3: How to Delete a Stack using a –profile in AWS CLI?

In AWS, a user can also use the –profile parameter to delete a stack in CloudFormation. For this purpose, create an IAM user and then configure the user in AWS CLI using the –profile parameter. It is worth noting here that the IAM user created must be equipped with the

“AmazonCloudFormationFullAccess” permission for deleting the stack.

For this demo, we have already created and configured the –profile user:

Provide the following command to the terminal for deleting the stack using the –profile user:

aws cloudformation delete-stack --stack-name <stackname> --region <AWSRegionName>

Note: Replace the <stackname> with your CloudFormation stack’s name. Similarly, in the <AWSRegionName>, provide the name of your AWS region in which the stack is created

Output

That is all from this guide.

Conclusion

The delete-stack command removes a specific stack in a specific region from CloudFormation’s dashboard. This command does not generate any output upon successful execution. For AWS users, this command can help manage the cost and monitor the services by deleting the resources that sit idle. This article is a step-by-step guide for implementing the delete-stack command in AWS CLI.

About the author

Shameen Shahid

I am a self-motivated technical content writer. I hold a bachelor’s degree in computer science an have expertise in AWS and want to share my knowledge with the world.