William P. Barr is stepping down as attorney general, ending a controversial tenure in which critics say he repeatedly used the Justice Department to aid President Trump’s allies, only to have Trump turn on him when he did not announce investigations of political foes and disputed White House claims of widespread election fraud.

Trump revealed the move on Twitter, writing that he and Barr had a “nice meeting” at the White House, and that Barr would “be leaving just before Christmas to spend the holidays with his family.” Trump also posted a copy of Barr’s resignation letter, in which Barr indicated he had just provided the president an “update” on the department’s review of voter fraud allegations.

Barr’s letter said he was “greatly honored” to have served in the administration, and heaped praise on Trump for his “many successes and unprecedented achievements.” Trump on Twitter claimed of Barr, “Our relationship has been a very good one, he has done an outstanding job!”

But in public and behind the scenes, the men’s relationship had significantly soured on a number of fronts, with one person saying the pair had barely spoken directly in recent months. A senior White House official, who like others spoke on the condition of anonymity to discuss a sensitive matter, insisted that Barr resigned of his own accord.

“He was not fired,” the official said.

Trump had expressed frustration with Barr in recent days because Barr did not reveal before the election that Hunter Biden, President-elect Joe Biden’s son, was under investigation by the Justice Department. Trump told Fox News this weekend that Barr “should have stepped up” on the matter.

“All he had to do is say an investigation’s going on,” Trump said, adding later, “When you affect an election, Bill Barr, frankly, did the wrong thing.”

Before that, Trump had been fuming at his attorney general for not taking public, pre-election steps in a separate probe, run by Connecticut U.S. Attorney John Durham, that is examining the FBI’s investigation of Trump’s 2016 campaign. That anger intensified recently when Barr publicly broke with the president and declared he had “not seen fraud on a scale that could have effected a different outcome in the election,” while virtually simultaneously revealing he secretly had made Durham a special counsel in October.

The move gave Durham enhanced legal and political protection to continue his work, but because it was not announced until after the election, had no political benefit for Trump. Trump had wanted some kind of public report, people familiar with the matter have previously told The Washington Post.

On Wednesday, Hunter Biden himself revealed he was under investigation by federal prosecutors in Delaware for possible tax crimes. Since 2018, according to people familiar with the matter, federal agents have been exploring whether Hunter Biden failed to report income from China-related business deals — a politically explosive probe that is likely to challenge the Justice Department in the incoming administration.

That the probe had been largely kept under wraps, though, vexed Trump, and worsened his relationship with Barr. Last week, 27 House Republicans wrote to Trump urging him to instruct Barr to name a special counsel to investigate election fraud, because, they charged, the Justice Department was not taking the allegations seriously.

Barr’s letter indicated that his last day would be Dec. 23. Trump wrote on Twitter that Barr would be replaced on an acting basis by the Justice Department’s No. 2 official, Jeffrey A. Rosen, and that Rosen would be replaced by his own top deputy, Richard Donoghue.

Barr had told associates in recent weeks that he might leave his post before the Trump administration came to an end — a possibility he had been considering since shortly after Election Day, when it became clear that Biden had won.

But after news reports emerged of that consideration, a Justice Department official pushed back, saying, “The AG’s intention is and has been to stay as long as the president needs him.” Friends of Barr had also said previously that he wanted to stay on if Trump won a second term.

A White House official said that Trump discussed firing Barr as recently as Friday but that it was unclear whether the president was serious. White House Chief of Staff Mark Meadows had been critical toward Barr to the president on several occasions, officials said.

But the president’s criticisms of Barr in recent days drew some pushback, even from typically fervent GOP allies. In an interview late last week, Sen. Lindsey O. Graham (R-S.C.), chairman of the Senate Judiciary Committee, said he had faith in Barr, even though he knew Trump was frustrated.

“I can assure you Barr is looking at credible accusations of misconduct,” Graham said. “I have confidence in the attorney general. I trust the system. It may not give you the answer you want.”

After Barr’s resignation was public, Graham said in a statement, “I have nothing but total respect and admiration for the job done by William Barr as Attorney General of the United States.”

Senate Majority Leader Mitch McConnell (R-Ky.) also told other senators he continued to support Barr.

Inside and outside the Justice Department, Barr’s critics were not sorry to see him go. Noah Bookbinder, executive director of Citizens for Responsibility and Ethics in Washington, said, “Good riddance,” arguing that the attorney general “has done tremendous damage to the Department of Justice and to the American people’s very faith in our justice system.”

Rep. Jerrold Nadler (D-N.Y.), chairman of the House Judiciary Committee, said in a statement that the attorney general “was willing to do the President’s bidding on every front but one. Barr refused to play along with President Trump’s nonsensical claims to have won the election. He is now out as Attorney General one month early.”

Nadler added that Biden’s pick for attorney general, whoever it is, “will have a tremendous amount of work to do to repair the integrity of the Department of Justice.”

Barr was confirmed as attorney general in February 2019, taking over as Trump’s second attorney general after the president ousted former senator Jeff Sessions of Alabama from the position. Barr came on just as special counsel Robert S. Mueller III’s investigation into whether the Trump campaign coordinated with Russia to influence the 2016 election was winding down. And when Mueller delivered to the attorney general his final investigative report, Barr was thrust into one of his first major controversies.

Before releasing Mueller’s report publicly, Barr described to Congress what he termed the special counsel’s “principal conclusions” — that there was not evidence to demonstrate a conspiracy between the Trump campaign and Russia, and that Mueller had reached no determination on whether Trump had sought to obstruct justice. Barr further wrote that he and the deputy attorney general, Rod J. Rosenstein, had reviewed the case themselves and determined that obstruction charges were not warranted.

That description, which belied far more serious evidence against Trump in Mueller’s full report, so infuriated the special counsel team that Mueller wrote a letter to Barr complaining that he “did not fully capture the context, nature, and substance” of the investigation. But even when Barr ultimately released the full report, he did so only after holding a news conference and essentially repeating Trump’s long-held talking point that there had been no collusion and no obstruction.

In his resignation letter, Barr doubled down on his attacks on the Russia probe, writing that Trump’s political opponents had launched an effort “to cripple, if not oust, your Administration with frenzied and baseless accusations of collusion with Russia.”

“Few could have weathered these attacks, much less forge ahead with a positive program for the country,” Barr wrote, citing the strength of the U.S. economy and military and peace deals brokered in the Middle East.

Responding to Barr’s letter, David Laufman, a former Justice Department counterintelligence official involved in the Russia probe, wrote on Twitter, “Even in the act of resigning, Barr soils his tenure as AG further.”

Critics say Barr had long sought to undermine Mueller’s work, to the benefit of Trump and his friends. Barr appointed Durham to review the Russia investigation for possible impropriety, and he tapped a U.S. attorney in St. Louis, Jeff Jensen, to particularly examine a case Mueller brought against former national security adviser Michael Flynn.

Barr’s Justice Department ultimately moved to throw out that case — even though Flynn already had pleaded guilty and was awaiting sentencing. Barr also personally intervened in a case brought by Mueller against longtime Trump friend Roger Stone, reducing the sentencing recommendation offered by career prosecutors.

Barr’s actions sparked intense controversy, with thousands of Justice Department alumni calling on him to resign in open letters. Inside the Justice Department, too, morale plummeted, with many career employees feeling they were not supported by the man at the top.

Barr, though, defended his interventions — saying the facts of each case required them — and in September famously launched a broadside against the department’s career prosecutors, asserting they too often inject themselves into politics and go “headhunting” for high-profile targets.

Even outside the Russia investigation, Barr was one of Trump’s most loyal and effective Cabinet secretaries, often emphasizing the commander in chief’s public talking points and using the imprimatur of the Justice Department to defend him. Barr echoed some of the president’s attacks on mass mail-in voting, making exaggerated claims of possible fraud. He orchestrated a massive show of force against racial justice demonstrators in Washington, D.C., even personally asking for the removal of protesters around Lafayette Square just before Trump walked across the area to pose for a photo in front of a nearby church.

His department also intervened in the defamation lawsuit brought by a woman who alleges that Trump raped her years ago, trying to substitute the U.S. government — rather than Trump himself — as the defendant in the case.

Barr’s defenders note that he has long held an expansive view of presidential power, and that what some see as his serving Trump’s interests is more accurately described as his trying to bolster the office of the president. Barr previously served as attorney general in the George H.W. Bush administration, and he has long advocated for a strong executive branch.

Throughout his term, though, Barr did not always make moves that pleased Trump. Although the Justice Department investigated former FBI director James B. Comey and former deputy director Andrew McCabe — both frequent targets of Trump’s ire — for possible crimes, officials ultimately closed both cases without bringing charges. Durham, too, made no public moves in the weeks before the November election, frustrating Trump, who had hoped the prosecutor would vindicate his attacks on the Russia probe and prosecute his political foes.