I am getting this error:
CodeSign error: Certificate identity \'iPhone Developer: XXX\' appears more than once in the keychain. The codesign too
build...
Quoting an answer to a duplicate of this question, some of Xcode's bugs in this area can only be solved by restarting OS X.
In the end, I achieved success by deleting everything mentioned in any of the answers to this question, restarting, then manually installing the parts I needed (I did not trust/try the automatic profile/certificate download feature).
The other reason that xcode keeps installing your certificate is because you did not delete key, only certificate. You need to delete private key (and corresponding public key) as well as certificate in keychain otherwise xcode will reinstall the certificate.
Check here To remove development and distribution certificates from your keychain
-- EDIT --
Merge @Codezy's answer and @iphone007's comment.
if you want to mess with keychain access to remove the expired certificate, you may try this:
the expired certificate disappears together with the keychain. worked perfectly for me.
Deleting provisioning profiles in Organizer's library, additionally, you should delete iPhone developer certificate in Keychain Access. (The certificate's name is probably like "iPhone Developer: XXXXX ..." and indicated by red X sign, as expired)