jenkinsci/jenkins · error · IllegalStateException

Job ' already exists

Error message

Job ' already exists

What it means

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

Source

Thrown at core/src/main/java/hudson/cli/CopyJobCommand.java:59

public class CopyJobCommand extends CLICommand {
    @Override
    public String getShortDescription() {
        return Messages.CopyJobCommand_ShortDescription();
    }

    @Argument(metaVar = "SRC", usage = "Name of the job to copy", required = true)
    public TopLevelItem src;

    @Argument(metaVar = "DST", usage = "Name of the new job to be created.", index = 1, required = true)
    @SuppressFBWarnings(value = "PA_PUBLIC_PRIMITIVE_ATTRIBUTE", justification = "Preserve API compatibility")
    public String dst;

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

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

        ModifiableTopLevelItemGroup ig = jenkins;
        int i = dst.lastIndexOf('/');
        if (i > 0) {
            String group = dst.substring(0, i);
            Item item = jenkins.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);
            }
            dst = dst.substring(i + 1);
        }

View on GitHub (pinned to 2e228ff40b)

Solutions

  1. Choose a unique destination name (e.g. append a timestamp or counter).
  2. Delete the existing destination first: `java -jar jenkins-cli.jar delete-job <dst>` then retry the copy.
  3. In a script, check `jenkins.getItemByFullName(dst) == null` before invoking copy-job.

Example fix

// before:
//   java -jar jenkins-cli.jar copy-job template newjob   // newjob exists
//   -> IllegalStateException: Job 'newjob' already exists
//
// after: pick a fresh name or delete first
//   java -jar jenkins-cli.jar delete-job newjob
//   java -jar jenkins-cli.jar copy-job template newjob
Defensive patterns

Strategy: validation

Validate before calling

// Ensure destination is free before copy-job
if (jenkins.getItemByFullName(dst) != null) {
    throw new IllegalStateException("Destination already exists: " + dst
        + " — delete it or pick a unique name");
}

Prevention

When it happens

Trigger: 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).

Common situations: 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.

Related errors


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