In a humiliating failure, US President Donald Trump and GOP leaders pulled their bill to repeal “Obamacare” off the House floor Friday when it became clear it would fail badly — after seven years of nonstop railing against the law. Democrats said Americans can “breathe a sigh of relief.” Trump said the current law was imploding “and soon will explode.”

Thwarted by two factions of fellow Republicans, from the centre and far right, House Speaker Paul Ryan said President Barack Obama’s health care law, the GOP’s No. 1 target in the new Trump administration, will remain in place “for the foreseeable future.”

It was a stunning defeat for the new president after he had demanded House Republicans delay no longer and vote on the legislation Friday, pass or fail.

His gamble failed. Instead Trump, who campaigned as a master deal-maker and claimed that he alone could fix the nation’s health care system, saw his ultimatum rejected by Republican lawmakers who made clear they answer to their own voters, not to the president.

Watch: a humiliating defeat for Trump

He “never said repeal and replace it in 64 days,” a dejected but still combative Trump said at the White House, though he repeatedly shouted during the presidential campaign that it was going down on Day One of his term.

The bill was withdrawn just minutes before the House vote was to occur, and lawmaker said there were no plans to revisit the issue. Republicans will try to move ahead on other agenda items, including overhauling the tax code, though the failure on the health bill can only make whatever comes next immeasurably harder.

Trump pinned the blame on Democrats.

After seven horrible years of ObamaCare (skyrocketing premiums & deductibles, bad healthcare), this is finally your chance for a great plan!

For his part, Ryan told reporters: “We came really close today but we came up short. ... This is a disappointing day for us.” He said the president has “really been fantastic.”

But when asked how Republicans could face voters after their failure to make good on years of promises, Ryan quietly said: “It’s a really good question. I wish I had a better answer for you.”

Last fall, Republicans used the issue to gain and keep control of the White House, Senate and House. During the previous years, they had cast dozens of votes to repeal Obama’s law in full or in part, but when they finally got the chance to pass a repeal version that actually had a chance to become law, they couldn’t deliver.

Democrats could hardly contain their satisfaction.

“Today is a great day for our country, what happened on the floor is a victory for the American people,” said House Minority Leader Nancy Pelosi, who as speaker herself helped Obama pass the Affordable Care Act in the first place. “Let’s just for a moment breathe a sigh of relief for the American people.”

The outcome leaves both Ryan and Trump weakened politically.

For the president, this piles a big early congressional defeat onto the continuing inquiries into his presidential campaign’s Russia connections and his unfounded wiretapping allegations against Obama.

Ryan was not able to corral the House Freedom Caucus, the restive band of conservatives that ousted the previous speaker. Those Republicans wanted the bill to go much further, while some GOP moderates felt it went too far.

Instead of picking up support as Friday wore on, the bill went the other direction, with several key lawmakers coming out in opposition. Rep. Rodney Frelinghuysen of New Jersey, chairman of a major committee, Appropriations, said the bill would raise costs unacceptably on his constituents.

The defections raised the possibility that the bill would not only lose on the floor, but lose big.

The GOP bill would have eliminated the Obama statute’s unpopular fines on people who do not obtain coverage and would also have removed the often-generous subsidies for those who purchase insurance.

Republican tax credits would have been based on age, not income like Obama’s, and the tax boosts Obama imposed on higher-earning people and health care companies would have been repealed. The bill would have ended Obama’s Medicaid expansion and trimmed future federal financing for the federal-state programme, letting states impose work requirements on some of the 70 million beneficiaries.

The nonpartisan Congressional Budget Office said the Republican bill would have resulted in 24 million additional uninsured people in a decade and lead to higher out-of-pocket medical costs for many lower-income and people just shy of age 65 when they would become eligible for Medicare. The bill would have blocked federal payments for a year to Planned Parenthood.

Republicans had never built a constituency for the legislation, and in the end the nearly uniform opposition from hospitals, doctors, nurses, the AARP, consumer groups and others weighed heavily with many members. On the other side, conservative groups including the Koch outfit argued the legislation did not go far enough in uprooting Obamacare.

Ryan made his announcement to lawmakers at a very brief meeting, he was greeted by a standing ovation in recognition of the support he still enjoys from many lawmakers.

When the gathering broke up, Rep. Greg Walden of Oregon, chairman of the Energy and Commerce Committee that helped write the bill, told reporters: “”We gave it our best shot. That’s it. It’s done. D-O-N-E done. This bill is dead.“

■ US stocks flirted with sharp losses but managed a mixed finish after Republicans cancelled the vote on their health care bill. Hospital stocks soared in response, while companies that stand to benefit from other Trump proposals faltered.

For the second day in a row, stocks started higher and wilted as it became clear the health care bill was in trouble. The Dow Jones industrial average plunged as much as 126 points in afternoon trading on reports of the bill’s impending failure, although Wall Street cut its losses after the vote was cancelled. Consumer-focused companies like Nike, Starbucks and clothing company PVH rose.

The Standard & Poor’s 500 index finished down 1.98 points, or 0.1 per cent, at 2,343.98. The Dow lost 59.86 points, or 0.3 per cent, to 20,596.72 as Goldman Sachs and Boeing sank.

Trading was relatively light as investors waited for answers about the state of Trump’s business-friendly agenda. That may have contributed to the big fluctuations.