How to delete gpg secret keys by force, without fingerprint?

Solution 1:

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'

Solution 2:

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

Solution 3:

I combined the answers by Gilles and Dolanor into this one liner that is useful in case you want to delete a specific key:

gpg --fingerprint --with-colons ${GPG_KEY} |\
    grep "^fpr" |\
    sed -n 's/^fpr:::::::::\([[:alnum:]]\+\):/\1/p' |\
    xargs gpg --batch --delete-secret-keys

Not answering the original question, but might be useful for someone else.

Solution 4:

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