Attorney General Jeff Sessions resigned on Wednesday at President Trump’s request, ending the tenure of a beleaguered loyalist whose relationship with the president was ruined when Sessions recused himself from control of the investigation of Russian interference in the 2016 presidential campaign.

In a letter to Trump, Sessions wrote that he had been “honored to serve as Attorney General” and had “worked to implement the law enforcement agenda based on the rule of law that formed a central part of your campaign for the presidency.” Trump tweeted that Sessions would be replaced on an acting basis by Matthew G. Whitaker, who had been serving as Sessions’s chief of staff.

“We thank Attorney General Jeff Sessions for his service, and wish him well!” Trump tweeted. “A permanent replacement will be nominated at a later date.”

A Justice Department official said Whitaker would assume authority over the special counsel probe into Russian interference in the 2016 presidential election — though his role will be subject to the normal review process for conflicts. Because Sessions was recused, the special counsel probe had been overseen by Deputy Attorney General Rod J. Rosenstein, who also has had strained relations with Trump, but is considered safe in his position for the moment. Rosenstein went to the White House on Wednesday afternoon for what an official said was a pre-scheduled meeting.

Though Sessions’s removal was long expected, the installation of Whitaker sparked fears that the president might be trying to exert control over the special counsel investigation led by Robert S. Mueller III.

A legal commentator before he came into the Justice Department, Whitaker had mused publicly about how a Sessions replacement might reduce Mueller’s budget “so low that his investigation grinds to almost a halt.” He also wrote in an August 2017 column that Mueller had “come up to a red line in the Russia 2016 election-meddling investigation that he is dangerously close to crossing,” after CNN reported that the special counsel could be looking into Trump and his associates’ financial ties to Russia. Trump has told advisers that Whitaker is loyal and would not have recused himself from the investigation, current and former White House officials said.

Whitaker said in a statement: “It is a true honor that the President has confidence in my ability to lead the Department of Justice as Acting Attorney General. I am committed to leading a fair Department with the highest ethical standards, that upholds the rule of law, and seeks justice for all Americans.”

Democrats and others issued statements Wednesday urging that Mueller be left to do his work and vowing to investigate whether Sessions’s ouster was meant to interfere with the special counsel. Come January, Democrats will have subpoena power, having won control of the House in Tuesday’s midterm elections.

“Congress must now investigate the real reason for this termination, confirm that Acting Attorney General Matthew Whitaker is recused from all aspects of the Special Counsel’s probe, and ensure that the Department of Justice safeguards the integrity of the Mueller investigation,” Rep. Elijah E. Cummings (Md.), the ranking Democrat on the House Oversight Committee, said in a statement.

Sen. Mark R. Warner (Va.), the ranking Democrat on the Senate Intelligence Committee, said in a statement: “No one is above the law and any effort to interfere with the Special Counsel’s investigation would be a gross abuse of power by the President. While the President may have the authority to replace the Attorney General, this must not be the first step in an attempt to impede, obstruct or end the Mueller investigation.”

Senator-elect Mitt Romney (R-Utah) tweeted that it was “imperative” Mueller’s work be allowed to continue “unimpeded.” Sen. Lamar Alexander (R-Tenn.) said that “no new Attorney General can be confirmed who will stop that investigation.”

A person close to Sessions, who spoke on the condition of anonymity to be frank, said the attorney general shared the president’s frustration with the pace of the Russia inquiry and wished that it had been completed. But Sessions also thought that by staying in the job, he had protected the investigation’s integrity, the person said. In the long run, Sessions is convinced that the country will be better served by the investigation proceeding naturally, as the findings will be more credible to the American public, the person said.

Justice Department officials had been bracing for Sessions’s ouster. Sessions told confidants earlier this week that he expected Trump to fire him or push him out soon after the midterm elections, and friends urged him to quit and consider running again for a Senate seat in Alabama. A person close to Sessions said he is considering doing so. Still, some senior leaders at the Justice Department were shocked to hear the news Wednesday.

Sessions received a phone call Wednesday morning from White House Chief of Staff John F. Kelly — before the president held a news conference to discuss the results of the midterm elections — telling him the president wanted Sessions to resign, an administration official said.

Sessions sought to stay on the job at least until the end of the week, according to people familiar with the discussion. Kelly rejected that suggestion, insisting Wednesday would be Sessions’s last day, the people said. Sessions canceled meetings and scheduled one for later in the day, where he would say goodbye to his staff.

A White House official said that Trump had been held at bay until after the elections in demanding Sessions’s resignation but that the president talked eagerly about ousting his attorney general as soon as the votes were tallied. Even as election results were coming in, Trump complained about Sessions and said he hoped Republicans would win a large enough margin in the Senate that he could fire the attorney general quickly, a person familiar with the matter said.

Another person said other Cabinet officials also were in jeopardy.

In a matter of hours, Sessions was out and Whitaker was in. About 150 employees gathered in the Justice Department courtyard Wednesday evening to bid farewell to the attorney general. Sessions walked out of the building flanked by Rosenstein; Solicitor General Noel Francisco; Jody Hunt, his former chief of staff; and Whitaker. He shook hands with the four, waved to the crowd and gave a thumbs up before climbing into a black government SUV and leaving.

The White House official said the president liked Whitaker, who was a “backslapping, football kind of guy” who had briefed Trump on many occasions because the president preferred not to talk to Sessions.

“The president never wanted to see Jeff. So a lot of other people at DOJ got to see the president,” the person said.

Whitaker, a former U.S. attorney who ran an unsuccessful campaign for a Senate seat in Iowa, played college football at the University of Iowa. In 2014, he chaired the campaign of Sam Clovis, a Republican candidate for Iowa state treasurer. That might present another potential ethics complication for Whitaker’s supervision of the special counsel; Clovis went on to work as a Trump campaign adviser and has become a witness in Mueller’s investigation.

Justice Department officials said Whitaker will follow the regular process for reviewing possible ethical conflicts as he assumes the new job of the nation’s top law enforcement official. That process involves Justice Department ethics lawyers reviewing an official’s past work to see if there are any financial or personal conflicts that preclude the official from being involved in specific cases.

The Justice Department advises employees that “generally, an employee should seek advice from an ethics official before participating in any matter in which her impartiality could be questioned.” The department’s regulations prohibit a Justice Department employee, “without written authorization, from participating in a criminal investigation or prosecution if he has a personal or political relationship with any person or organization substantially involved in the conduct that is the subject of the investigation or prosecution.”

Two close Trump advisers said the president does not plan on keeping Whitaker permanently. Among those said to be under consideration for the job are Health and Human Services Secretary Alex Azar, former U.S. attorney general Bill Barr and former federal judges Janice Rogers Brown and J. Michael Luttig. An administration official said the president has also considered selecting another U.S. senator for the position, on the grounds that a lawmaker might have an easier confirmation, but so far GOP lawmakers have privately expressed little interest in the position.

Two other officials said former New Jersey governor Chris Christie (R) might be under consideration. One said Christie has talked with the president about the job.

“I don’t see him staying,” one Trump aide said of Whitaker. “I think the president will be a lot more deliberate in interviewing potential replacements for Jeff Sessions.”

Sessions, 71, was the first U.S. senator to endorse Trump, and in many ways he had been the biggest supporter of the president’s policies on immigration, crime and law enforcement.

But all of those areas of agreement were overshadowed by the Russia investigation — specifically, Sessions’s recusal from the inquiry after it was revealed that he had met more than once with the Russian ambassador to the United States during the 2016 campaign, even though he had said during his confirmation hearing that he had not met with any Russians.

Trump has never forgiven Sessions for his recusal, which he regarded as an act of disloyalty that denied him the protection he thought he deserved from his attorney general. “I don’t have an attorney general,” he said in September.

Privately, Trump has derided Sessions as “Mr. Magoo,” a cartoon character who is elderly, myopic and bumbling, according to people with whom the president has spoken.

Trump also had repeatedly threatened or demanded Sessions’s ouster behind closed doors, only to be convinced by aides that removing him could provoke a political crisis within the Republican Party. Former White House counsel Donald McGahn urged the president to keep Sessions in the job until the Mueller probe was over, current and former White House officials said.

After an early confrontation, Sessions gave Trump a resignation letter and let him hold onto it. The move deeply concerned White House aides, including then-Chief of Staff Reince Priebus, who told Sessions that Trump would use the letter to manipulate him.

“You have to get that letter back,” Priebus told Sessions, according to people familiar with the conversation. Trump ultimately returned the missive with a short handwritten note about how he was not accepting it.

As the president railed against Sessions through early and mid-2017, Republican senators publicly and privately defended the attorney general. But in recent months, some of Sessions’s most prominent defenders, including Sens. Lindsey O. Graham (R-S.C.) and Charles E. Grassley (R-Iowa), said they were open to a new attorney general after the midterm elections.

Grassley said in a statement Wednesday that Sessions’s “leadership, integrity and dedication to our country as Attorney General are admirable and commendable.” He added of Whitaker: “The Justice Department is in good hands during this time of transition.”

Despite the tension with the White House, Sessions had described the position of top law enforcement officer as his dream job, and he pursued his conservative agenda with gusto. But he also had to live with sometimes humiliating attacks from a president he couldn’t seem to please and the suspicions of career staff members who feared the politicization of a Justice Department that prides itself on its independence.

Department veterans have expressed concerns that Trump’s repeated public attacks on Sessions, the Justice Department and the FBI could cause lasting damage to federal law enforcement.

Mueller is looking into Trump’s statements seeking to fire Sessions or force his resignation in an effort to determine whether those acts are part of a pattern of attempted obstruction of justice, according to people close to the investigation.

Earlier this year, Mueller’s team questioned witnesses about Trump’s private comments and state of mind in late July and early August of last year, around the time he belittled his “beleaguered” attorney general on Twitter, these people said. The questions sought to determine whether the president’s goal was to oust Sessions so he could replace him with someone who would take control of the investigation, these people said.

Sessions usuallydid not respond to the president’s criticism — including in his resignation letter, which thanked Trump for the “opportunity” to serve as attorney general — but he has at times pushed back.

After one particularly blistering tweet in February — in which the president said Sessions’s actions were “DISGRACEFUL!” — he issued a statement: “As long as I am the Attorney General, I will continue to discharge my duties with integrity and honor, and this Department will continue to do its work in a fair and impartial manner according to the law and Constitution.’’