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.


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


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 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. 
        // 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());



The following response is returned: