How to Delete GPG Key in Linux: A Complete Guide

Delete GPG Keys

Table of Contents

Get up to 50% off now

Become a partner with CyberPanel and gain access to an incredible offer of up to 50% off on CyberPanel add-ons. Plus, as a partner, you’ll also benefit from comprehensive marketing support and a whole lot more. Join us on this journey today!

Data breaches and other malicious activities like ID theft or decryption of sensitive data are extremely common, which is why you need a dependable manner of encrypting data for business continuity. 

A highly reliable encryption method prevents malicious actors from impersonating you in digital transactions. Here GPG keys, also known as GnuPG keys come into play. They play an essential role in encrypting, decrypting, and signing data to ensure that communications and files remain confidential and untampered. 

However, at times, you might need to delete GPG key due to it being compromised or simply no longer in use. This guide will walk you through all the ways there are to delete that news. 

Types of GPG Keys

There are five basic types of GPG keys that are used: 

  1. Public Keys

These keys are used to encrypt messages or verify digital signatures. As they are public keys, these keys are shared with others for secure communication. 

  1. Private Keys

Private GPG keys are used to decrypt messages or create digital signatures. Since they are private keys, they must be kept secret and secure. 

Tech Delivered to Your Inbox!

Get exclusive access to all things tech-savvy, and be the first to receive 

the latest updates directly in your inbox.

  1. Subkeys

Subkeys are associated with a primary key but used for specific tasks like encryption, signing, or authentication. They also help with key management and adds flexibility. 

  1. Master Keys

Master keys are primary keys used to certify other subkeys to manage the trust network. They perform all the key operations but are often kept offline for security. 

  1. Revocation Certificates 

Revocation certificates are special keys that are used as backup to revoke compromised or obsolete keys. These keys also allow users to invalidate keys if needed. 

How to View GPG Keys on a Linux System

To view the GPG keys stored on your Linux system, you can use the GPG command-line tool. Here are the exact commands: 

  • To view public GPG keys, you can use the following command for a complete list with key ID, user ID, and other important details.


gpg –list-keys

Delete GPG Key - 1
  • To view all the private GPG keys that are stored on your system with all important information use the command: 

gpg –list-secret-keys

Delete GPG Key - 2

If your system does not have secret keys, you can create them using the following command:

gpg –full-generate-key

Delete GPG Key - 3

Here I have selected 1 RSA and RSA(default as follow-up key does not expire and provide the affirmation with Y.

Then it would ask for identity:

Delete GPG Key - 4


Tap on ok, when you’re sure about your information.
Delete GPG Key - 5

Enhance Your CyerPanel Experience Today!
Discover a world of enhanced features and show your support for our ongoing development with CyberPanel add-ons. Elevate your experience today!

Please provide the passphrase with eight characters and hit enter.

Delete GPG Key - 6
  • To view details key information for a specific GPG key, use the command: 

gpg –list-keys <key-id>

  • To view a list of GPG keys in machine-readable and script-readable format, use the command: 

gpg –list-keys –with-colons

  • To export a key to view all of its content, use the command: 

gpg –export –armor <key-id>

For private keys, replace –export with –export-secret-keys.

Reasons to Delete GPG Keys

There are multiple reasons as to why you would need to delete GPG key. 

  1. If your GPG keys are compromised or leaked, it should be deleted to prevent unauthorized access. 
  2. You would also need to delete GPG key if it is obsolete or expired or no longer in use. They should be removed instantly to maintain a clean keyring. 
  3. Unused or duplicate keys can clutter the keyring and should be deleted for organization. 
  4. When migrating to a new key management system, older keys might need to be deleted. 
  5. When transitioning to a new key management system, older keys might need to delete older keys. 

How to Delete a Specific GPG Key

Deleting keys in GPG is quite a straightforward process. Follow the commands below to delete GPG keys. 

  1. Identify the key that you want to delete by listing all the public keys. Use the command: 


gpg –list-keys

  • Proceed by listing all the secret keys as well: 

gpg –list-secret-keys

  1. If you want to delete a public key, you can do so using this command with the key ID or fingerprint. 

gpg –delete-key <key-id>

  • For example: 

gpg –delete-key ABCDEF1234567890

Delete GPG Key - 7
  1. If you want to delete a secret key, use the command: 

gpg –delete-secret-key <key-id>

  1. Confirm deletion using the prompts. 

Deleting All GPG Keys in Linux

If you want to delete the entire keyring, use these command: 

  1. Delete all public keys, using the command: 


gpg –list-keys | grep pub | awk ‘{print $2}’ | cut -d’/’ -f2 | xargs -n1 gpg –delete-key

  1. To delete all secret keys, use the command: 

gpg –list-secret-keys | grep sec | awk ‘{print $2}’ | cut -d’/’ -f2 | xargs -n1 gpg –delete-secret-key

Delete GPG Key - 8
  1. Clear the entire keyring with all the public and private keys associated with the GPG directory, using the command: 

rm -rf ~/.gnupg

This action clears all keys and resets the GPG configuration.

Conclusion 

Managing GPG keys effectively is a crucial step for maintaining security in your Linux organization. Whether you’re removing an obsolete key, deleting a compromised one, or clearing all keys for a fresh start, the process is straightforward with the right commands. Always double-check the keys you intend to delete and back up any critical data to avoid unintended consequences. By understanding how to delete GPG keys responsibly, you can ensure a secure and efficient key management system.

Frequently Asked Questions

1. Why would I need to delete a GPG key?

Common reasons include key compromise, expiration, redundancy, or transitioning to new key management systems.

2. How can I delete a specific GPG key?

Use the command gpg --delete-key <key-id> for public keys or gpg --delete-secret-key <key-id> for private keys.

3. How do I delete all GPG keys on my system?

You can clear all keys by deleting the .gnupg directory:
rm -rf ~/.gnupg

Marium Fahim
Hi! I am Marium, and I am a full-time content marketer fueled by an iced coffee. I mainly write about tech, and I absolutely love doing opinion-based pieces. Hit me up at [email protected].
Unlock Benefits

Become a Community Member

SIMPLIFY SETUP, MAXIMIZE EFFICIENCY!
Setting up CyberPanel is a breeze. We’ll handle the installation so you can concentrate on your website. Start now for a secure, stable, and blazing-fast performance!