This topic describes how to use the Alibaba Cloud ECS Java SDK to call the DeleteInstance operation to delete an ECS instance.

Prerequisites

You must have at least one existing ECS instance. For more information, see Batch create ECS instances.

Sample code

The following code can be used to delete an ECS instance in China (Hangzhou).
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.exceptions.ServerException;
import com.aliyuncs.profile.DefaultProfile;
import com.google.gson.Gson;
import java.util.*;
import com.aliyuncs.ecs.model.v20140526.*;

public class DeleteInstance {

    public static void main(String[] args) {
        // Create and initialize a DefaultAcsClient instance.
        DefaultProfile profile = DefaultProfile.getProfile("cn-hangzhou", "<yourAccessKeyId>", "<yourAccessSecret>");
        IAcsClient client = new DefaultAcsClient(profile);

        // Create an API request and configure the parameters.
        DeleteInstanceRequest request = new DeleteInstanceRequest();
        // Specify a region ID.
        request.setRegionId("cn-hangzhou");
        // Specify an instance ID.
        request.setInstanceId("i-bp1gvi17n5p8hav0i***");

        try {
        // Initiate the request and handle the response or exceptions.
            DeleteInstanceResponse response = client.getAcsResponse(request);
            System.out.println(new Gson().toJson(response));
        } catch (ServerException e) {
            e.printStackTrace();
        } catch (ClientException e) {
            System.out.println("ErrCode:" + e.getErrCode());
            System.out.println("ErrMsg:" + e.getErrMsg());
            System.out.println("RequestId:" + e.getRequestId());
        }

    }
}

Result

Response:
{"RequestId":"928E2273-5715-46B9-A730-238DC996A533"}