This topic describes how to use SDK for Java to delete one or more event targets of an event rule.

Sample code:

import java.util.Arrays;

import com.aliyun.eventbridge.EventBridge;
import com.aliyun.eventbridge.EventBridgeClient;
import com.aliyun.eventbridge.models.Config;
import com.aliyun.eventbridge.models.DeleteTargetsRequest;
import com.aliyun.eventbridge.models.DeleteTargetsResponse;

public class deleteTargetsSample {

    private EventBridge eventBridgeClient;

    public deleteTargetsSample() {
        Config authConfig = new Config();
    authConfig.accessKeyId = "{accessKeyId}";
        authConfig.accessKeySecret = "{accessKeySecret}";
        authConfig.endpoint = "{endpoint}";
        eventBridgeClient = new EventBridgeClient(authConfig);
    }

    public void deleteTargetsSample() {
        try {
            DeleteTargetsRequest deleteTargetsResponse = new DeleteTargetsRequest();
            deleteTargetsResponse.setRuleName("myRule");
            deleteTargetsResponse.setEventBusName("mybus");
            deleteTargetsResponse.setTargetIds(Arrays.asList("httpTarget2"));
            DeleteTargetsResponse response = eventBridgeClient.deleteTargets(deleteTargetsResponse);
            System.out.println("remove targets success : " + response);
        } catch (Throwable e) {
            e.printStackTrace();
        }
    }

    public static void main(String[] args) {
        deleteTargetsSample eventRuleSamples = new deleteTargetsSample();
        try {
            eventRuleSamples.deleteTargetsSample();
        } catch (Throwable e) {
            e.printStackTrace();
        }
    }
}