in samcli/commands/delete/delete_context.py [0:0]
def delete(self):
"""
Delete method calls for Cloudformation stacks and S3 and ECR artifacts
"""
# Fetch the template using the stack-name
cf_template = self.cf_utils.get_stack_template(self.stack_name, TEMPLATE_STAGE)
# Get the cloudformation template name using template_str
self.cf_template_file_name = get_uploaded_s3_object_name(file_content=cf_template, extension="template")
template = Template(
template_path=None,
parent_dir=None,
uploaders=self.uploaders,
code_signer=None,
template_str=cf_template,
)
# If s3 info is not available, try to obtain it from CF
# template resources.
if not self.s3_bucket:
s3_info = template.get_s3_info()
self.s3_bucket = s3_info["s3_bucket"]
self.s3_uploader.bucket_name = self.s3_bucket
self.s3_prefix = s3_info["s3_prefix"]
self.s3_uploader.prefix = self.s3_prefix
self.s3_prompts()
retain_resources = self.ecr_repos_prompts(template)
# ECR companion stack delete prompts, if it exists
companion_stack = CompanionStack(self.stack_name)
ecr_companion_stack_exists = self.cf_utils.can_delete_stack(stack_name=companion_stack.stack_name)
if ecr_companion_stack_exists:
LOG.debug("ECR Companion stack found for the input stack")
self.companion_stack_name = companion_stack.stack_name
self.delete_ecr_companion_stack()
# Delete the artifacts and retain resources user selected not to delete
template.delete(retain_resources=retain_resources)
# Delete the CF template file in S3
if self.delete_cf_template_file:
self.s3_uploader.delete_artifact(remote_path=self.cf_template_file_name)
# Delete the folder of artifacts if s3_bucket and s3_prefix provided
elif self.delete_artifacts_folder:
self.s3_uploader.delete_prefix_artifacts()
# Delete the primary input stack
try:
click.echo(f"\t- Deleting Cloudformation stack {self.stack_name}")
self.cf_utils.delete_stack(stack_name=self.stack_name)
self.cf_utils.wait_for_delete(self.stack_name)
LOG.debug("Deleted Cloudformation stack: %s", self.stack_name)
except CfDeleteFailedStatusError:
LOG.debug("delete_stack resulted failed and so re-try with retain_resources")
self.cf_utils.delete_stack(stack_name=self.stack_name, retain_resources=retain_resources)
self.cf_utils.wait_for_delete(self.stack_name)
# Warn the user that s3 information is missing and to use --s3 options
if not self.s3_bucket:
LOG.debug("Cannot delete s3 objects as bucket is missing")
click.secho(
"\nWarning: Cannot resolve s3 bucket information from command options"
" , local config file or cloudformation template. Please use"
" --s3-bucket next time and"
" delete s3 files manually if required.",
fg="yellow",
)