Walmart will stop selling cigarettes and other tobacco products to shoppers younger than 21, making it the latest retailer to raise the minimum buying age amid government pressure.

The world’s largest retailer said Wednesday that it is also phasing out the sale of fruit- and dessert-flavored e-cigarettes at its 5,000 Walmart and Sam’s Club stores. The changes will take effect July 1.

“Preventing the sale of tobacco products to minors is critical,” John Scudder, Walmart’s chief ethics and compliance officer, wrote in a letter to the Food and Drug Administration on Wednesday. “Even a single sale of a tobacco product to a minor is one too many.”

Earlier this year, the FDA threatened to fine a dozen retailers, including Walmart, Kroger and Family Dollar, for illegally selling tobacco products to children younger than 18. The agency said its inspections found 17 percent of Walmart stores had sold tobacco to minors since 2010. (By comparison, 7-Eleven had a violation rate of 25 percent, compared with 41 percent at Marathon Petroleum.)

“These illegal sales must stop,” the FDA said in a letter to Walmart on April 5. “Violating the law . . . and paying associated fines and penalties, should not simply be viewed as a cost of doing business.”

After years of steady decline, the number of middle- and high-school students using tobacco products increased between 2017 and 2018, largely because of the growing popularity of e-cigarettes. One in five high school students said they used e-cigarettes last year, up from 12 percent the year before, according to the U.S. Centers for Disease Control and Prevention. Less popular forms of tobacco included cigarettes (used by 8 percent of students), smokeless tobacco (6 percent) and hookah (4 percent).

Overall, 4.9 million minors said they used tobacco products last year, a 36 percent increase from the year before, according to the CDC.

E-cigarettes have become big business for companies like Juul Labs, a San Francisco start-up that last year sold more than $1 billion worth of vaping devices and nicotine cartridges. In December, tobacco giant Altria paid nearly $13 billion for a 35 percent stake in the company, which accounts for about 30 percent of all e-cigarette sales.

Walmart’s announcement comes two weeks after Walgreens took similar steps to raise the minimum age to 21 for tobacco products. CVS, meanwhile, stopped selling tobacco products in its stores in 2014.

Although Walmart has more than 5,000 stores around the country, analysts said they did not think restricting 18-, 19- and 20-year-olds from buying tobacco products would have a discernible impact on sales.

“In the scheme of things, the impact will be quite negligible,” said Neil Saunders, managing director of research firm GlobalData Retail. “This was, to a degree, a necessary step for Walmart to show that it is selling tobacco products responsibly.”

Read more: