Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

It's accident generated a bulk of secret keys (without public key) in the GPG key ring, and I have written a script to delete those keys, but GPG doesn't allow me to do that:

$ gpg --batch --yes --delete-secret-keys KEYS
gpg: can't do this in batch mod
gpg: (unless you specify the key by fingerprint)

well I know what I'm doing and I know there may be duplicated key-id(s), but there seems no way to force to delete them, though the following doesn't work, too:

$ yes | gpg --delete-secret-keys KEYS

Any idea?

share|improve this question
up vote 2 down vote accepted

Use gpg --list-secret-keys --with-colons --fingerprint to generate the list of fingerprints in an easily parsable format. Grab the lines of the form fpr:::::::::xxxx: that correspond to the keys you want to delete, and pass the fingerprints (the xxxx) to gpg --batch --delete-secret-keys.

The following command generates all secret key fingerprints. Be sure to select only the ones you want to delete!

gpg --list-secret-keys --with-colons --fingerprint | sed -n 's/^fpr:::::::::\([[:alnum:]]\+\):/\1/p'
share|improve this answer
Great! I thought the fingerprint is only available in public keys. – Xiè Jìléi Aug 11 '10 at 9:47
Oops! I'm wrong, the fingerprint couldn't be fetched from, the command yeilds: gpg: key XXXXXXXX: secret key without public key - skipped gpg: error reading key: secret key not availble In fact, I'm just wondering why `yes | ...' doesn't work, and I think the only way is patch on gpg. Thanks – Xiè Jìléi Aug 11 '10 at 9:56

Export the keys you want to keep, delete the file and then recreate your ring.

share|improve this answer
I'd rather prefer to patch & recompile gpg. – Xiè Jìléi Aug 11 '10 at 9:30

I know this topic is old, and that the answer kinda looks like Gilles one, but I think it answers the question totally (since I had the same kind of problems as OP) :

for i in `gpg --with-colons --fingerprint | grep "^fpr" | cut -d: -f10`; do gpg --batch --delete-secret-keys "$i" ; done
share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.