{"record":{"id":"c1f038f86d4ad4ae","repo":"hashicorp/packer","slug":"error-destroying-builder-artifact-s-bad-artifac","errorCode":null,"errorMessage":"Error destroying builder artifact: %s; bad artifact: %#v","messagePattern":"Error destroying builder artifact: (.+?); bad artifact: %#v","errorType":"error_code","errorClass":null,"httpStatus":null,"severity":"error","filePath":"packer/build.go","lineNumber":468,"sourceCode":"\t\t\t}\n\n\t\t\tpriorArtifact = artifact\n\t\t}\n\n\t\t// Add on the last artifact to the results\n\t\tif priorArtifact != nil {\n\t\t\tartifacts = append(artifacts, priorArtifact)\n\t\t}\n\t}\n\n\tif keepOriginalArtifact {\n\t\tartifacts = append(artifacts, nil)\n\t\tcopy(artifacts[1:], artifacts)\n\t\tartifacts[0] = builderArtifact\n\t} else {\n\t\tlog.Printf(\"Deleting original artifact for build '%s'\", b.Type)\n\t\tif err := builderArtifact.Destroy(); err != nil {\n\t\t\terrors = append(errors, fmt.Errorf(\"Error destroying builder artifact: %s; bad artifact: %#v\", err, builderArtifact.Files()))\n\t\t}\n\t}\n\n\tif len(errors) > 0 {\n\t\terr = &packersdk.MultiError{Errors: errors}\n\t\treturn artifacts, err\n\t}\n\n\treturn artifacts, nil\n}\n\nfunc (b *CoreBuild) SetDebug(val bool) {\n\tif b.prepareCalled {\n\t\tpanic(\"prepare has already been called\")\n\t}\n\n\tb.debug = val\n}","sourceCodeStart":450,"sourceCodeEnd":486,"githubUrl":"https://github.com/hashicorp/packer/blob/eb36e3c3e48a036f3e8cc94087636ee72e1303c9/packer/build.go#L450-L486","documentation":"At the end of CoreBuild.Run, when on-error is configured to clean up (not keep) the builder's original artifact, builderArtifact.Destroy() is invoked. If that fails, Run records \"Error destroying builder artifact: %s; bad artifact: %#v\", embedding the destroy error and the artifact's Files() list for debugging, and the run returns a packersdk.MultiError containing it.","triggerScenarios":"A build finishes (or is cancelled/aborts) with cleanup enabled and the builder's artifact cannot be destroyed — e.g. EC2 AMI deregistration fails, a docker container/image removal fails, or the remote VM cannot be torn down due to API errors.","commonSituations":"Cloud-side permission errors on delete; resources locked or in a transitional state; artifact already destroyed by a concurrent process; network failures during the destroy API call; builders with incomplete Destroy implementations for partially-created resources.","solutions":["Inspect the destroy error and the listed artifact files to identify the resource that failed cleanup","Manually delete the leftover resource (AMI, image, container) to avoid orphaned/billed resources","Check IAM/cloud permissions for delete/deregister operations and retry the build","Enable PACKER_LOG=1 for the builder's destroy logs; if the builder's cleanup is buggy, report it to the plugin repo"],"exampleFix":"// before (HCL2)\nbuild { on_error = \"cleanup\" }\n// after - keep the artifact on error so failed destroys don't mask the real error\nbuild { on_error = \"ask\" } // or \"abort\"/\"keep\", then clean up manually","handlingStrategy":"try-catch","validationCode":"// Pre-flight: verify the builder's credentials can delete its resources\n// (e.g. ec2:DeregisterImage + ec2:DeleteSnapshot for the amazon-ebs builder).","typeGuard":null,"tryCatchPattern":"artifacts, err := build.Run(ctx, ui)\nif err != nil {\n    var multi *packersdk.MultiError\n    if errors.As(err, &multi) {\n        for _, e := range multi.Errors {\n            if strings.Contains(e.Error(), \"Error destroying builder artifact\") {\n                log.Printf(\"builder artifact cleanup failed — delete manually to avoid billing: %v\", e)\n            }\n        }\n    }\n    return err\n}","preventionTips":["Choose on_error values consciously: cleanup destroys artifacts (and can fail); keep/ask preserve them for manual handling","Ensure builder credentials include delete rights, not just create","Clean up orphaned resources from failed runs with tagged-resource sweeps","Report builder plugins whose Destroy fails on partially-created resources"],"tags":["artifact","destroy","builder","cleanup"],"backgroundTag":"artifact-destroy-failed","analyzedSha":"eb36e3c3e48a036f3e8cc94087636ee72e1303c9","analyzedAt":"2026-09-05T13:20:43.127Z","contentChangedAt":"2026-09-05T13:20:43.127Z","schemaVersion":2},"datasetVersion":"2026-09-12T17:17:11.597Z"}