As Joseph Arpaio’s federal case headed toward trial this past spring, President Trump wanted to act to help the former Arizona county sheriff who had become a campaign-trail companion and a partner in their crusade against illegal immigration.

The president asked Attorney General Jeff Sessions whether it would be possible for the government to drop the criminal case against Arpaio, but was advised that would be inappropriate, according to three people with knowledge of the conversation.

After talking with Sessions, Trump decided to let the case go to trial, and if Arpaio was convicted, he could grant clemency.

So the president waited, all the while planning to issue a pardon if Arpaio was found in contempt of court for defying a federal judge’s order to stop detaining people merely because he suspected them of being undocumented immigrants. Trump was, in the words of one associate, “gung-ho about it.”

“We knew the president wanted to do this for some time now and had worked to prepare for whenever the moment may come,” said one White House official who spoke on the condition of anonymity because of the sensitivity of the action.

Responding to questions about Trump’s conversation with Sessions, White House press secretary Sarah Huckabee Sanders said, “It’s only natural the president would have a discussion with administration lawyers about legal matters. This case would be no different.”

The Justice Department declined to comment.

Trump’s Friday-evening decision to issue his first pardon for Arpaio was the culmination of a five-year political friendship with roots in the “birther” movement to undermine President Barack Obama. In an extraordinary exercise of presidential power, Trump bypassed the traditional review process to ensure that Arpaio, who was convicted of contempt of court, would face no time in prison.

Trump’s pardon, issued without consulting the Justice Department, raised a storm of protest over the weekend, including from some fellow Republicans, and threatens to become a stain on the president’s legacy. His effort to see if the case could be dropped showed a troubling disregard for the traditional wall between the White House and the Justice Department, and taken together with similar actions could undermine respect for the rule of law, experts said.

Arpaio faced up to six months in prison and was due to be sentenced in October. During his 23 years as Maricopa County sheriff, Arpaio was a lightning rod, in part because of his aggressive crackdown on illegal immigrants. He also was accused of racial profiling, failure to investigate sex crimes, poor treatment of prisoners and other instances of police misconduct.

To Trump, however, Arpaio is an American hero — a man who enlisted in the military at 18 after the outbreak of the Korean War, worked as a beat cop in Washington and Las Vegas and as a special agent investigating drug crimes around the world, then was elected sheriff in the epicenter of the nation’s roiling immigration debate.

Joe Arpaio's illegal-immigration crackdown made him a polarizing figure and an early ally of President Trump. (Meg Kelly/The Washington Post)

Arpaio’s age weighed on Trump, some of his confidants said. The 71-year-old president could not stomach seeing an 85-year-old he admired as a law-and-order icon wasting away in a jail cell.

Trump’s spring inquiry about intervening in Arpaio’s case is consistent with his alleged attempts to influence the federal investigation of Michael Flynn, the former national security adviser. Trump also made separate appeals in March to Director of National Intelligence Daniel Coats and National Security Agency Director Michael S. Rogers to publicly deny the existence of any evidence of collusion between the Russians and the Trump campaign during the 2016 election.

Trump’s pardon of Arpaio “was his backhand way of doing what he wanted to do at the front end,” said Robert Bauer, a former White House counsel in the Obama administration. “He just wanted to kill the prosecution off. He couldn’t do it the one way, so he ended up doing it the other way. This is just another vivid demonstration of how far removed from an appropriate exercise of the pardon power this was.”

Presidents can set law enforcement priorities, but they are expected to steer clear of involvement in specific cases to avoid the perception of politicizing the impartial administration of justice.

Trump backed off the Arpaio case after being advised it would be inappropriate, but that he even tried is “beyond the pale,” said Chiraag Bains, a former senior counsel in the Justice Department’s Civil Rights Division.

Bains said he believes Trump “has a sense that the chief executive controls everything in the executive branch, including the exercise of criminal power. And that is just not the way the system is set up.”​

Trump and Arpaio became brothers in arms five years ago. As they saw it, the two provocateurs — one a celebrity real estate developer, the other a polarizing sheriff — were pursuing justice in the form of supposed evidence that Obama’s birth certificate was fraudulent.

As caretakers of the false “birther” conspiracy, Trump and Arpaio relentlessly probed Obama’s birth in Hawaii and nurtured a lie to damage the legitimacy of the nation’s first African American president.

“There was no collusion,” Arpaio said in an interview Saturday. “I started my birth certificate investigation around the same time he did his.”

The Manhattan mogul sent Arpaio a fan letter and flattered him on social media. “Congratulations to @RealSheriffJoe on his successful Cold Case Posse investigation which claims @BarackObama’s ‘birth certificate’ is fake,” Trump tweeted in 2012.

Three years later, in July 2015, when Trump swooped into Arpaio’s hometown of Phoenix for the first mega-rally of his upstart presidential campaign, the sheriff returned the favor by testifying on stage to “the silent majority” that Trump had begun to awaken.

Backstage at that rally, Arpaio recalled, the two men talked about their shared birthday — June 14, which is Flag Day. Their friendship blossomed and Arpaio became a fan favorite at Trump rallies. “I had a gut feeling that he was going to win,” Arpaio said.

Even as Trump went on to win last November, however, Arpaio lost his reelection — and that was the least of his troubles.

Federal prosecutors filed criminal charges against Arpaio last October. Trump was paying attention to the case and he called Arpaio to check in on him around Thanksgiving, according to the former sheriff. That’s when Arpaio told the president-elect that his wife, Ava, had cancer.

On July 31, Arpaio was convicted by a judge, as opposed to a jury. Arpaio and his lawyer, Mark Goldman, said they did not contact Trump during this period, nor ask anyone in the administration for a pardon.

“I didn’t ask for the pardon,” Arpaio said. “He wanted to do it because I think he understood what I was going through.”

Inside the West Wing, the pardon process was set in motion. Senior policy adviser Stephen Miller, who had gotten to know Arpaio through their work on immigration policy during the campaign, advocated internally for the pardon, as did chief strategist Stephen K. Bannon, according to people familiar with the deliberations.

The White House Counsel’s Office had quietly begun preparing the paperwork and communications staffers had started drawing up talking points when Trump foreshadowed his intentions Aug. 15 by retweeting a Fox News story reporting that the president was “seriously considering” pardoning Arpaio.

Around the same time, Arpaio received a call from the White House Counsel’s Office asking whether he would accept a pardon if one were issued. He told the presidential lawyer that he would, according to Goldman.

The drumbeat culminated Tuesday when Trump returned to the Phoenix Convention Center — the site of the July 2015 rally — for a “Make America Great Again” campaign event.

As Air Force One rumbled toward Arizona, Sanders tried to douse speculation by telling reporters that the president would have “no discussion” and “no action” pertaining to Arpaio at the rally.

Arpaio said he was eager to attend the rally and visit with the president backstage, but decided, “I didn’t want to cause any harm or riots, so I stayed away, which really hurt me.”

When Arpaio heard Sanders say Trump would not talk about a pardon, he said he turned to his wife and told her, “Don’t believe anything you hear because I know how he is.”

Sure enough, Trump bellowed from the stage, “I’m just curious: Do the people in this room like Sheriff Joe?”

The crowd burst into applause.

“Was Sheriff Joe convicted for doing his job?” Trump asked.

More applause.

“He should have had a jury, but you know what? I’ll make a prediction,” the president said. “I won’t do it tonight, because I don’t want to cause any controversy. . . But Sheriff Joe can feel good.”

Arpaio and his legal team did not feel very good the next night, when they read a CBS News report that Trump was being advised not to pardon Arpaio until after his sentencing.

Goldman wrote a two-page letter to White House Counsel Donald F. McGahn II, sent on Friday morning, saying, “Hopefully this is more fake news,” and telling McGahn that a delay until after sentencing “would place Sheriff Arpaio in an untenable and unprecedented position.”

Without a pardon, Goldman said, Arpaio could be “sentenced, handcuffed, given a ‘perp walk’ and incarcerated” and “left to languish in federal custody.”

McGahn did not immediately reply, but a few hours later, at about 6:30 p.m. D.C. time, another lawyer in his office called Goldman’s co-counsel to double-check that Arpaio would accept a pardon. A few minutes later, an email arrived from the White House with a single page attachment: an “Executive Grant of Clemency” for Arpaio signed by Trump in his thick, black script, complete with a golden Justice Department seal.

Goldman printed out three copies of the document and drove out to Arpaio’s home in Fountain Hills, a suburb of Phoenix, where the former sheriff was getting ready to take his wife to dinner at Arrivederci, an Italian restaurant, to celebrate Ava’s 86th birthday.

“Of course, his first question was, ‘Is this a fake document?’ ” Goldman recalled. “We know the sheriff has looked into fake documents.”

The Arpaios still went out for their spaghetti dinner. As of Saturday, Arpaio had not heard from Trump personally, but said if the president were to call he would advise him to take a lesson from his Arizona adventures.

“If they can do it to me, they can do it to anybody, including the president of the United States,” Arpaio said. Alluding to the Russia probe, he said, “He’s been under a lot of fire right now, him and his family, and I’ve been through the fire quite a while.”