{"record":{"id":"04e18038b9bf5fbc","repo":"jenkinsci/jenkins","slug":"job-already-exists-04e180","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/CreateJobCommand.java","lineNumber":55,"sourceCode":" * @author Kohsuke Kawaguchi\n */\n@Extension\npublic class CreateJobCommand extends CLICommand {\n    @Override\n    public String getShortDescription() {\n        return Messages.CreateJobCommand_ShortDescription();\n    }\n\n    @Argument(metaVar = \"NAME\", usage = \"Name of the job to create\", required = true)\n    @SuppressFBWarnings(value = \"PA_PUBLIC_PRIMITIVE_ATTRIBUTE\", justification = \"Preserve API compatibility\")\n    public String name;\n\n    @Override\n    protected int run() throws Exception {\n        Jenkins h = Jenkins.get();\n\n        if (h.getItemByFullName(name) != null) {\n            throw new IllegalStateException(\"Job '\" + name + \"' already exists\");\n        }\n\n        ModifiableTopLevelItemGroup ig = h;\n        int i = name.lastIndexOf('/');\n        if (i > 0) {\n            String group = name.substring(0, i);\n            Item item = h.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            name = name.substring(i + 1);\n        }","sourceCodeStart":37,"sourceCodeEnd":73,"githubUrl":"https://github.com/jenkinsci/jenkins/blob/2e228ff40b14dbc8b14ffbc6edf0e4383cf744fc/core/src/main/java/hudson/cli/CreateJobCommand.java#L37-L73","documentation":"`CreateJobCommand.run()` checks `h.getItemByFullName(name) != null` and throws IllegalStateException 'Job '<name>' already exists' before creating. The job name must be unused. Behavior mirrors CopyJobCommand's duplicate check. The truncated prefix 'Job ' already exists' is just the message template before name interpolation.","triggerScenarios":"Running `java -jar jenkins-cli.jar create-job <name> < config.xml` where an item with full name <name> already exists.","commonSituations":"Re-running a create script; importing a config whose name collides with an existing job; CI provisioning a fixed job name repeatedly.","solutions":["Use a unique name for the new job.","Delete the existing job first (`delete-job <name>`) then recreate, or use update-job if you intend to overwrite.","In provisioning scripts, check existence before creating and skip or update accordingly."],"exampleFix":"// before:\n//   java -jar jenkins-cli.jar create-job deploy < config.xml  // 'deploy' exists\n//   -> IllegalStateException: Job 'deploy' already exists\n//\n// after: delete first or use update semantics\n//   java -jar jenkins-cli.jar delete-job deploy\n//   java -jar jenkins-cli.jar create-job deploy < config.xml\n//   // or, to overwrite in place, POST config.xml to the existing job instead","handlingStrategy":"validation","validationCode":"// Check name is free before create-job\nif (jenkins.getItemByFullName(name) != null) {\n    throw new IllegalStateException(\"Job '\" + name + \"' already exists — delete or rename\");\n}","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Make job provisioning idempotent: check existence and update/skip instead of blind create.","Use unique names or delete-before-create in repeatable scripts."],"tags":["jenkins","cli","job","create","duplicate"],"backgroundTag":null,"analyzedSha":"2e228ff40b14dbc8b14ffbc6edf0e4383cf744fc","analyzedAt":"2026-08-14T07:07:15.274Z","schemaVersion":2},"datasetVersion":"2026-08-14T10:17:34.591Z"}