{"record":{"id":"1c9fff0f568d15ce","repo":"passbolt/passbolt_api","slug":"the-metadata-key-does-not-exist-or-has-been-deleted","errorCode":null,"errorMessage":"The metadata key does not exist or has been deleted.","messagePattern":"The metadata key does not exist or has been deleted\\.","errorType":"http","errorClass":"NotFoundException","httpStatus":404,"severity":"error","filePath":"plugins/PassboltCe/Metadata/src/Service/MetadataKey/MetadataKeyDeleteService.php","lineNumber":61,"sourceCode":"     * @throws \\Cake\\Http\\Exception\\NotFoundException if the key does not exist or is already deleted\n     * @throws \\Cake\\Http\\Exception\\BadRequestException if the key id format is Invalid or some items are still using the key\n     */\n    public function delete(UserAccessControl $uac, string $id): void\n    {\n        $uac->assertIsAdmin();\n\n        if (!Validation::uuid($id)) {\n            throw new BadRequestException(__('The metadata key ID should be a valid UUID.'));\n        }\n\n        $metadataKeysTable = $this->fetchTable('Passbolt/Metadata.MetadataKeys');\n\n        // Assert the key exist\n        try {\n            /** @var \\Passbolt\\Metadata\\Model\\Entity\\MetadataKey $metadataKey */\n            $metadataKey = $metadataKeysTable->get($id);\n        } catch (RecordNotFoundException $exception) { // @phpstan-ignore-line\n            throw new NotFoundException(__('The metadata key does not exist or has been deleted.'), 404, $exception);\n        }\n\n        // Assert the key is not already deleted\n        if ($metadataKey->isDeleted()) {\n            throw new NotFoundException(__('The metadata key has already been deleted.'));\n        }\n\n        // Assert they key was previously marked as expired\n        if (!$metadataKey->isExpired()) {\n            throw new BadRequestException(__('The metadata key should be marked as expired first.'));\n        }\n\n        // Assert the key is not used by folders, resources, tags, etc.\n        if ((new MetadataKeyAssertUsageService())->isKeyInUse($metadataKey->get('id'))) {\n            $msg = __('The metadata key is still in use, migrate the remaining items to the new key first.');\n            throw new BadRequestException($msg);\n        }\n","sourceCodeStart":43,"sourceCodeEnd":79,"githubUrl":"https://github.com/passbolt/passbolt_api/blob/31c1bbc10f32808a607fa9bd81891e898779c0bc/plugins/PassboltCe/Metadata/src/Service/MetadataKey/MetadataKeyDeleteService.php#L43-L79","documentation":"Thrown when no metadata key row matches the given UUID: MetadataKeysTable::get() raises RecordNotFoundException, which the service converts to NotFoundException (HTTP 404). 'Deleted' is included because soft-deleted keys may also be filtered out.","triggerScenarios":"DELETE /metadata/keys/<valid-uuid> where the UUID does not exist, belongs to another environment/organization, or references an already soft-deleted key.","commonSituations":"Stale id cached from a previous database or a key deleted by another admin concurrently; pointing a script at a different environment (staging vs prod); DB was re-seeded or migrations recreated the table.","solutions":["List current keys via GET /metadata/keys and confirm the id exists","Use the correct environment/database credentials","Handle HTTP 404 in the caller and re-fetch the key list instead of retrying a stale id"],"exampleFix":"// before\nawait deleteMetadataKey(staleKeyId);\n// after\nconst keys = await listMetadataKeys();\nconst key = keys.find(k => k.id === staleKeyId);\nif (!key) throw new Error('key no longer exists');\nawait deleteMetadataKey(key.id);","handlingStrategy":"try-catch","validationCode":"const key = (await listMetadataKeys()).find(k => k.id === id);\nif (!key) throw new Error(`metadata key ${id} not found`);","typeGuard":null,"tryCatchPattern":"try {\n  await deleteMetadataKey(id);\n} catch (e) {\n  if (e.status === 404) refreshKeyCache(); else throw e;\n}","preventionTips":["Refresh the key list before batch operations","Don't reuse ids across environments","Treat 404 as a signal to re-sync, not retry"],"tags":["not-found","metadata-keys","rest-api"],"backgroundTag":"entity-not-found","analyzedSha":"31c1bbc10f32808a607fa9bd81891e898779c0bc","analyzedAt":"2026-09-17T00:04:38.960Z","contentChangedAt":"2026-09-17T00:04:38.960Z","schemaVersion":2},"datasetVersion":"2026-09-21T04:17:39.646Z"}