{"record":{"id":"9046191f7e4782c5","repo":"GoogleContainerTools/skaffold","slug":"preparing-job-v-for-execution","errorCode":null,"errorMessage":"preparing job %v for execution","messagePattern":"preparing job (.+?) for execution","errorType":"console","errorClass":null,"httpStatus":null,"severity":"error","filePath":"pkg/skaffold/actions/k8sjob/task.go","lineNumber":103,"sourceCode":"\tif err != nil {\n\t\treturn err\n\t}\n\n\tc := t.getContainerToDeploy()\n\tvar original corev1.Container\n\tfor _, ec := range t.jobManifest.Spec.Template.Spec.Containers {\n\t\tif ec.Name == c.Name {\n\t\t\toriginal = ec\n\t\t\tbreak\n\t\t}\n\t}\n\tpatchToK8sContainer(c, &original)\n\n\tt.jobManifest.Spec.Template.Spec.Containers = []corev1.Container{original}\n\tt.jobManifest.ObjectMeta.Name = t.Name()\n\n\tif err := k8sjobutil.ForceJobDelete(ctx, t.jobManifest.Name, jm, t.kubectl); err != nil {\n\t\treturn errors.Wrap(err, fmt.Sprintf(\"preparing job %v for execution\", t.jobManifest.Name))\n\t}\n\n\tt.execEnv.TrackContainerAndJobFromBuild(graph.Artifact{\n\t\tImageName: t.jobManifest.Name,\n\t\tTag:       t.jobManifest.Name,\n\t}, tracker.Job{Name: t.jobManifest.Name, ID: t.jobManifest.Name}, &t.jobManifest)\n\n\tif err := t.deployJob(ctx, t.jobManifest, jm); err != nil {\n\t\treturn err\n\t}\n\n\tif err = t.watchStatus(ctx, t.jobManifest, jm); err != nil {\n\t\tt.execEnv.logger.CancelJobLogger(t.jobManifest.Name)\n\t\tk8sjobutil.ForceJobDelete(context.TODO(), t.jobManifest.Name, jm, t.kubectl)\n\t}\n\n\treturn err\n}","sourceCodeStart":85,"sourceCodeEnd":121,"githubUrl":"https://github.com/GoogleContainerTools/skaffold/blob/a1189de023efc32d4b8e11f395acc678aa555011/pkg/skaffold/actions/k8sjob/task.go#L85-L121","documentation":"K8sTask.Exec in Skaffold's k8sjob action runner calls k8sjobutil.ForceJobDelete to delete any stale Job with the same name before applying the new Job manifest. If that forced delete (or the preparation it performs) fails, the error is wrapped as \"preparing job <name> for execution\". The new Job cannot be scheduled until the old one is gone, since Job names are immutable in a namespace.","triggerScenarios":"Calling Exec when ForceJobDelete errors: a previous Job with the same name exists and cannot be deleted (finalizers blocking deletion, API server unreachable, RBAC forbids delete on jobs.batch, or bad kubecontext/namespace).","commonSituations":"A prior run's Job is stuck Terminating due to a finalizer; the service account running skaffold lacks delete permission on jobs; kubeconfig points at the wrong cluster/namespace; API server temporarily unavailable.","solutions":["Check the unwrapped kubectl error; run `kubectl -n <ns> get job <name> -o yaml` and remove blocking finalizers if stuck Terminating.","Manually delete the stale job (`kubectl -n <ns> delete job <name> --force --grace-period=0`) then rerun.","Grant the active identity RBAC: `rules: [{apiGroups: [batch], resources: [jobs], verbs: [\"delete\", \"create\"]}]`.","Verify kubeconfig context/namespace (kubectl config current-context) match the intended cluster.","Retry if the API server was transiently unreachable."],"exampleFix":"// before\nif err := task.Exec(ctx, out); err != nil { return err }\n// after\n// pre-check stale job before Exec\nerr := exec.Command(\"kubectl\", \"-n\", ns, \"delete\", \"job\", name, \"--ignore-not-found\").Run()\nif err != nil { return fmt.Errorf(\"stale job cleanup failed: %w\", err) }\nif err := task.Exec(ctx, out); err != nil { return err }","handlingStrategy":"retry","validationCode":"# ensure stale job is gone before Exec\nkubectl -n $NS delete job $JOB --ignore-not-found\nkubectl -n $NS wait --for=delete job/$JOB --timeout=60s || true","typeGuard":null,"tryCatchPattern":"// Go\nerr := task.Exec(ctx, out)\nif err != nil && strings.Contains(err.Error(), \"finalizer\") {\n    // retry after unblocking finalizers / forced delete\n    err = task.Exec(ctx, out)\n}","preventionTips":["Grant the runner RBAC delete on batch/jobs.","Watch for jobs stuck Terminating (finalizers) and clean them.","Verify kubeconfig context/namespace before runs."],"tags":["kubernetes","jobs","skaffold","rbac"],"backgroundTag":"kubernetes-resource-delete-failed","analyzedSha":"a1189de023efc32d4b8e11f395acc678aa555011","analyzedAt":"2026-09-05T12:09:27.064Z","contentChangedAt":"2026-09-05T12:09:27.064Z","schemaVersion":2},"datasetVersion":"2026-09-12T17:17:11.597Z"}