{"record":{"id":"869e2876f5c98ace","repo":"floci-io/floci","slug":"resourceinuse","errorCode":"ResourceInUse","errorMessage":"Auto Scaling group '{name}' has {count} instance(s). Set ForceDelete=true to delete anyway.","messagePattern":"Auto Scaling group '(.+?)' has (.+?) instance\\(s\\)\\. Set ForceDelete=true to delete anyway\\.","errorType":"exception","errorClass":"AwsException","httpStatus":400,"severity":"error","filePath":"src/main/java/io/github/hectorvent/floci/services/autoscaling/AutoScalingService.java","lineNumber":282,"sourceCode":"        if (minSize != null) { asg.setMinSize(minSize); }\n        if (maxSize != null) { asg.setMaxSize(maxSize); }\n        if (desiredCapacity != null) { asg.setDesiredCapacity(desiredCapacity); }\n        if (defaultCooldown != null) { asg.setDefaultCooldown(defaultCooldown); }\n        if (availabilityZones != null) { asg.setAvailabilityZones(new ArrayList<>(availabilityZones)); }\n        if (subnetIds != null) { asg.setSubnetIds(new ArrayList<>(subnetIds)); }\n        if (healthCheckType != null) { asg.setHealthCheckType(healthCheckType); }\n        if (healthCheckGracePeriod != null) { asg.setHealthCheckGracePeriod(healthCheckGracePeriod); }\n        if (terminationPolicies != null) { asg.setTerminationPolicies(new ArrayList<>(terminationPolicies)); }\n        groups.put(asgKey(region, name), asg);\n    }\n\n    public void deleteAutoScalingGroup(String region, String name, boolean forceDelete) {\n        AutoScalingGroup asg = requireGroup(region, name);\n        List<AsgInstance> active = asg.getInstances().stream()\n                .filter(i -> !\"Terminated\".equals(i.getLifecycleState()))\n                .collect(Collectors.toList());\n        if (!active.isEmpty() && !forceDelete) {\n            throw new AwsException(\"ResourceInUse\",\n                    \"Auto Scaling group '\" + name + \"' has \" + active.size()\n                            + \" instance(s). Set ForceDelete=true to delete anyway.\", 400);\n        }\n        if (forceDelete && ec2Service != null && !active.isEmpty()) {\n            active.stream()\n                    .map(AsgInstance::getInstanceId)\n                    .filter(Objects::nonNull)\n                    .forEach(instanceId -> {\n                        try {\n                            ec2Service.terminateInstances(region, List.of(instanceId));\n                        }\n                        catch (AwsException ignored) {\n                            // ForceDelete should remove stale ASG membership even if EC2 no longer has the instance.\n                        }\n                    });\n        }\n        groups.remove(asgKey(region, name));\n        // clean up associated hooks and policies","sourceCodeStart":264,"sourceCodeEnd":300,"githubUrl":"https://github.com/floci-io/floci/blob/62ff490619e7bd3554597c28c704081b4c15add5/src/main/java/io/github/hectorvent/floci/services/autoscaling/AutoScalingService.java#L264-L300","documentation":"Thrown by deleteAutoScalingGroup when the group still has instances not in the Terminated lifecycle state and ForceDelete is not set. Amazon EC2 Auto Scaling refuses to delete non-empty groups to prevent orphaned instances; ForceDelete=true terminates the members first. Floci reproduces this as ResourceInUse (HTTP 400), matching AWS.","triggerScenarios":"Calling DeleteAutoScalingGroup with the group's AutoScalingGroupName while desiredCapacity > 0 or any instance is InService/Pending/Standby, without setting ForceDelete=true.","commonSituations":"Tearing down test stacks without scaling the group to zero first; cleanup scripts that assume delete is unconditional; instances mid-launch from a recent scale-out; standby instances that never terminated.","solutions":["Set desired capacity to 0 (UpdateAutoScalingGroup with DesiredCapacity=0), wait for instances to reach Terminated, then delete","Or set ForceDelete=true on the DeleteAutoScalingGroup call, which terminates instances and deletes the group","For graceful teardown: detach instances with ShouldDecrementDesiredCapacity=true, wait, then delete"],"exampleFix":"// before\nDeleteAutoScalingGroupRequest req = DeleteAutoScalingGroupRequest.builder()\n    .autoScalingGroupName(asgName)\n    .build();\n\n// after\nDeleteAutoScalingGroupRequest req = DeleteAutoScalingGroupRequest.builder()\n    .autoScalingGroupName(asgName)\n    .forceDelete(true)\n    .build();","handlingStrategy":"validation","validationCode":"Group g = autoscaling.describeAutoScalingGroups(DescribeAutoScalingGroupsRequest.builder()\n        .autoScalingGroupNames(asgName).build())\n    .autoScalingGroups().get(0);\nlong active = g.instances().stream()\n    .filter(i -> !\"Terminated\".equals(i.lifecycleState())).count();\nif (active > 0) {\n    // scale in first, or set forceDelete(true) on the delete request\n}","typeGuard":null,"tryCatchPattern":"catch (ResourceInUseException e) { log.warn(\"ASG {} not empty\", asgName); retryWithForceDelete or scaleInFirst(); }","preventionTips":["Scale groups to DesiredCapacity=0 before deletion in teardown scripts","Treat ResourceInUse as a retryable-with-action signal, not a hard failure"],"tags":["autoscaling","delete","resource-in-use","aws-emulator"],"backgroundTag":null,"analyzedSha":"62ff490619e7bd3554597c28c704081b4c15add5","analyzedAt":"2026-08-14T14:25:23.764Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}