When experimenting with CDK it's possible to deploy a service which is outside of free tier (since nearly all AWS services are supported) and it's useful to be able to delete an entire CDK stack if necessary.
In this quick lesson we're going to learn how to delete an entire AWS CDK stack with cdk destroy
command.
Instructor: [0:00] At some point, we may decide that we would like to delete our stack entirely. For instance, we would like to migrate it to a different region, maybe we cannot afford our infrastructure at all or maybe we just want to delete the app.
[0:11] Given the amount of resources that we currently have in our stack, it will be tricky to go into CloudFormation and delete all of them one by one. Luckily, there's a better way of solving that problem.
[0:21] In order to destroy the stack entirely, we can open up the terminal and then run cdk destroy. This is going to ask us are we sure that we want to delete our entire stack? Yes, we want to do that.
[0:31] This is absolutely fine because with CDK, we have a single place where this infrastructure is defined, that is our stack. If we want to, we can deploy that again in the future. Deleting a stack can take a while depending on the amount of resources that we have. We can see the status of our stack over here.
[0:48] Currently, the delete is in progress. After a while, we can see the result over here. The todo app stack is completely gone. We can see it over here as well. In the CloudFormation console, if I refresh it, we can no longer see our stack.
[1:00] One thing to notice is that our logo bucket was not deleted because the delete operation was skipped. The reason why it happens is that even though this S3 bucket was created by our stack, the files inside of it could have been uploaded by our users. We don't want to accidentally delete user data, but of course, nothing is stopping us from going into the S3 Console and deleting those buckets as well.
[1:22] Let's assume that we've managed to secure some extra funding and we would like to deploy this up again. We can just run cdk deploy as usual, and this is going to recreate our entire stack in AWS. After a while, we can see that everything has been successfully redeployed to AWS.