In an interview on Tuesday for a coming book about the president’s immigration policies, Mr. Trump acknowledged the tumult among his top immigration officials.

“I do burn out on people, I do. If somebody’s not really great, I do,” the president said during the interview in the Oval Office. He did not directly address the turnover at Customs and Border Protection, but said he had made “good changes” in the people who run his immigration agencies.

“I understand what I want,” he said. “And we’re starting to get there.”

The White House directed Kevin McAleenan, the acting secretary of homeland security, to replace Mr. Sanders with Mr. Morgan after multiple White House officials expressed displeasure that Mr. Sanders was not being aggressive enough at the southwestern border, administration officials said. Mr. McAleenan complied, hoping to diminish friction with the White House, officials said.

Mr. McAleenan resisted an earlier push by Stephen Miller, the architect behind Mr. Trump’s immigration policy, to appoint Mr. Morgan as the head of Customs and Border Protection, according to current and former homeland security officials.

In 2017, shortly after he took office, Mr. Trump forced out Mr. Morgan as Border Patrol chief. But Mr. Morgan won the approval of the White House after backing the president’s aggressive policies on television. In one appearance on Fox News, Mr. Morgan said that when he looked into the eyes of detained migrant children, he saw a “soon-to-be MS-13 gang member.”

In a letter to Customs and Border Protection employees on Tuesday, Mr. Sanders confirmed his departure.

“Although I will leave it to you to determine whether I was successful,” Mr. Sanders, a former chief technology officer at the Transportation Security Administration who helped spearhead the agency’s PreCheck program, wrote, “I can unequivocally say that helping support the amazing men and women of C.B.P. has been the most fulfilling and satisfying opportunity of my career.”