Select your cookie preferences

We use essential cookies and similar tools that are necessary to provide our site and services. We use performance cookies to collect anonymous statistics, so we can understand how customers use our site and make improvements. Essential cookies cannot be deactivated, but you can choose “Customize” or “Decline” to decline performance cookies.

If you agree, AWS and approved third parties will also use cookies to provide useful site features, remember your preferences, and display relevant content, including relevant advertising. To accept or decline all non-essential cookies, choose “Accept” or “Decline.” To make more detailed choices, choose “Customize.”

Terminating an Amazon EC2 instance - AWS SDK for .NET

Terminating an Amazon EC2 instance

When you no longer need one or more of your Amazon EC2 instances, you can terminate them.

This example shows you how to use the AWS SDK for .NET to terminate EC2 instances. It takes an instance ID as input.

NuGet packages:

Programming elements:

NuGet packages:

Programming elements:

using System; using System.Threading.Tasks; using System.Collections.Generic; using Amazon.EC2; using Amazon.EC2.Model; namespace EC2TerminateInstance { class Program { static async Task Main(string[] args) { if((args.Length == 1) && (args[0].StartsWith("i-"))) { // Terminate the instance var ec2Client = new AmazonEC2Client(); await TerminateInstance(ec2Client, args[0]); } else { Console.WriteLine("\nCommand-line argument missing or incorrect."); Console.WriteLine("\nUsage: EC2TerminateInstance instance-ID"); Console.WriteLine(" instance-ID - The EC2 instance you want to terminate."); return; } } // // Method to terminate an EC2 instance private static async Task TerminateInstance(IAmazonEC2 ec2Client, string instanceID) { var request = new TerminateInstancesRequest{ InstanceIds = new List<string>() { instanceID }}; TerminateInstancesResponse response = await ec2Client.TerminateInstancesAsync(new TerminateInstancesRequest{ InstanceIds = new List<string>() { instanceID } }); foreach (InstanceStateChange item in response.TerminatingInstances) { Console.WriteLine("Terminated instance: " + item.InstanceId); Console.WriteLine("Instance state: " + item.CurrentState.Name); } } } }

After you run the example, it's a good idea to sign in to the Amazon EC2 console to verify that the EC2 instance has been terminated.

PrivacySite termsCookie preferences
© 2025, Amazon Web Services, Inc. or its affiliates. All rights reserved.