New York Mayor Michael R. Bloomberg canceled the city’s annual marathon Friday afternoon after being pummeled in the press, online and in neighborhood coffee shops for insisting the race should go on days after a devastating storm killed at least 40 residents and left hundreds of others with burned and flooded homes.

The mayor earlier had said the race should go forward Sunday and would bolster the local economy and buoy residents’ spirits. “This city is a city where we have to go on,” he said.

But that position was roundly criticized as selfish, unfeeling — even “asinine” by Staten Island Borough President James Moli­naro. Hundreds of the nearly 50,000 runners registered for the race dropped out after the storm, according to one race official.

Some longtime spectators of the event threatened to boycott the race, which starts in Staten Island, where 19 bodies have been found in the aftermath of the storm, and goes through Brooklyn and Queens, where blocks of homes are still flooded or have been burned to the ground. Many said going ahead with the race was unsafe, given that police officers, firefighters and ambulance workers were needed to rescue those injured in the storm.

In canceling the marathon, Bloomberg issued a joint statement with Mary Wittenberg, president and chief executive of the New York Road Runners, the nonprofit group that created the event, insisting that the race would not divert resources from the recovery effort.

“It is clear that it has become the source of controversy and division,’’ the statement said. “We would not want a cloud to hang over the race or its participants, and so we have decided to cancel it. We cannot allow a controversy over an athletic event — even one as meaningful as this — to distract attention away from all the critically important work that is being done to recover from the storm and get our city back on track.”

The reaction to the mayor’s turnaround was instantaneous.

“The people have spoken,’’ wrote one Twitter user.

“Score one for sanity,’’ said state Sen. Liz Krueger (D), parts of whose Manhattan district have been without power much of the week. She had argued that with four hospitals evacuating from Lower Manhattan, emergency workers couldn’t tend to runners with pulled hamstrings or heat exhaustion. “I am just delighted that people realized what I realized earlier: This was a bad idea at this time. I certainly hope that there is a way to reschedule when we’re all up and running, because New York City loves the marathon and loves tourism.’’

Many runners had already decided to pull out, saying they couldn’t stomach running past ravaged neighborhoods. Kristen Faughnan, 31, of Philadelphia, trained four months through the summer heat, running 750 miles, ignoring a nagging injury, to compete in her third New York City Marathon. But she said Friday that she decided not to enter the race when she heard that some hotels in New York City would be kicking out refugees from the storm to honor reservations made by runners.

“They have tents in Central Park for runners, and generators,” she said. “Those resources should go to people who really need them. I can’t have any part of that.’’

Kate Krimmel of Manhattan canceled her marathon registration Wednesday with a note to race officials, copied onto her Facebook page, demanding that the race water, energy gel and insulated poncho she would have received be donated to storm victims.

“It didn’t feel right to have a celebration, especially of our own accomplishments, so close to neighborhoods that had been decimated,’’ said Krimmel, whose in-laws in eastern Brooklyn lost their heating system after their basement flooded. “I just couldn’t imagine running up Fourth Avenue and having someone hand me water when [there’s] such a desperate need for [bottled] water.’’