{"record":{"id":"9e15606a9a6ed7d0","repo":"invoke-ai/InvokeAI","slug":"unexpected-error-while-deleting-all-except-current","errorCode":null,"errorMessage":"Unexpected error while deleting all except current: {e}","messagePattern":"Unexpected error while deleting all except current: (.+?)","errorType":"http","errorClass":"HTTPException","httpStatus":500,"severity":"error","filePath":"invokeai/app/api/routers/session_queue.py","lineNumber":331,"sourceCode":"\n@session_queue_router.put(\n    \"/{queue_id}/delete_all_except_current\",\n    operation_id=\"delete_all_except_current\",\n    responses={200: {\"model\": DeleteAllExceptCurrentResult}},\n)\ndef delete_all_except_current(\n    current_user: CurrentUserOrDefault,\n    queue_id: str = Path(description=\"The queue id to perform this operation on\"),\n) -> DeleteAllExceptCurrentResult:\n    \"\"\"Immediately deletes all queue items except in-processing items. Non-admin users can only delete their own items.\"\"\"\n    try:\n        # Admin users can delete all items, non-admin users can only delete their own\n        user_id = None if current_user.is_admin else current_user.user_id\n        return ApiDependencies.invoker.services.session_queue.delete_all_except_current(\n            queue_id=queue_id, user_id=user_id\n        )\n    except Exception as e:\n        raise HTTPException(status_code=500, detail=f\"Unexpected error while deleting all except current: {e}\")\n\n\n@session_queue_router.put(\n    \"/{queue_id}/cancel_by_batch_ids\",\n    operation_id=\"cancel_by_batch_ids\",\n    responses={200: {\"model\": CancelByBatchIDsResult}},\n)\ndef cancel_by_batch_ids(\n    current_user: CurrentUserOrDefault,\n    queue_id: str = Path(description=\"The queue id to perform this operation on\"),\n    batch_ids: list[str] = Body(description=\"The list of batch_ids to cancel all queue items for\", embed=True),\n) -> CancelByBatchIDsResult:\n    \"\"\"Immediately cancels all queue items from the given batch ids. Non-admin users can only cancel their own items.\"\"\"\n    try:\n        # Admin users can cancel all items, non-admin users can only cancel their own\n        user_id = None if current_user.is_admin else current_user.user_id\n        return ApiDependencies.invoker.services.session_queue.cancel_by_batch_ids(\n            queue_id=queue_id, batch_ids=batch_ids, user_id=user_id","sourceCodeStart":313,"sourceCodeEnd":349,"githubUrl":"https://github.com/invoke-ai/InvokeAI/blob/0b6a024f2ff6a86bfb953dcdb9cc504ef7397a06/invokeai/app/api/routers/session_queue.py#L313-L349","documentation":"Thrown by delete_all_except_current: any exception from SessionQueue.delete_all_except_current(queue_id, user_id) is wrapped as a 500 with the cause embedded in the detail. Deletion touches more state than cancellation (rows are removed), so foreign-key or integrity problems in the queue store surface through this handler.","triggerScenarios":"PUT /{queue_id}/delete_all_except_current with a bad queue_id, DB constraint/foreign-key failure while deleting queue rows, or a service-layer error during bulk delete.","commonSituations":"Deleting while items are actively being written by the processor (contention); databases with orphaned references from interrupted upgrades; stale queue_id pointing at a nonexistent queue.","solutions":["Read the embedded exception message in detail for the specific DB/service failure","Pause the processor before bulk-deleting to avoid write contention","Validate queue_id exists before the call","Back up and run DB integrity checks / migrations if constraint errors recur"],"exampleFix":"// before: delete while processing\nawait fetch(`/api/v1/queue/${queueId}/delete_all_except_current`, {method: 'PUT'});\n// after: pause first, then delete\nawait fetch(`/api/v1/queue/${queueId}/processor/pause`, {method: 'PUT'});\nawait fetch(`/api/v1/queue/${queueId}/delete_all_except_current`, {method: 'PUT'});","handlingStrategy":"try-catch","validationCode":"const q = await fetch(`/api/v1/queue/${queueId}`);\nif (!q.ok) throw new Error(`Queue ${queueId} not found`);\nawait fetch(`/api/v1/queue/${queueId}/processor/pause`, {method:'PUT'}); // stop writes before delete","typeGuard":"function isDeleteResult(v) { return v !== null && typeof v === 'object' && 'deleted' in v; }","tryCatchPattern":"try {\n  const res = await fetch(`/api/v1/queue/${queueId}/delete_all_except_current`, {method:'PUT'});\n  if (res.status === 500) throw new Error((await res.json()).detail);\n  return await res.json();\n} catch (e) { console.error('bulk delete failed:', e.message); }","preventionTips":["Pause the processor before deleting to avoid write contention and constraint errors","Keep the database migrated; orphaned rows cause FK failures on bulk delete","Back up invokeai.db before large destructive operations","Treat the embedded detail message as the primary diagnostic"],"tags":["fastapi","http-500","queue","deletion","api"],"backgroundTag":"api-internal-server-error","analyzedSha":"0b6a024f2ff6a86bfb953dcdb9cc504ef7397a06","analyzedAt":"2026-08-29T04:46:49.967Z","schemaVersion":2},"datasetVersion":"2026-08-29T07:17:48.351Z"}