This topic describes how to use Alibaba Cloud Elastic Compute Service (ECS) SDK for Java to call the DeleteInstance operation to release an ECS instance.

Prerequisites

One or more ECS instances are created. For more information, see Batch create ECS instances.

Precautions

After an instance is released, its data is deleted and cannot be recovered. We recommend that you create snapshots to back up data before you release the instance. For more information, see Snapshot overview.
Note After an instance is released, snapshots and images that were manually created from the instance are not affected.

Sample code

The following code can be used to release an instance in the China (Hangzhou) region:
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 a DefaultAcsClient instance and initialize the 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

The following response is returned:
{"RequestId":"928E2273-5715-46B9-A730-238DC996A533"}