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


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 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.
        // Specify an instance ID.

        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) {
        } catch (ClientException e) {
            System.out.println("ErrCode:" + e.getErrCode());
            System.out.println("ErrMsg:" + e.getErrMsg());
            System.out.println("RequestId:" + e.getRequestId());