If you hold a stock that was previously shariah compliant but has now fallen out of compliance, you have a couple of options:
Sell the stock immediately. This is the most conservative approach - as soon as you are alerted that a stock is now non-compliant, you can exit your position right away. This avoids directly benefiting from any impermissible income going forward.
Apply a grace period to give the stock a chance to come back into compliance. This is a common practice adopted by the shariah boards of various Islamic funds and index providers where they will allow a grace period before removing a stock, often one or two financial periods. You can take a similar approach and monitor the stock over the next few months, for example, to see if it rectifies the non-compliant factors. If it remains non-compliant after the grace period, then exit the position.
In either case, it is recommended to purify your investment during the time the stock was non-compliant. Purification involves donating a portion of the dividends or gains to charity in proportion to the percentage of non-compliant revenue. You can read more about the purification process here.
The decision between immediately selling or allowing a grace period depends on your personal preference. However you choose to proceed, the goal remains to avoid directly benefiting from non-compliant activities and maintain the integrity of your portfolio.