The Food and Drug Administration on Wednesday asked companies to stop selling all forms of the heartburn drug Zantac, after concluding that a potential cancer-causing contaminant can build up in the drug when stored for long periods.

The agency also recommended that consumers who use over-the-counter forms of the drug, also known as ranitidine, stop taking it and that they should dispose of any tablets or liquid that they have. People who take prescription forms of the drug should speak with their doctors about other options before stopping treatment.

Most manufacturers withdrew their products from the market several months ago, after an outside pharmacy raised the alarm about the drug last year. Large pharmacy chains, including Walgreens, Rite Aid and CVS, had also removed all of the products from their shelves.

The F.D.A. said in September that the contaminant was a type of nitrosamine called N-nitrosodimethylamine, or NDMA, which is believed to be carcinogenic in humans and is found in a variety of products, including cured meats.