A federal judge in Texas has struck down the entire Affordable Care Act on the grounds that its mandate requiring people to buy health insurance, done away with by President Trump and the Republican Congress, is unconstitutional and the rest of the law cannot stand without it.

After sitting on a ruling for months, judge Reed O’Connor has given Trump and a group of Republican-led states exactly what they asked for, and then some: the invalidation of Obamacare.

The ruling, issued late on Friday and only one day before the end of the law’s annual open enrollment period, is not a model of constitutional or statutory analysis.

It’s instead a predictable exercise in motivated reasoning — drafted by a jurist with a history of ruling against policies and laws advanced by President Obama.

If O’Connor’s decision ultimately stands, about 17 million Americans will lose their health insurance.

That includes millions who gained coverage through the law’s expansion of Medicaid, and millions more who receive subsidized private insurance through the law’s online marketplaces.

Insurers will also no longer have to cover young adults up to age 26 under their parents’ plans; annual and lifetime limits on coverage will again be permitted; and there will be no cap on out-of-pocket costs.

Also gone will be the law’s popular protections for people with pre-existing conditions, which became a major talking point in the November midterm elections, as Democratic candidates constantly reminded voters that congressional Republicans had tried to repeal the law last year.

On Friday night, a spokeswoman for Xavier Becerra, the California attorney general, said California and the other defendant states would challenge the ruling with an appeal in the United States Court of Appeals for the Fifth Circuit in New Orleans.

“Today’s ruling is an assault on 133 million Americans with pre-existing conditions, on the 20 million Americans who rely on the A.C.A.’s consumer protections for health care, on America’s faithful progress toward affordable health care for all Americans,” Mr. Becerra said in a statement. “The A.C.A. has already survived more than 70 unsuccessful repeal attempts and withstood scrutiny in the Supreme Court.”

Trump, who has consistently sought the law’s repeal and has weakened it through regulatory changes, posted a response to the ruling on Twitter late Friday night:

As I predicted all along, Obamacare has been struck down as an UNCONSTITUTIONAL disaster! Now Congress must pass a STRONG law that provides GREAT healthcare and protects pre-existing conditions. Mitch and Nancy, get it done! — Donald J. Trump (@realDonaldTrump) December 15, 2018

The White House, in a separate statement, said: “We expect this ruling will be appealed to the Supreme Court. Pending the appeal process, the law remains in place.”

This all-out assault on health care is one reason Democrats did so well in the midterm elections, as voters rejected anti-Obamacare candidates at the polls.

They included several lawmakers who had gleefully voted for Trump’s tax bill less than a year earlier.

Except the tax bill did not invalidate the Affordable Care Act — it did away only with the penalty for not being insured.

Congress left the rest of the law intact.

Instead of respecting that legislative choice, Judge O’Connor proceeded to find all the operative provisions of the A.C.A. “inseverable” from the hollowed-out individual mandate.

The whole law must fall.

He gave the Texas-led challengers precisely what they wanted.

This partisan, activist ruling will likely not stand.

If it’s not reversed by the conservative United States Court of Appeals for the Fifth Circuit, then it’s off to the Supreme Court, where all five justices who, in 2012, already determined that the Affordable Care Act was constitutional will still be there.