iisrsax509certificate2

Clean my MachineKeys folder by removing multiple RSA files without touching IIS ones


I'm currently running IIS on my server using an app instantiating certificates.

By doing this code, for instance :

X509Certificate2 myX509Certificate = new 
X509Certificate2(Convert.FromBase64String(byteArrayRawCertificate), passwordCertificate, 
X509KeyStorageFlags.Exportable | 
X509KeyStorageFlags.MachineKeySet | 
X509KeyStorageFlags.PersistKeySet);

The code works fine. But I encounter a problem on my computer, on the following folder :

C:\ProgramData\Microsoft\Crypto\RSA\MachineKeys

3KB RSA files keep on being added on that folder. For now, I have more than a million files like those ones :

enter image description here

I would like to delete those files, but :

Thanks in advance for your help.


Solution

  • We ended up with this same problem.... 42 GB of machine key files. So I wrote this powershell: RemoveMachineKeys.ps1. Took a while before it actually started deleting them, but once it did the script blazed through them pretty fast. I added protection from removing IIS machine keys.

    I could not use the above answers that depended upon which user created the keys, as these keys were being created in a web site and had the same created by user. I also did not want to care about the application pool name if I did not have to.