ON APRIL 24th, Donald Trump suffered a third setback to his effort to undo Deferred Action for Childhood Arrivals (DACA). The programme, introduced by Barack Obama in 2012, shields undocumented immigrants brought to America as children from deportation. Twice in recent months, federal judges in California and New York have concluded that Mr Trump’s decision in September 2017 to ditch DACA was illegal. Both rulings effectively wiped out the end-date of March 5th the Trump administration set in the autumn.

The decision this week from Judge John Bates goes further than either of the previous injunctions. As well as requiring Mr Trump to process renewals of existing DACA enrollees, Judge Bates orders the president to invite and approve first-time applications from so-called Dreamers who have yet to register for the programme.

Judge Bates grounded his ruling in the Administrative Procedure Act (APA), a federal law that sets protocols for agencies when they adopt a new rule or abandon an old one. He noted that “[t]he APA generally requires notice-and-comment procedures”—an opportunity for the public to weigh in on a change—”whenever an agency creates, amends, or repeals a rule”, but agreed with the government that Mr Trump’s DACA announcement did not in fact require this due to an exception in the APA. Nevertheless, Judge Bates chided the government for failing to provide a sound justification for the change. In the words of the APA, agencies may not alter rules for reasons that are “arbitrary and capricious”.

Neither of Mr Trump’s stated reasons for dropping DACA, the judge found, passed muster. First, in the announcement in September 2017, Jeff Sessions, the attorney-general, said that Mr Obama’s programme was unlawful and unconstitutional. But this conclusion is “erroneous”, Judge Bates found, and was based on “scant legal reasoning”. The meagre justification was “particularly egregious”, he wrote, “in light of the reliance interests involved”. In undoing the protections, the government “made no mention of the fact that DACA had been in place for five years and had engendered the reliance of hundreds of thousands of beneficiaries, many of whom had structured their education, employment, and other life activities on the assumption that they would be able to renew their DACA benefits”. Mr Trump’s second justification for scrapping DACA—a fear of lawsuits—was equally weak in Judge Bates’s view. Worries over “threatened litigation over DACA’s validity...was so implausible that it fails even under the deferential arbitrary and capricious standard”.

So what is to be done? Judge Bates takes what he calls an “intermediate course”: halting the rescission of DACA but staying that order for 90 days. In the intervening three months, the executive branch “may reissue a memorandum rescinding DACA, this time providing a fuller explanation for the determination that the program lacks statutory and constitutional authority”. If such a memo is presented to the court, and Judge Bates finds it less laughable than the 2017 memo, Mr Trump may go ahead with his wish to rescind protections for young immigrants. But barring that, “the original DACA programme will be restored in full”; the government will be ordered to “resume accepting initial DACA applications and applications for advanced parole”.

This is a silver lining for the Trump administration. Yes, Judge Bates criticised the rollback as “virtually unexplained” and obliquely scolded the Trump administration for being callous. But in delaying the implementation of his order, he suggested one example of how DACA could be legitimately ended. It is no good to say that a worry about being sued, by itself, justifies scrapping the programme. But the government “could conclude”, he wrote, “that the costs of defending the policy in court would outweigh its benefits to the public, or that the negative publicity that would surround the litigation would undermine the policy’s effectiveness”. This cost-benefit analysis may be an acceptable reason to end the policy, as it would make “the decision to rescind…’discretionary’ in a meaningful sense”.

How will the Trump administration react? In previous legal setbacks, Mr Trump has taken to Twitter to express his displeasure with judges meddling with his policies. At times he has attacked judges personally, calling one jurist who ruled against his first travel-ban order a “so-called judge”. But an ad hominem attack on Judge Bates will be hard to sustain: he was appointed to his seat by George W. Bush in 2001 and, from 1995 to 1997, served under Kenneth Starr in the investigation that led to Bill Clinton’s impeachment. He is no leftie. But he pulled no punches in an opinion that is sure to enrage a president who rankles at judicial oversight.