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:
Python dependencies installed at the required version. See Installation. Using the wrong Python version causes subsequent API calls to fail.
The Extension.Uploader utility class downloaded and imported into your project
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
| Placeholder | Description | Example |
|---|---|---|
<your-person-id> | The ID of the person to delete | python_personId_test_1 |
ALIBABA_CLOUD_ACCESS_KEY_ID | Environment variable for your RAM user's AccessKey ID | — |
ALIBABA_CLOUD_ACCESS_KEY_SECRET | Environment 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
codeis not200, the request failed (for example, due to an authentication or endpoint error).If the top-level
codeis200butdata.codeis not200, the delete operation failed for the specified person.
Common errors
| Error condition | Likely cause | Action |
|---|---|---|
Top-level code is not 200 | Invalid credentials, wrong endpoint, or network error | Check that your AccessKey ID, AccessKey secret, and endpoint (green.cn-shanghai.aliyuncs.com) are correct |
data.code is not 200 | The personId does not exist or has already been deleted | Confirm the personId value and retry with a valid ID |
What's next
Add a person — register a new person in the library
API operation for deleting a person — full parameter reference