All Products
Search
Document Center

AI Guardrails:Delete faces

Last Updated:Mar 31, 2026

Removes one or more faces from a person in your custom face library using the Content Moderation Java SDK.

Prerequisites

Before you begin, make sure you have:

  • Java dependencies installed. For installation instructions, see Installation.

  • (Optional) The Extension.Uploader utility class downloaded and imported into your project, if your workflow involves submitting local images or binary image streams for image moderation.

Important

Use the Java version specified in the Installation topic. Using a different version causes subsequent API calls to fail.

Usage notes

  • Both personId and faceIds are required. personId identifies the person to update; faceIds is the list of face IDs to remove. For the full parameter reference, see API operation for deleting faces.

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

Delete faces

The example below deletes one face from a person. Before running it, replace the following values:

PlaceholderDescription
personId_test_3The ID of the person whose face you want to delete
31820666292926465The face ID to delete. Add more IDs to the list to delete multiple faces at once
import java.util.Arrays;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;

import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.exceptions.ServerException;
import com.aliyuncs.green.model.v20180509.DeleteFacesRequest;
import com.aliyuncs.http.FormatType;
import com.aliyuncs.http.HttpResponse;
import com.aliyuncs.profile.DefaultProfile;

public class FaceDeleteFaceRequestSample {

    public static void main(String[] args) throws Exception {
        // Use a RAM user's AccessKey credentials instead of your Alibaba Cloud account
        // credentials to reduce security risk. Load credentials from environment variables
        // to avoid hardcoding sensitive information in your code.
        //
        // Method 1 (environment variables):
        //   AccessKey ID:     System.getenv("ALIBABA_CLOUD_ACCESS_KEY_ID")
        //   AccessKey secret: System.getenv("ALIBABA_CLOUD_ACCESS_KEY_SECRET")
        //
        // Method 2 (system properties):
        //   AccessKey ID:     System.getProperty("ALIBABA_CLOUD_ACCESS_KEY_ID")
        //   AccessKey secret: System.getProperty("ALIBABA_CLOUD_ACCESS_KEY_SECRET")
        DefaultProfile profile = DefaultProfile.getProfile(
                "cn-shanghai",
                System.getenv("ALIBABA_CLOUD_ACCESS_KEY_ID"),
                System.getenv("ALIBABA_CLOUD_ACCESS_KEY_SECRET"));
        DefaultProfile.addEndpoint("cn-shanghai", "Green", "green.cn-shanghai.aliyuncs.com");

        // Reuse the client instance across requests to improve performance
        // and avoid repeated connection setup.
        IAcsClient client = new DefaultAcsClient(profile);

        DeleteFacesRequest deleteFacesRequest = new DeleteFacesRequest();
        deleteFacesRequest.setAcceptFormat(FormatType.JSON);
        deleteFacesRequest.setMethod(com.aliyuncs.http.MethodType.POST);
        deleteFacesRequest.setEncoding("utf-8");

        JSONObject data = new JSONObject();
        data.put("personId", "personId_test_3");                   // Required: the person's ID
        data.put("faceIds", Arrays.asList("31820666292926465"));    // Required: face IDs to delete

        deleteFacesRequest.setHttpContent(data.toJSONString().getBytes("UTF-8"), "UTF-8", FormatType.JSON);

        deleteFacesRequest.setConnectTimeout(3000); // 3 seconds
        deleteFacesRequest.setReadTimeout(6000);    // 6 seconds

        try {
            HttpResponse httpResponse = client.doAction(deleteFacesRequest);

            if (httpResponse.isSuccess()) {
                JSONObject scrResponse = JSON.parseObject(new String(httpResponse.getHttpContent(), "UTF-8"));
                System.out.println(JSON.toJSONString(scrResponse, true));

                if (200 == scrResponse.getInteger("code")) {
                    JSONObject resultObject = scrResponse.getJSONObject("data");
                    if (200 == resultObject.getInteger("code")) {
                        // Deletion succeeded; print the person ID as confirmation
                        System.out.println(resultObject.getString("personId"));
                    } else {
                        System.out.println("task process fail:" + resultObject.getInteger("code"));
                    }
                } else {
                    System.out.println("detect not success. code:" + scrResponse.getInteger("code"));
                }
            } else {
                System.out.println("response not success. status:" + httpResponse.getStatus());
            }
        } catch (ServerException e) {
            e.printStackTrace();
        } catch (ClientException e) {
            e.printStackTrace();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

What's next