BaragonService/src/main/java/com/hubspot/baragon/service/elb/ApplicationLoadBalancer.java
private void deregisterRemovableTargets(TrafficSource trafficSource,
                                          BaragonGroup baragonGroup,
                                          TargetGroup targetGroup,
                                          Collection<BaragonAgentMetadata> agents,
                                          Collection<TargetDescription> targets) {
    Collection<TargetDescription> removableTargets = listRemovableTargets(trafficSource, baragonGroup, targets, agents);

    for (TargetDescription removableTarget : removableTargets) {
      try {
        if (configuration.isPresent()
            && !configuration.get().isRemoveLastHealthyEnabled()
            && isLastHealthyInstance(removableTarget, targetGroup)) {
          LOG.info("Will not de-register target {} because it is last healthy instance in {}", removableTarget, targetGroup);
        } else {
          elbClient.deregisterTargets(new DeregisterTargetsRequest()
              .withTargetGroupArn(targetGroup.getTargetGroupArn())
              .withTargets(removableTarget));
          LOG.info("De-registered target {} from target group {}", removableTarget, targetGroup);
        }
      } catch (AmazonClientException acexn) {
        LOG.error("Could not de-register target {} from target group {} due to error",
            removableTarget, targetGroup, acexn);
        exceptionNotifier.notify(acexn, ImmutableMap
            .of("targetGroup", targetGroup.getTargetGroupName()));
      }
    }
  }
private void deregisterRemovableTargets(TrafficSource trafficSource,
                                          BaragonGroup baragonGroup,
                                          TargetGroup targetGroup,
                                          Collection<BaragonAgentMetadata> agents,
                                          Collection<TargetDescription> targets) {
    Collection<TargetDescription> removableTargets = listRemovableTargets(trafficSource, baragonGroup, targets, agents);

    for (TargetDescription removableTarget : removableTargets) {
      try {
        if (configuration.isPresent()
            && !configuration.get().isRemoveLastHealthyEnabled()
            && isLastHealthyInstance(removableTarget, targetGroup)) {
          LOG.info("Will not de-register target {} because it is last healthy instance in {}", removableTarget, targetGroup);
        } else {
          elbClient.deregisterTargets(new DeregisterTargetsRequest()
              .withTargetGroupArn(targetGroup.getTargetGroupArn())
              .withTargets(removableTarget));
          LOG.info("De-registered target {} from target group {}", removableTarget, targetGroup);
        }
      } catch (AmazonClientException acexn) {
        LOG.error("Could not de-register target {} from target group {} due to error",
            removableTarget, targetGroup, acexn);
        exceptionNotifier.notify(acexn, ImmutableMap
            .of("targetGroup", targetGroup.getTargetGroupName()));
      }
    }
  }
De-register any targets representing agents that are not known to the BaragonService,
or which otherwise need to be removed.

@param targetGroup TargetGroup to check for old agents
@param agents Known agents, to be used as a reference sheet