President Trump announced Saturday that White House Chief of Staff John F. Kelly would exit his post by the end of the year, capping the retired Marine general’s rocky tenure as the president’s top aide and portending a major personnel shake-up as Trump prepares to navigate a divided Congress and focuses on his reelection campaign.

White House officials said the two men had a private discussion Friday after months of mounting frustration on the part of the president about his chief of staff and nonstop speculation about Kelly’s future. Kelly is likely to be replaced by Nick Ayers, Vice President Pence’s chief of staff and an experienced campaign operative who possesses the political skills and network that Trump felt Kelly lacked.

But the departure of Kelly — a four-star general with battlefield experience and deep government know-how — deprives the West Wing of a seasoned leader who was seen by allies as a check on some of the president’s most reckless impulses.

The selection of Ayers was not final Saturday as the president attended the Army vs. Navy football game in Philadelphia, and the two men remained in negotiations about the amount of time Ayers would commit to serve, according to White House officials.

As he left the White House on Saturday, Trump told reporters that he would name a replacement in coming days, and that it may be on an interim basis.

“John Kelly will be leaving — I don’t know if I can say ‘retiring.’ But, he’s a great guy,” Trump said on the South Lawn of the White House as he prepared to board the Marine One presidential helicopter. “John Kelly will be leaving toward the end of the year, at the end of the year.”

Kelly’s tenure in the White House came with its successes and failures and underscored a bigger question: How much difference can any White House chief of staff make with the headstrong, impulsive and mercurial president, who often governs by impulse and tweet, is uninterested in reading lengthy documents and appears happiest at his raucous rallies?

Current and former aides say Kelly brought much-needed discipline to a dysfunctional West Wing by limiting the number of visitors to the Oval Office, curbing the flow of erroneous information from the president’s desk and limiting attendance at meetings to people who needed to be present.

He often talked the president out of his worst impulses, removed some of the president’s most contentious aides, including Omarosa Manigault Newman, Sebastian Gorka and Stephen K. Bannon, and provided the president necessary lessons in national security matters. Republicans in Congress and military officials saw Kelly as an essential steadying hand.

Kelly told others that among his biggest accomplishments was keeping the president from making rash military moves, such as removing troops from sensitive zones.

How many days has it been since a high-profile departure from the Trump administration?

“He was a force for order, clarity and good sense,” said House Speaker Paul D. Ryan (R-Wis.). “He is departing what is often a thankless job, but John Kelly has my eternal gratitude.”

Though Trump loyalists said Kelly tried to change the president too much, Kelly also drew derision internally for supporting the president’s rhetoric after last year’s deadly white-nationalist rally in Charlottesville and for mishandling the case of former staff secretary Rob Porter.

In one of his most memorable episodes, Kelly falsely attacked Rep. Frederica S. Wilson, a Florida Democrat who criticized the president. He also showed support for Confederate Gen. Robert E. Lee in White House meetings and supported the widely criticized family separation policy at the U.S.-Mexico border.

He also was unable to head off some of the president’s foreign policy blunders, and he often shared the president’s most hawkish impulses on immigration.

He was seen by some White House aides as duplicitous, presenting different narratives to different advisers.

The president resisted the perception that Kelly was controlling him, and the chief of staff eventually clashed so often with Jared Kushner and Ivanka Trump, the president’s family members and senior advisers, that the relationship became uncomfortable.

Kelly’s departure is anticlimactic, after months of the president’s musing about replacing him and complaining about his chief of staff to some advisers, even discussing possible successors. Still, the president did not diminish Kelly as his senior aide prepares to leave the White House, a departure from other instances of aides leaving the White House, and has no plans to humiliate Kelly, officials said. Current and former officials said Trump continues to respect Kelly, no matter how often the two men clashed.

White House officials had previously indicated that Kelly would serve as chief of staff through 2020 at Trump’s request, but the clashes between the two men were an open secret — and White House aides said Trump agreed to that announcement only to quell persistent speculation about Kelly’s status, which he saw as harmful to the West Wing.

The president has often grown frustrated that his White House, which has experienced a historically high rate of staff turnover and is often the scene of infighting among aides, is not depicted as a smooth-running machine. No one thought Kelly would survive until 2020 in the White House, according to former and current officials.

As he left for Philadelphia on Saturday, Trump told reporters that he would announce the new chief of staff in the “next day or two” and noted that Kelly has been with him for nearly two years.

Trump previously spoke with Ayers about assuming the chief of staff role, according to his advisers, and has settled on Ayers as the likely replacement. But Ayers has not committed to taking the job for the long term, frustrating the president, who is said to want a replacement who will stay until 2020.

Many senior White House officials, with the exception of Kushner and Ivanka Trump, are skeptical of Ayers, a sharp-elbowed, 36-year-old political operative, and his elevation could precipitate departures, White House officials said.

Kushner and Ivanka Trump have battled for some time to replace Kelly, and the firing showed their continued influence in a West Wing where the president’s family members often have the last say.

The couple told others privately that Kelly shared damaging stories about them and had not always served the president well. For his part, Kelly joked that the couple was “playing government,” and he said they should never have been brought into the White House — and that the pair thought they did not have to follow the traditional rules.

The news of Kelly’s imminent departure came amid a flurry of bad headlines for the president: that prosecutors in New York said he had worked with his former personal attorney, Michael Cohen, to violate campaign finance laws in paying for the silence of women with whom he had affairs; that his former secretary of state called him an uneducated leader who sometimes wanted to break the law; and continual declines in the stock market — the latter a particular point of frustration for the president. He also faces the prospect of a Democratic-led House of Representatives launching investigations into his administration after Republicans lost the chamber in last month’s elections.

Kelly’s exit comes as little surprise. As with former secretary of state Tillerson, former attorney general Jeff Sessions and others before, the president had spoken of his dissatisfaction with Kelly for months and had begun looking for a successor.

The two men have had profane arguments in the West Wing, with Kelly sometimes leaving for the day after battling with Trump. “I’m outta here,” Kelly has been heard to say, leaving others wondering whether he would return.

The chief of staff has told others in the White House that Trump is ignorant of the workings of much of the government — including military operations, immigration laws and Congress — and that he is obsessed with his news coverage.

As homeland security secretary, Kelly was so disturbed by the first few months of the Trump White House that he only reluctantly accepted the job of chief of staff; the president tweeted that Kelly would be taking the job before Kelly formally agreed.

In private, Trump has often criticized Kelly as lacking political skills — a potential liability from Trump’s standpoint. The president also chafed at Kelly’s management style and resisted some of his moves to instill discipline in the West Wing and contain chaos. In recent months, the chief of staff’s power has ebbed, and administration decisions have been guided more by Trump’s instincts than by Kelly’s processes.

The two men have also split on personnel decisions. For instance, Trump and Kelly have repeatedly clashed over the fate of Homeland Security Secretary Kirstjen Nielsen, a close ally of Kelly’s who has drawn the president’s ire for her perceived lackluster performance in immigration enforcement. Kelly had been fighting Nielsen’s potential ouster from the administration, even as his own future in the West Wing remained murky. His repeated defenses of Nielsen came to be seen as “ridiculous to the point of being absurd” in the West Wing, according to a former senior administration official.

Kelly has also battled with national security adviser John Bolton, an influential figure in Trump’s orbit. He also did not get along with Corey Lewandowski, an aggressive outside adviser who maintains significant sway in the president’s orbit.

But despite the private battles between Trump, 72, and Kelly, 68, the two men are generational peers, and Trump has long admired the retired four-star Marine Corps general’s military experience.

The great irony, current and former aides said, is that Trump hired Kelly because he said the Marine general could bring order to the White House. But in recent months, Trump had begun calling Reince Priebus, his former chief of staff, more often. And he said he missed having fun.

In recent times, the president has often spent only six or seven hours in the Oval Office daily, instead preferring to be in the residence, where he can do as he pleases. Kelly told others that the less time Trump spent in the Oval Office some days, the better.

Chatter about Kelly’s exit welled up again in the past week, as multiple news outlets reported that the chief of staff’s departure was imminent. Kelly did not show up at work Friday, and the lights in his West Wing office were off all day. Those closest to Kelly said they did not know his job status. He is said to have gone to the White House on Friday night ahead of a staff dinner and ironed out the departure.

Trump and Kelly had agreed that the chief of staff would announce the move Monday. But the president announced it Saturday amid a flurry of headlines he wanted to change, two advisers said.