{"record":{"id":"c6159ded7196c8a1","repo":"jenkinsci/jenkins","slug":"job-already-exists","errorCode":null,"errorMessage":"Job ' already exists","messagePattern":"Job ' already exists","errorType":"exception","errorClass":"IllegalStateException","httpStatus":null,"severity":"error","filePath":"core/src/main/java/hudson/cli/CopyJobCommand.java","lineNumber":59,"sourceCode":"public class CopyJobCommand extends CLICommand {\n    @Override\n    public String getShortDescription() {\n        return Messages.CopyJobCommand_ShortDescription();\n    }\n\n    @Argument(metaVar = \"SRC\", usage = \"Name of the job to copy\", required = true)\n    public TopLevelItem src;\n\n    @Argument(metaVar = \"DST\", usage = \"Name of the new job to be created.\", index = 1, required = true)\n    @SuppressFBWarnings(value = \"PA_PUBLIC_PRIMITIVE_ATTRIBUTE\", justification = \"Preserve API compatibility\")\n    public String dst;\n\n    @Override\n    protected int run() throws Exception {\n        Jenkins jenkins = Jenkins.get();\n\n        if (jenkins.getItemByFullName(dst) != null) {\n            throw new IllegalStateException(\"Job '\" + dst + \"' already exists\");\n        }\n\n        ModifiableTopLevelItemGroup ig = jenkins;\n        int i = dst.lastIndexOf('/');\n        if (i > 0) {\n            String group = dst.substring(0, i);\n            Item item = jenkins.getItemByFullName(group);\n            if (item == null) {\n                throw new IllegalArgumentException(\"Unknown ItemGroup \" + group);\n            }\n\n            if (item instanceof ModifiableTopLevelItemGroup) {\n                ig = (ModifiableTopLevelItemGroup) item;\n            } else {\n                throw new IllegalStateException(\"Can't create job from CLI in \" + group);\n            }\n            dst = dst.substring(i + 1);\n        }","sourceCodeStart":41,"sourceCodeEnd":77,"githubUrl":"https://github.com/jenkinsci/jenkins/blob/2e228ff40b14dbc8b14ffbc6edf0e4383cf744fc/core/src/main/java/hudson/cli/CopyJobCommand.java#L41-L77","documentation":"`CopyJobCommand.run()` checks `jenkins.getItemByFullName(dst) != null` and throws IllegalStateException 'Job '<dst>' already exists' before performing the copy. The destination full name must be free. The truncated message 'Job ' already exists' is just the prefix before the dst value is interpolated.","triggerScenarios":"Running `java -jar jenkins-cli.jar copy-job <src> <dst>` where an item with full name <dst> already exists in Jenkins (top-level job or any item).","commonSituations":"Re-running a copy command after a prior successful copy; CI script that copies to a fixed name without cleaning up; name collision with a folder.","solutions":["Choose a unique destination name (e.g. append a timestamp or counter).","Delete the existing destination first: `java -jar jenkins-cli.jar delete-job <dst>` then retry the copy.","In a script, check `jenkins.getItemByFullName(dst) == null` before invoking copy-job."],"exampleFix":"// before:\n//   java -jar jenkins-cli.jar copy-job template newjob   // newjob exists\n//   -> IllegalStateException: Job 'newjob' already exists\n//\n// after: pick a fresh name or delete first\n//   java -jar jenkins-cli.jar delete-job newjob\n//   java -jar jenkins-cli.jar copy-job template newjob","handlingStrategy":"validation","validationCode":"// Ensure destination is free before copy-job\nif (jenkins.getItemByFullName(dst) != null) {\n    throw new IllegalStateException(\"Destination already exists: \" + dst\n        + \" — delete it or pick a unique name\");\n}","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Make copy destinations unique (append timestamp/counter) in automation.","Delete or check existing items before copying into a fixed name."],"tags":["jenkins","cli","job","copy","duplicate"],"backgroundTag":null,"analyzedSha":"2e228ff40b14dbc8b14ffbc6edf0e4383cf744fc","analyzedAt":"2026-08-14T07:07:15.274Z","schemaVersion":2},"datasetVersion":"2026-08-14T10:17:34.591Z"}