Search code examples
pythongnupggpgme

how to use in python gpgme advanced (low-level) functions to delete a key?


I want to delete a key from my keystore, but the main module (gpg) doesn't provide any function to do it (or I missed it). Just to revoke key UIDs and that's not what I want.

I found that the function gpgme_op_delete_ext (gpgme_ctx_t ctx, const gpgme_key_t key, unsigned int flags) is avaiable through the gpg.gpgme module. But I don't how to use it because the GPG context I have is from the main python module and not of the required "low-level" type the function takes. And so I guess is my key I get from the get_key function of my context.

So, how can I convert my Context object from the main module and the key I get from it to feed the gpgme_op_delete_ext function to delete the said key?


Solution

  • I found that the GPG Context object has a wrapped attribut which is the data type gpgme_op_delete_ext requires. And for the keys, the object returned by get_key is accepted by the function.

    So I can call it like this for instance:

    context = gpg.Context()
    key = context.get_key(fingerprint)
    gpgme_op_delete_ext(context.wrapped, key, GPGME_DELETE_FORCE)