I want to delete my AWS CloudFormation stack, but my stack is stuck in DELETE_FAILED status.
Short description
Your stack might be stuck in DELETE_FAILED status for the following reasons:
- A stack resource has a dependent object or other dependencies that can't be deleted. To resolve this issue, complete the steps in the Delete a stack with a dependent object or other dependencies that can't be deleted section.
- The service role that's used isn't valid or can't be assumed. To resolve this issue, complete the steps in the section Delete a stack with a role that's not valid.
- A custom resource failed to stabilize in the expected amount of time. To resolve this issue, complete the steps in the Delete a stack for a custom resource that failed to stabilize section.
Resolution
Delete a stack with a dependent object or other dependencies that can't be deleted
Dependency issues usually occur when you make an out-of-band change. For example, your stack fails if a security group that's part of your stack is attached to an elastic network interface that's not part of your stack. The stack fails because the security group resource can't be deleted. To delete the stack, you must retain that dependent resource.
To delete a stack while retaining a resource, complete the following steps:
CloudFormation console
To delete a stack that's stuck in DELETE_FAILED status, complete the following steps:
- Open the AWS CloudFormation console.
- Choose the stack that's stuck in DELETE_FAILED status.
- Choose Delete.
Note: A window opens and lists the resources that failed to delete.
- In the window, select all the resources that you want to retain, and then choose Delete stack.
AWS CLI
Note: If you receive errors when you run AWS Command Line Interface (AWS CLI) commands, then see Troubleshoot AWS CLI errors. Also, make sure that you're using the most recent AWS CLI version.
Run the following delete-stack command:
$ aws cloudformation delete-stack --stack-name my-stack --retain-resources myresource1 myresource2
Note: Replace my-stack with the name of your stack. Replace myresource1 and myresource2 with the logical IDs of the resources that failed to delete.
CloudFormation deletes the stack, but doesn't delete any of the resources that you selected to retain. The status of your stack changes to DELETE_COMPLETE.
Important: To avoid unnecessary charges, you must manually delete the resources that you retained during stack deletion.
Delete a stack with a role that's not valid
If you try to delete a stack that used a service role that now doesn't exist, then you receive the following error: "An error occurred (ValidationError) when calling the DeleteStack operation: Role is invalid or cannot be assumed."
To resolve this error, complete the following steps:
- Open the AWS CloudFormation console.
- Choose the stack that you want to delete to see its details.
- In the Stack info tab, note the name of the IAM role assigned to the stack.
- Create an AWS Identity and Access Management (IAM) role that uses the same name as the IAM role in step 3.
Important: When you create your IAM role, verify that it has the right permissions to delete the resources in the stack.
- In the AWS CloudFormation console, re-attempt to delete the stack.
Note: If you don't remember the permissions for a deleted role, you can't create a new role with the same permissions to delete the stack. Contact your administrator to get a new role. Then, run the AWS CLI delete-stack command with the new role to delete the stack:
aws cloudformation delete-stack --stack-name my-stack --role-arn new_role
Note Replace my-stack with your stack name. Replace new_role with your new role's Amazon Resource Name (ARN).
Delete a stack for a custom resource that failed to stabilize
To delete a stack for a custom resource that failed to stabilize in the expected amount of time, see How do I troubleshoot an AWS Lambda backed custom resource that's stuck in DELETE_FAILED status or DELETE_IN_PROGRESS status in AWS CloudFormation?