He bought shares in a company then he found out that it deals with riba
You have to sell them and get rid of them, and there is no sin on you in that, because you did not know that they are haraam.
Shaykh Dr. Muhammad ibn Sa’ood al-‘Usaymi was asked about that and he replied:
Praise be to Allaah and blessings and peace be upon the Messenger of Allaah and his family and companions.
If a person deals with a permissible company which then changes into an impermissible company, or he buys it and did not realize that it was haraam, then he comes to know that it is haraam, or he buys it on the basis of a fatwa from scholars who say it is permissible to deal with mixed companies (i.e., those that deal with both halaal and haraam things), then he finds out that this is not permissible, then the ruling is as follows: He has to sell whatever he owns of shares in that company, but he does not have to purify anything; if selling it will cause a loss, then he may wait until he can regain his capital. End quote.