code: def delete(
      stack_name: str, boto3_session: boto3.session.Session
  ) -> None:
      ec2 = boto3_session.client("ec2")
  
      # Check if we have stack outputs containing ec2 instance and
      # bucket information, if not, just remove the stack.
      try:
          stack_output = get_output(stack_name, boto3_session)
      except NoStackOutputError:
          print(
              f"Stack {stack_name} is in inconsistent state. "
              "This error is ignored as stack is going to be deleted."
          )
          delete_stack(stack_name, boto3_session)
          return
  
      # First, shut down the master so no new agents are started.
      master_id = stack_output[constants.cloudformation.MASTER_ID]
      describe_instance_response = ec2.describe_instances(
          Filters=[{"Name": "instance-id", "Values": [master_id]}],
      )
  
      if describe_instance_response["Reservations"]:
          print("Stopping Master Instance")
          stop_master(master_id, boto3_session, delete=True)
  
      # Second, terminate the agents so nothing can write to the checkpoint bucket. We create agent
      # instances outside of cloudformation, so we have to manually terminate them.
      if stack_uses_spot(stack_name, boto3_session):
          print("Terminating Running Agents and Pending Spot Requests")
          clean_up_spot(stack_name, boto3_session)
          print("Agents and Spot Requests Terminated")
      else:
          print("Terminating Running Agents")
          terminate_running_agents(
              stack_output[constants.cloudformation.AGENT_TAG_NAME],
              boto3_session,
          )
          print("Agents Terminated")
  
      # Third, empty the bucket that was created for this stack.
      bucket_name = get_output(stack_name, boto3_session).get(
          constants.cloudformation.CHECKPOINT_BUCKET
      )
      if bucket_name:
          print("Emptying Checkpoint Bucket")
          empty_bucket(bucket_name, boto3_session)
          print("Checkpoint Bucket Empty")
  
      delete_stack(stack_name, boto3_session)
  
imports: import sys
  import threading
  import time
  from typing import Any, Callable, Dict, List, Optional, Tuple
  import boto3
  import tqdm
  from botocore.exceptions import ClientError, WaiterError
  from determined.deploy.aws import constants
label_desc: Use image node_id 4 for calling a global function with example usage: delete(stack_name, boto3_session) without return types