Linux Commands

How to Delete GPG Keys in Linux

Is your GPG keyring overloaded with hundreds of unnecessary entries? If yes, then you should immediately delete the revoked, unsigned, expired keys. The ideal solution of this issue is to only keep the keys that have signed your public key and then import the new keys as per requirements. Do you not know how to delete GPG keys in Linux? No worries! This write-up will demonstrate how to delete GPG keys of single and multiple users. The procedures of using “uid” and the “Key ID” for deleting the private and public keys will be provided as well. So, let’s start!

Type of GPG keys

The GPG key pair comprises two types of keys: Private and Public keys. You can use the public key for the data encryption, and that encrypted data will be decrypted using the Private key. As a Linux user, you can only share your Public key. For security reasons, you must keep your Private key secret. Anyone can use your public key to encrypt the data; however, the private key will be needed to decrypt it.

How to list GPG keys in Linux

The gpg command permits Linux users to list public keys and private keys using two different commands. For instance, the “–list-keys” option is added in the “gpg” command for listing the public GPG keys from the public key:

$ gpg --list-keys

In the below-given output, “pub” represents Public key, “uid” indicates the “User ID”, and “sub” shows the Subkey:

The secret keyring stores the private GPG keys, and to list them on the Linux terminal, add the “–list-secret-keys” in the “gpg” command and execute it:

$ gpg --list-secret-keys

Here, “sec” represents Secret or Private key, “uid” is for the User ID, and “ssb” indicates the Secret Subkey:

Note: To delete the GPG keys of a specific user, note down its user ID “uid” and “Key ID” from the output of the above-given commands and then move to the next section.

How to delete GPG keys of a single user in Linux

There are two methods for deleting a user’s public and private GPG keys; either you can delete them using the user ID “uid” or their Key ID (Public ID). Also, remember that you have to delete the private key first and then move towards the public key deletion process, as GPG only permits you to delete the public key when its related private key is removed from the system.

Here is the syntax for deleting private GPG using the “uid”:

$ gpg --delete-secret-key [uid]

For instance, to delete the GPG keys of the user “Liza”, we will first delete its private key by specifying the “Liza” “uid” in the following command:

$ gpg --delete-secret-key liza

A confirmation dialog box will appear on your screen, click on the “Delete Key” button, and the private key of the specified user will be deleted within a few seconds:

Now, check out the syntax for deleting the public GPG of the user by using its “uid”:

$ gpg --delete-key [uid]

To delete the private of the “Liza”, we will write out the following command in the terminal:

$ gpg --delete-key liza

Enter “y” to confirm the deletion process, and you are all done!

Next, list out the public and private keys for the verification purpose:

$ gpg --list-keys

$ gpg --list-secret-keys

Now, we will demonstrate the GPG key deletion method using a user’s Key ID. To delete a user’s private GPG key, we will follow the below-given syntax:

$ gpg --delete-secret-key [Key_ID]

For instance, to delete the private GPG key of “john”, we will add its Key ID in the “gpg” command in the following way:

$ gpg --delete-secret-key 1B61835F2E64E1C62A9A4725F88BAB439C28653E

Click on the “Delete key” button to confirm that you want to delete the secret key of the mentioned user permanently:

The error-free output indicates that John’s private key is deleted. Now, to delete the public key, we will execute the “gpg –delete-key” command with its Key ID:

$ gpg --delete-key 1B61835F2E64E1C62A9A4725F88BAB439C28653E

Check out the list of public and private keys to confirm whether the deletion operation worked or not:

$ gpg --list-keys

How to delete GPG keys of multiple users in Linux

The “gpg” command also permits you to delete the GPG keys of multiple users at once, and it follows the order for the deletion in which the uids or the Key IDs are added.

Before moving towards, we will list out the private and public GPG keys and note down the uid, the private and Key ID of the “sharqa” and “fred” user:

$ gpg --list-secret-keys

Using the Linux terminal, you can delete the GPG keys of the multiple users by adding their uids in the gpg command:

$ gpg --delete-secret-key [uid1] [uid2]

For instance, we will execute the following command for deleting private keys of “sharqa” and “fred” with the help of their “uids”:

$ gpg --delete-secret-key sharqa fred

The gpg command will firstly delete the secret key of the user “sharqa,” and then it will remove the fred’s key from the secret keyring:

Lastly, we will delete the public keys as well:

$ gpg --delete-key sharqa fred

Similarly, if you want to delete the private GPG key of multiple users by using their Key IDs, then have a look at the below-given syntax:

$ gpg --delete-secret-key [Key_ID1] [Key_ID2]…

For instance, to delete the private GPG key of “sharqa” and “fred”, we will add their Key IDs in the following way:

$ gpg --delete-secret-key BCD7B50F5AD69B6B931E85E32E6E1BEE9855E1CE B7537CEAA0C89170A6AB4065F6F040E15E66CEA2

Note that we have added the Key ID of the “sharqa” first; that’s why its private key is going to delete before fred’s:

Now, hit the “Delete Key” button for the confirmation:

At this point, the private key of the user “sharqa” is deleted, and the gpg command will move towards the deletion of fred’s key:

The above-given error-free output indicates that the private keys of both users are now deleted. In the next step, we will write the following command for deleting the sharqa’s and fred’s public keys from the public keyring:

$ gpg --delete-key BCD7B50F5AD69B6B931E85E32E6E1BEE9855E1CE B7537CEAA0C89170A6AB4065F6F040E15E66CEA2

Conclusion

As a Linux user, your GPG keyring comprises a long list after importing the keys of the other users to your system. You can delete the unnecessary GPG keys by using the “gpg” command. The “–delete-key” option is added in the “gpg” command for deleting the public key, whereas the “–delete-secret-key” option assists in the secret or private key deletion. This write-up demonstrated how to delete GPG keys of single and multiple users. Moreover, the procedures of using “uid” and the “Key ID” for deleting the private and public keys are also provided.

About the author

Sharqa Hameed

I am a Linux enthusiast, I love to read Every Linux blog on the internet. I hold masters degree in computer science and am passionate about learning and teaching.