LONDON — The British ambassador to the United States resigned Wednesday following leaked memos that showed he had called President Donald Trump "insecure" and "incompetent."

Sir Kim Darroch said in a statement that the fallout from the leaked communications — which sparked a series of broadsides from Trump — was "making it impossible for me to carry out my role as I would like."

After the secret diplomatic communications were published this weekend, the president responded with a series of unprecedented attacks on the British ambassador and the prime minister, historically among Washington's closest allies.

Trump called Darroch "wacky," "very stupid" and a "pompous fool," and suggested he would not be able to do his job in Washington because "we will no longer deal with him." The ambassador was uninvited from a dinner Trump was hosting for the emir of Qatar.

Sir Simon McDonald, the most senior official at the Foreign Office, told British lawmakers later Wednesday it was the first time he knew in which a foreign government had refused to deal with one of their envoys.

"The last time I know that we had difficulty with the United States was 1856," McDonald told the House of Commons Foreign Affairs Select Committee. Back then, he said, the ambassador "was accused of recruiting Americans to fight on the British side in the Crimean War. President Franklin Pierce was in the White House."

Trump's targets following the leak included Prime Minister Theresa May. He tweeted that he had been "very critical" of May and the "mess" she had made of her Brexit negotiations.

However, May will stand down in two weeks. Far more attention has been paid to her likely successor, former London mayor Boris Johnson, who repeatedly avoided the question when invited to support the ambassador during a televised debate Tuesday night.

Nigel Sheinwald, a former British ambassador to the United States between 2007 and 2012, said the episode showed that the values of public service were "under siege."

Lawmakers need to respect the boundaries between public servants and ministers, he said.

It was key that diplomats were appointed impartially and not according to political convictions because they were there to serve the government of the day — whoever is elected, he added.

“You need someone who can analyze that administration dispassionately and who has the experience and the courage to take up British positions," he said. "I don't think President Trump should be allowed to pick the next British ambassador to the United States."

Sheinwald said he thought it was "highly likely" that Johnson's evasive performance in Tuesday night's leadership debate would have influenced Darroch's decision.

"He should have given much clearer backing to the civil service and to Kim Darroch, he’s tried to do a bit of a repair job today but the damage frankly was already done," he said.

Following Darroch's resignation, Johnson called him a "superb diplomat."

Jeremy Hunt, Johnson's rival for the Conservative leadership, called Darroch one of the best diplomats in the world.

Deeply saddened to hear of the resignation of Sir Kim Darroch. Standing up for Britain means standing up for the finest diplomats on the world. It should never have come to this. — Jeremy Hunt (@Jeremy_Hunt) July 10, 2019

The ambassador was due to stand down at the end of this year, but he said in the statement issued through the U.K. Foreign Office that the leak had made his position untenable.

He said the support offered on both sides of the Atlantic had "brought home to me the depth of friendship and close ties between our two countries."

He added that "the professionalism and integrity of the British civil service is the envy of the world. I will leave it full of confidence that its values remain in safe hands."

May said that "it is a matter of great regret that he has felt it necessary to leave his position." In a statement to the House of Commons, she stressed it was important for diplomats to be able to speak their minds and to "defend our values and principles, particularly when they are under pressure"

McDonald, the most senior official at the Foreign Office, spoke on behalf of many experienced officials in Britain who believe that Darroch did nothing wrong in expressing his sincerely held belief about Trump's White House.

"You were the target of a malicious leak, you were simply doing your job," he wrote Wednesday.

Lord Peter Ricketts, the former British ambassador to France, said Darroch was the victim of "political sabotage."

Kim has been an outstanding public servant with a distinguished career in the highest-profile jobs. It should not have ended like this. He has been taken out by an act of political sabotage. What does this say about the state of our country? https://t.co/ybgU1QaNSL — Peter Ricketts (@LordRickettsP) July 10, 2019

And Sir Christopher Meyer, a former British ambassador in Washington, said Darroch was "blameless" and had been brought down by a "disgraceful leak and the vindictive reaction of the U.S. president."

The leak presented a dilemma for Britain. Some acknowledged that the messages, and Trump's reaction, would make Darroch's last few months near impossible. But many were also cautious of the U.K. being seen to allow other countries to veto the diplomats posted to their embassies.

"We should never allow any country to dictate who we send as ambassador," Charles Parton, who served as a British diplomat for almost four decades, told NBC News before Darroch's resignation. "It would give enormous power to other countries, so you just can't do it."