Delete a person

更新时间:
复制 MD 格式

Delete a person record from the Content Moderation custom person library using the Python SDK. Deleting a person permanently removes the person record and all associated face images.

Prerequisites

Before you begin, ensure that you have:

Usage notes

  • Deleting a person record permanently removes all associated face images and related information.

  • Use Content Moderation API endpoints when calling this SDK. For the full list, see Endpoints.

  • For the full parameter reference, see API operation for deleting a person.

Delete a person record

Replace <your-person-id> with the ID of the person to delete. Set your AccessKey ID and AccessKey secret as environment variables before running the code.

# coding=utf-8
# Delete a person

import os
import json
from aliyunsdkcore import client
from aliyunsdkcore.profile import region_provider
from aliyunsdkgreen.request.v20180509 import DeletePersonRequest

# Reuse the client instance across requests to improve moderation performance
# and avoid repeated connection overhead.
# Load AccessKey credentials from environment variables — do not hardcode them.
access_key_id = os.environ.get('ALIBABA_CLOUD_ACCESS_KEY_ID')
access_key_secret = os.environ.get('ALIBABA_CLOUD_ACCESS_KEY_SECRET')

clt = client.AcsClient(access_key_id, access_key_secret, "cn-shanghai")
region_provider.modify_point('Green', 'cn-shanghai', 'green.cn-shanghai.aliyuncs.com')

request = DeletePersonRequest.DeletePersonRequest()
request.set_accept_format('JSON')

# Replace <your-person-id> with the personId of the person to delete
request.set_content(
    bytearray(json.dumps({"personId": "<your-person-id>"}), "utf-8"))

response = clt.do_action_with_exception(request)
result = json.loads(response)

if result["code"] == 200:
    result_object = result["data"]
    if result_object["code"] == 200:
        # Person deleted successfully
        print("Deleted personId:", result_object["personId"])
    else:
        print("Delete failed. Error code:", result_object["code"])
else:
    print("Request failed. Top-level code:", result["code"])

Placeholders

PlaceholderDescriptionExample
<your-person-id>The ID of the person to deletepython_personId_test_1
ALIBABA_CLOUD_ACCESS_KEY_IDEnvironment variable for your RAM user's AccessKey ID
ALIBABA_CLOUD_ACCESS_KEY_SECRETEnvironment variable for your RAM user's AccessKey secret

Response structure

A successful response returns code: 200 at both the top level and within data:

{
  "code": 200,
  "data": {
    "code": 200,
    "personId": "<your-person-id>"
  }
}
  • If the top-level code is not 200, the request failed (for example, due to an authentication or endpoint error).

  • If the top-level code is 200 but data.code is not 200, the delete operation failed for the specified person.

Common errors

Error conditionLikely causeAction
Top-level code is not 200Invalid credentials, wrong endpoint, or network errorCheck that your AccessKey ID, AccessKey secret, and endpoint (green.cn-shanghai.aliyuncs.com) are correct
data.code is not 200The personId does not exist or has already been deletedConfirm the personId value and retry with a valid ID

What's next