{"record":{"id":"dd862a0e23979926","repo":"oven-sh/bun","slug":"failed-to-delete-existing-gallery-image-version","errorCode":null,"errorMessage":"Failed to delete existing gallery image version: ${del.status} ${await del.text()}","messagePattern":"Failed to delete existing gallery image version: (.+?) (.+?)","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"scripts/machine.mjs","lineNumber":1236,"sourceCode":"    headers: { Authorization: `Bearer ${token}` },\n  });\n  if (existing.ok) {\n    console.log(`[packer] Deleting existing gallery image version 1.0.0 of ${imageDefName} before re-publish`);\n    const del = await fetch(`https://management.azure.com${versionPath}?api-version=2024-03-03`, {\n      method: \"DELETE\",\n      headers: { Authorization: `Bearer ${token}` },\n    });\n    if (del.status === 202) {\n      const op = del.headers.get(\"Azure-AsyncOperation\") ?? del.headers.get(\"Location\");\n      for (let i = 0; op && i < 120; i++) {\n        await new Promise(r => setTimeout(r, 10_000));\n        const poll = await fetch(op, { headers: { Authorization: `Bearer ${token}` } });\n        const body = await poll.json().catch(() => ({}));\n        if (body.status === \"Succeeded\") break;\n        if (body.status === \"Failed\") throw new Error(`Delete of ${versionPath} failed: ${JSON.stringify(body)}`);\n      }\n    } else if (!del.ok && del.status !== 404) {\n      throw new Error(`Failed to delete existing gallery image version: ${del.status} ${await del.text()}`);\n    }\n  }\n\n  // Install Packer if not available\n  const packerBin = await ensurePacker();\n\n  // Initialize plugins\n  console.log(\"[packer] Initializing plugins...\");\n  await spawnSafe([packerBin, \"init\", templateDir], { stdio: \"inherit\" });\n\n  // Build the image\n  console.log(`[packer] Building ${templateName} image: ${imageDefName}`);\n  const packerArgs = [\n    packerBin,\n    \"build\",\n    \"-only\",\n    `azure-arm.${templateName}`,\n    \"-var\",","sourceCodeStart":1218,"sourceCodeEnd":1254,"githubUrl":"https://github.com/oven-sh/bun/blob/8c5296ac459e8252d3cd702f3fbcbb0c249d95d5/scripts/machine.mjs#L1218-L1254","documentation":"The DELETE of the existing gallery image version returned a non-OK, non-404 status (404 means 'already gone' and is fine). The message embeds the HTTP status and response text: 401/403 = auth or RBAC, 409 = another conflicting operation on the same version.","triggerScenarios":"Token expired between auth and delete; principal lacking Microsoft.Compute/galleries/images/versions/delete; a concurrent delete/replica operation on version 1.0.0 returning 409.","commonSituations":"Overlapping publish jobs deleting the same version; RBAC drift after principal rotation; long bakes letting the bearer token age out.","solutions":["Map the embedded status: 401/403 fix credentials/RBAC, 409 wait for the in-flight operation then retry","Serialize the publish pipeline so only one job deletes a given version","Re-run the publish job — the delete-and-recreate path is designed to be idempotent"],"exampleFix":null,"handlingStrategy":"try-catch","validationCode":"if (!token) throw new Error('no Azure token — call getAzureToken before deleting versions');\nconst probe = await fetch(`https://management.azure.com${versionPath}?api-version=2024-03-03`, {\n  headers: { Authorization: `Bearer ${token}` },\n});\nif (probe.status === 401 || probe.status === 403) throw new Error('token/RBAC cannot delete gallery versions');","typeGuard":null,"tryCatchPattern":"try {\n  await deleteVersion(versionPath);\n} catch (error) {\n  const msg = String(error);\n  if (/ 409 /.test(msg)) {\n    // wait for the in-flight operation, then retry the delete once\n  } else {\n    throw error; // 401/403 need credential/RBAC fixes, not retries\n  }\n}","preventionTips":["Refresh the Azure token before long bake sequences so it doesn't age out mid-publish","Only one pipeline at a time may delete a given gallery version","404 on this DELETE is normal (already gone) — never treat it as a failure"],"tags":["azure","gallery","http","ci"],"backgroundTag":null,"analyzedSha":"8c5296ac459e8252d3cd702f3fbcbb0c249d95d5","analyzedAt":"2026-08-16T08:01:58.794Z","schemaVersion":2},"datasetVersion":"2026-08-16T08:17:34.114Z"}