ASHINGTON (AP) — President Donald Trump fired Defense Secretary Mark Esper on Monday, an unprecedented move by a president struggling to accept election defeat and angry at a Pentagon leader he believes wasn't loyal enough.

The decision was widely expected as Trump had grown increasingly unhappy with Esper over the summer, including sharp differences between them over the use of the military during the civil unrest in June. But the move could unsettle international allies and Pentagon leadership and injects another element of uncertainty to a rocky transition period as Joe Biden prepares to assume the presidency.

Presidents who win reelection often replace Cabinet members, but losing presidents have kept their Pentagon chiefs in place until Inauguration Day to preserve stability in the name of national security.

Trump announced the news in a tweet, saying that “effective immediately” Christopher Miller, the director of the National Counterterrorism Center, will serve as acting secretary, sidestepping the department’s No.2-ranking official, Deputy Defense Secretary David Norquist.

“Chris will do a GREAT job!” Trump tweeted. “Mark Esper has been terminated. I would like to thank him for his service.”

In a letter to Trump, Esper referred to his efforts to keep the Pentagon apolitical -- a resistance that often angered Trump. Esper said he served as defense secretary and Army secretary "in full faith to my sworn oath to support and defend the Constitution, and to safeguard the country and its interests, while keeping the Department out of politics and abiding by the values Americans hold dear.” The Associated Press obtained a copy of the letter.

Esper didn't thank Trump, but he also didn't openly criticize the president or his policies. He said he accepts Trump’s decision to replace him, adding, “I step aside knowing there is much we achieved at the Defense Department over the last eighteen months to protect the nation and improve the readiness, capabilities, and professionalism of the joint force, while fundamentally transforming and preparing the military for the future.”

U.S. defense officials said Miller arrived at the Pentagon in the early afternoon to take over the job, and that White House chief of staff Mark Meadows informed Esper of the firing before Trump announced the move on Twitter. Esper and Miller were in the building at the same time for a while, but Esper left by the end of the day, said defense officials who spoke on condition of anonymity to discuss internal matters.

Trump's abrupt move to dump Esper triggers questions about what the president may try to do before he leaves office, including adjustments in troop presence overseas or other national security changes.

More broadly, the U.S. military continued to operate as usual. U.S. officials said Gen. Mark Milley, chairman of the Joint Chiefs of Staff, met with Miller on Monday and also gathered the top military commanders and chiefs for a secure meeting.

Officials said Miller's message so far is that he won't make immediate changes and the department will stay the course. Military leaders, meanwhile, were calling top officials in their various geographic regions to assure them that the U.S. military is maintaining a stable presence around the world.

In a separate message to the force, Esper expressed a twinge of disappointment, saying “I step aside knowing that there is much more we could accomplish together to advance America’s national security.” He said much was achieved, and "through thick and thin, however, we have always put People and Country first,” he said.

Trump's decision brings to five the number of men who have held the job of defense chief under Trump — either in an acting capacity or confirmed by the Senate. The move was quickly condemned by Democratic members of Congress.

“Dismissing politically appointed national security leaders during a transition is a destabilizing move that will only embolden our adversaries and put our country at greater risk," said Rep. Adam Smith, D-Wash., chairman of the House Armed Services Committee. “President Trump’s decision to fire Secretary Esper out of spite is not just childish, it’s also reckless.”

Former military leaders weighed in. Jim Stavridis, a retired four-star Navy admiral, wrote on Twitter that, “Things are already unstable internationally, and this does not help.”

Republicans praised Esper but largely avoided criticizing Trump. GOP Sen. Jim Inhofe, the Senate Armed Services Committee chairman, told reporters it was Trump's decision and said, “I learned a long time ago I don’t tell the president not to do anything.”

Biden has not said who he would appoint as defense chief, but is widely rumored to be considering naming the first woman to the post — Michele Flournoy. Flournoy has served multiple times in the Pentagon, starting in the 1990s and most recently as the undersecretary of defense for policy from 2009 to 2012. She is well known on Capitol Hill as a moderate Democrat and is regarded among U.S. allies and partners as a steady hand who favors strong U.S. military cooperation abroad.

Miller most recently served as director of the National Counterterrorism Center and before that was a deputy assistant defense secretary and top adviser to Trump on counterterrorism issues. He spent more than 30 years in the military, including as an Army Green Beret, and was deployed multiple times to both Iraq and Afghanistan. After his retirement from the military, Miller worked as a defense contractor.

Esper's strained relationship with Trump came close to collapse last summer during civil unrest that triggered a debate within the administration over the proper role of the military in combating domestic unrest. Esper's opposition to using active duty troops to help quell protests in Washington, D.C., infuriated Trump, and led to wide speculation that the defense chief was prepared to quit if faced with such an issue again.

The tensions fueled rumors that Esper would be ousted if Trump won reelection.

Presidents historically have put a high priority on stability at the Pentagon during political transitions. Since the creation of the Defense Department and the position of defense secretary in 1947, the only three presidents to lose re-election — Gerald Ford, Jimmy Carter and George H.W. Bush — all kept their defense chiefs in place until Inauguration Day.

Esper, who was the official successor to former Marine Gen. James Mattis, routinely emphasized the importance of keeping the military and the Defense Department out of politics. But it proved to be an uphill struggle as Trump alternately praised what he called “his generals” and denigrated top Pentagon leaders as war-mongers devoted to drumming up business for the defense industry.

During Trump’s presidency, the Pentagon has often been at the center of the tumult, caught in a persistent and erratic debate over the use of American forces at war in Iraq, Syria and Afghanistan, and on U.S. soil, at the Mexico border and in cities roiled by civil unrest and rocked by the coronavirus.

Esper’s departure has appeared inevitable ever since he publicly broke with Trump in June over the president’s push to deploy military troops in the streets of the nation’s capital in response to civil unrest following the police killing of George Floyd. Esper publicly opposed Trump’s threats to invoke the two-centuries-old Insurrection Act, which would allow the president to use active-duty troops in a law enforcement role. And Trump was furious when Esper told reporters the Insurrection Act should be invoked “only in the most urgent and dire of situations,” and, “We are not in one of those situations now.”