How can I use the ‘CDK destroy’ command to permanently delete the Resources and clean up the environment?

338    Asked by CsabaToth in AWS , Asked on Mar 29, 2024

 I have developed a cloud infrastructure by using the AWS CDK for deployment of various resources such as EC2 Instances, S3 buckets, and lambda functions. In this particular task, I need to clean up and remove all the resources created by the CDKBstack. How can I use the ‘CDK destroy’ command to permanently delete the Resources and clean up the environment? 

Answered by Deepak Mistry

In the context of AWS, here is the example given of how you can use the “cdk destroy” command for permanently cleaning up and removing the resources created by a CDK stack:-




Navigate to the CDK project directory

Firstly, you would need to open the terminal or Command prompt and navigate to the directory where your VDK project is located.

Run the CDK destroy command

You can use the “cdk destroy” command followed by the name of your CDK stack for the purpose of initiating the resource deletion process.

Confirm the destruction

After running the “cdk destroy” command, you can confirm the destruction of the Resources. You can type “y” or “yes” to confirm and proceed with the deletion.

Verify cleanup completion

Once the task of CDK has finished, it would display a message indicating that the stack has been destroyed successfully.

Here is an example given of what the command flow would look like in your terminal:-

$ cdk destroy MyCDKStack
Are you sure you want to delete: MyCDKStack (y/n)? y
MyCDKStack: destroying…
15:40:07 | DELETE_IN_PROGRESS | AWS::S3::Bucket | MyBucket
15:40:08 | DELETE_COMPLETE | AWS::S3::Bucket | MyBucket
15:40:09 | DELETE_IN_PROGRESS | AWS::EC2::Instance| MyInstance
15:40:10 | DELETE_COMPLETE | AWS::EC2::Instance| MyInstance

Stack destroyed successfully


Your Answer

Interviews

Parent Categories