Delete key from a pgp KeyStore with C# and VB.NET

The example below demonstrates how to delete a key pair from a KeyStore. The first matching key pair counted by the internal order of the KeyStore is erased.

If only one of the keys of the key pair exists, for example only the public or only the private key, then only they will be deleted.

C# example

using System;
using DidiSoft.Pgp;
 
public class DeleteKeyPair
{
 public static void Demo()
 {
   // initialize the key store
   KeyStore ks = new KeyStore(@"DataFiles\key.store", "changeit");
 
   // delete the first key pair with this user Id
   ks.DeleteKeyPair("demo2@support.com");
 }
}

VB.NET code

Imports System
Imports DidiSoft.Pgp
 
Public Class DeleteKeyPair
 Public Shared Sub Demo()
  ' initialize the key store
  Dim ks As New KeyStore("DataFiles\key.store", "changeit")
 
  ' delete the first key pair with this user Id
  ks.DeleteKeyPair("demo2@didisoft.com")
 End Sub
End Class

If there is more than one key with the same user Id then we can use the overloaded version of the DeleteKeyPair method that accepts Key Id