jenkinsci/jenkins · error · IllegalStateException

Job ' already exists

Error message

Job ' already exists

What it means

`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.

Source

Thrown at core/src/main/java/hudson/cli/CreateJobCommand.java:55

 * @author Kohsuke Kawaguchi
 */
@Extension
public class CreateJobCommand extends CLICommand {
    @Override
    public String getShortDescription() {
        return Messages.CreateJobCommand_ShortDescription();
    }

    @Argument(metaVar = "NAME", usage = "Name of the job to create", required = true)
    @SuppressFBWarnings(value = "PA_PUBLIC_PRIMITIVE_ATTRIBUTE", justification = "Preserve API compatibility")
    public String name;

    @Override
    protected int run() throws Exception {
        Jenkins h = Jenkins.get();

        if (h.getItemByFullName(name) != null) {
            throw new IllegalStateException("Job '" + name + "' already exists");
        }

        ModifiableTopLevelItemGroup ig = h;
        int i = name.lastIndexOf('/');
        if (i > 0) {
            String group = name.substring(0, i);
            Item item = h.getItemByFullName(group);
            if (item == null) {
                throw new IllegalArgumentException("Unknown ItemGroup " + group);
            }

            if (item instanceof ModifiableTopLevelItemGroup) {
                ig = (ModifiableTopLevelItemGroup) item;
            } else {
                throw new IllegalStateException("Can't create job from CLI in " + group);
            }
            name = name.substring(i + 1);
        }

View on GitHub (pinned to 2e228ff40b)

Solutions

  1. Use a unique name for the new job.
  2. Delete the existing job first (`delete-job <name>`) then recreate, or use update-job if you intend to overwrite.
  3. In provisioning scripts, check existence before creating and skip or update accordingly.

Example fix

// before:
//   java -jar jenkins-cli.jar create-job deploy < config.xml  // 'deploy' exists
//   -> IllegalStateException: Job 'deploy' already exists
//
// after: delete first or use update semantics
//   java -jar jenkins-cli.jar delete-job deploy
//   java -jar jenkins-cli.jar create-job deploy < config.xml
//   // or, to overwrite in place, POST config.xml to the existing job instead
Defensive patterns

Strategy: validation

Validate before calling

// Check name is free before create-job
if (jenkins.getItemByFullName(name) != null) {
    throw new IllegalStateException("Job '" + name + "' already exists — delete or rename");
}

Prevention

When it happens

Trigger: Running `java -jar jenkins-cli.jar create-job <name> < config.xml` where an item with full name <name> already exists.

Common situations: Re-running a create script; importing a config whose name collides with an existing job; CI provisioning a fixed job name repeatedly.

Related errors


AI-assisted analysis of jenkinsci/jenkins@2e228ff40b (2026-08-14). Data as JSON: /api/errors/04e18038b9bf5fbc. Report an issue: GitHub.