code: def destroyCluster(self):
          
          assert self._ctx
          def expectedShutdownErrors(e):
              return e.status == 400 and 'dependent object' in e.body
  
          instances = self._getNodesInCluster(nodeType=None, both=True)
          spotIDs = self._getSpotRequestIDs()
          if spotIDs:
              self._ctx.ec2.cancel_spot_instance_requests(request_ids=spotIDs)
          instancesToTerminate = awsFilterImpairedNodes(instances, self._ctx.ec2)
          vpcId = None
          if instancesToTerminate:
              vpcId = instancesToTerminate[0].vpc_id
              self._deleteIAMProfiles(instances=instancesToTerminate)
              self._terminateInstances(instances=instancesToTerminate)
          if len(instances) == len(instancesToTerminate):
              logger.debug('Deleting security group...')
              removed = False
              for attempt in retry(timeout=300, predicate=expectedShutdownErrors):
                  with attempt:
                      for sg in self._ctx.ec2.get_all_security_groups():
                          if sg.name == self.clusterName and vpcId and sg.vpc_id == vpcId:
                              try:
                                  self._ctx.ec2.delete_security_group(group_id=sg.id)
                                  removed = True
                              except BotoServerError as e:
                                  if e.error_code == 'InvalidGroup.NotFound':
                                      pass
                                  else:
                                      raise
              if removed:
                  logger.debug('... Succesfully deleted security group')
          else:
              assert len(instances) > len(instancesToTerminate)
              # the security group can't be deleted until all nodes are terminated
              logger.warning('The TOIL_AWS_NODE_DEBUG environment variable is set and some nodes '
                             'have failed health checks. As a result, the security group & IAM '
                             'roles will not be deleted.')
docs: Terminate instances and delete the profile and security group.