'Remove private keys from Keychain from Terminal

I have a script that loads a user's identity into keychain. The script deletes old or duplicate certificates and imports new identities without issue. But the private keys are left behind from previous identities. I re-read "man security" but I can't seem to find a solution. Removing the users keychain in this case is not an option. Any advise?

I realize there is an similar question here: Remove private key from Mac OS X keychain using Terminal but the answer of "delete the Keychain" isn't possible in my case. I posted a question to that thread, but it was deleted by a moderator of some reason. So I'm opening a new question here.

Thank you!



Solution 1:[1]

The certificate and private key can be removed with the following command:

security delete-identity -Z {certificate-sha1} {keychain-path}

You may need to unlock the keychain first.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 BitByteDog