Deleting key pairs - Amazon SDK for .NET
Services or capabilities described in Amazon Web Services documentation might vary by Region. To see the differences applicable to the China Regions, see Getting Started with Amazon Web Services in China (PDF).

Deleting key pairs

This example shows you how to use the Amazon SDK for .NET to delete a key pair. The application takes the name of a key pair. It deletes the key pair and then displays all available key pairs. If you provide no command-line arguments, the application simply displays all available key pairs.

The following sections provide snippets of this example. The complete code for the example is shown after that, and can be built and run as is.

Delete the key pair

The following snippet deletes a key pair.

The example at the end of this topic shows this snippet in use.

// // Method to delete a key pair private static async Task DeleteKeyPair(IAmazonEC2 ec2Client, string keyName) { await ec2Client.DeleteKeyPairAsync(new DeleteKeyPairRequest{ KeyName = keyName}); Console.WriteLine($"\nKey pair {keyName} has been deleted (if it existed)."); }

Display available key pairs

The following snippet displays a list of the available key pairs.

The example at the end of this topic shows this snippet in use.

// // Method to show the key pairs that are available private static async Task EnumerateKeyPairs(IAmazonEC2 ec2Client) { DescribeKeyPairsResponse response = await ec2Client.DescribeKeyPairsAsync(); Console.WriteLine("Available key pairs:"); foreach (KeyPairInfo item in response.KeyPairs) Console.WriteLine($" {item.KeyName}"); }

Complete code

This section shows relevant references and the complete code for this example.

NuGet packages:

Programming elements:

using System; using System.Threading.Tasks; using Amazon.EC2; using Amazon.EC2.Model; namespace EC2DeleteKeyPair { class Program { static async Task Main(string[] args) { // Create the EC2 client var ec2Client = new AmazonEC2Client(); if(args.Length == 1) { // Delete a key pair (if it exists) await DeleteKeyPair(ec2Client, args[0]); // Display the key pairs that are left await EnumerateKeyPairs(ec2Client); } else { Console.WriteLine("\nUsage: EC2DeleteKeyPair keypair-name"); Console.WriteLine(" keypair-name - The name of the key pair you want to delete."); Console.WriteLine("\nNo arguments specified."); Console.Write( "Do you want to see a list of the existing key pairs? ((y) or n): "); string response = Console.ReadLine(); if((string.IsNullOrEmpty(response)) || (response.ToLower() == "y")) await EnumerateKeyPairs(ec2Client); } } // // Method to delete a key pair private static async Task DeleteKeyPair(IAmazonEC2 ec2Client, string keyName) { await ec2Client.DeleteKeyPairAsync(new DeleteKeyPairRequest{ KeyName = keyName}); Console.WriteLine($"\nKey pair {keyName} has been deleted (if it existed)."); } // // Method to show the key pairs that are available private static async Task EnumerateKeyPairs(IAmazonEC2 ec2Client) { DescribeKeyPairsResponse response = await ec2Client.DescribeKeyPairsAsync(); Console.WriteLine("Available key pairs:"); foreach (KeyPairInfo item in response.KeyPairs) Console.WriteLine($" {item.KeyName}"); } } }