{"record":{"id":"cc98bdb73a79e2ee","repo":"appwrite/appwrite","slug":"user-target-not-found","errorCode":"user_target_not_found","errorMessage":"The target could not be found.","messagePattern":"The target could not be found\\.","errorType":"exception","errorClass":"Appwrite\\Extend\\Exception","httpStatus":404,"severity":"error","filePath":"app/controllers/api/account.php","lineNumber":4733,"sourceCode":"                model: Response::MODEL_TARGET,\n            )\n        ],\n        contentType: ContentType::JSON\n    ))\n    ->param('targetId', '', fn (Database $dbForProject) => new UID($dbForProject->getAdapter()->getMaxUIDLength()), 'Target ID.', false, ['dbForProject'])\n    ->param('identifier', '', new Text(Database::LENGTH_KEY), 'The target identifier (token, email, phone etc.)')\n    ->inject('queueForEvents')\n    ->inject('user')\n    ->inject('request')\n    ->inject('response')\n    ->inject('dbForProject')\n    ->inject('authorization')\n    ->action(function (string $targetId, string $identifier, Event $queueForEvents, Document $user, Request $request, Response $response, Database $dbForProject, Authorization $authorization) {\n\n        $target = $authorization->skip(fn () => $dbForProject->getDocument('targets', $targetId));\n\n        if ($target->isEmpty()) {\n            throw new Exception(Exception::USER_TARGET_NOT_FOUND);\n        }\n\n        if ($user->getId() !== $target->getAttribute('userId')) {\n            throw new Exception(Exception::USER_TARGET_NOT_FOUND);\n        }\n\n        if ($identifier) {\n            $target\n                ->setAttribute('identifier', $identifier)\n                ->setAttribute('expired', false);\n        }\n\n        $detector = new Detector($request->getUserAgent());\n        $detector->skipBotDetection(); // OPTIONAL: If called, bot detection will completely be skipped (bots will be detected as regular devices then)\n\n        $device = $detector->getDevice();\n\n        $target->setAttribute('name', \"{$device['deviceBrand']} {$device['deviceModel']}\");","sourceCodeStart":4715,"sourceCodeEnd":4751,"githubUrl":"https://github.com/appwrite/appwrite/blob/cd368e707d4b492bc5e8e9c0f8ecbc5b741c4bf4/app/controllers/api/account.php#L4715-L4751","documentation":"Thrown by the update-push-target endpoint (PUT/PATCH /v1/account/targets/:targetId) when the target document fetched for the given targetId is empty. The target does not exist. Lookup uses authorization->skip so absence is real, not a permission issue.","triggerScenarios":"PUT /v1/account/targets/:targetId where :targetId does not exist. Common after the target was deleted, the ID is stale, or the client is using an invented/hardcoded value.","commonSituations":"Target was purged by a delete call but the client cached the ID; push-token refresh runs after the device row was deleted; client constructed the URL with a wrong ID; database migration dropped targets.","solutions":["Before updating, ensure the target still exists (GET) or refresh the local target list.","On this error, register a new target via createTarget('unique()', ...) and store the returned ID.","Do not cache targetId indefinitely; refetch from the server when the app starts."],"exampleFix":"// before: update a cached target id that may be gone\nawait account.updateTarget(cachedTargetId, { identifier: newToken });\n\n// after: fall back to create when the target is missing\ntry {\n  await account.updateTarget(cachedTargetId, { identifier: newToken });\n} catch (e) {\n  if (e.code === 'user_target_not_found') {\n    const created = await account.createTarget('unique()', { identifier: newToken, providerId });\n    cacheTargetId(created.$id);\n  } else { throw e; }\n}","handlingStrategy":"try-catch","validationCode":"// Verify the cached target ID is still listed before updating.\nconst targets = await account.listTargets();\nconst exists = targets.targets.some(t => t.$id === cachedId);\nif (!exists) throw new ClientError('Target is gone; re-register.');","typeGuard":"function isTargetIdLike(id) {\n  return typeof id === 'string' && id.length >= 8;\n}","tryCatchPattern":"try {\n  await account.updateTarget(cachedId, { identifier: token });\n} catch (e) {\n  if (e.code === 'user_target_not_found') {\n    const created = await account.createTarget('unique()', { identifier: token, providerId });\n    cacheTargetId(created.$id);\n  } else { throw e; }\n}","preventionTips":["Refetch the target list on app launch rather than trusting a cached ID.","After delete, evict the ID from local caches immediately.","On token refresh, be prepared to create rather than update."],"tags":["push","targets","lifecycle","update"],"backgroundTag":null,"analyzedSha":"cd368e707d4b492bc5e8e9c0f8ecbc5b741c4bf4","analyzedAt":"2026-08-12T14:42:48.571Z","schemaVersion":2},"datasetVersion":"2026-08-12T18:17:37.767Z"}