Washington — President Donald Trump spared former Sheriff Joe Arpaio a possible jail sentence on Friday by pardoning the recent federal conviction stemming from his immigration patrols, reversing what critics saw as a long-awaited comeuppance for a lawman who escaped accountability for headline-grabbing tactics during his tenure as metropolitan Phoenix’s top law enforcer.

The White House said the 85-year-old ex-sheriff was a “worthy candidate” for a presidential pardon. It was Trump’s first pardon as president.

“I am pleased to inform you that I have just granted a full Pardon to 85 year old American patriot Sheriff Joe Arpaio. He kept Arizona safe!” Trump tweeted Friday.

The announcement came three days after a rally in Phoenix at which the president signaled his willingness to absolve Arpaio’s misdemeanor contempt-of-court conviction.

Arpaio was in a celebratory mood after the pardon, eating dinner at an Italian restaurant as someone in his party ordered champagne. He told The Associated Press he was thankful for the pardon.

“I appreciate what the president did,” he said. “I have to put it out there: Pardon, no pardon — I’ll be with him as long as he’s president.”

The pardon drew a swift and harsh denunciation from Latinos and political leaders in Arizona and beyond. They said the action amounted to an endorsement of racism by wiping away the conviction of a man who has been found by the courts to have racially profiled Latinos in his immigration patrols.

“Pardoning Joe Arpaio is a slap in the face to the people of Maricopa County, especially the Latino community and those he victimized as he systematically and illegally violated their civil rights,” Phoenix Mayor Greg Stanton said.

The White House announced the pardon late Friday after Trump fleshed out the details of his ban on transgender individuals serving in the military, a policy that will cheer his conservative base, and as a powerful Category 4 hurricane threatened to batter Texas with heavy winds and severe flooding.

Arpaio became a nationally known political figure over the past dozen years as he took aggressive action to arrest immigrants in the country illegally. But years of legal issues and costs stemming from his immigration efforts began to take a toll on his political power at home, and he was handily defeated by a Democrat in the 2016 election.

It coincided with Trump winning the White House based in large part on his immigration rhetoric, with Arpaio campaigning for him around the country.

Trump has been plagued by poor job approval ratings that currently stand at 34 percent, the lowest mark ever for a president in his first year. His decision on Arpaio may serve to energize Trump supporters dispirited over the president’s decision a week ago to dismiss chief strategist Steve Bannon. But it has angered his opponents even more.

The pardon contradicts a key theme in the movement for tougher immigration enforcement — that all people, no matter who they are, aren’t above the law. Arizona politicians have invoked the “rule of law” for more than a decade as the guiding principle in pushing for tougher immigration laws.

The pardon also marked a devastating defeat for critics who believed the lawman sowed divisions by making hundreds of arrests in crackdowns that separated immigrant families and promoted a culture of cruelty by housing inmates in outdoor tents during triple-digit heat and forcing them to wear pink underwear.

They say it removed the last chance at holding Arpaio legally accountable for what they say is a long history of misconduct, including a 2013 civil verdict in which the sheriff’s officers were found to have racially profiled Latinos in his immigration patrols.

Arpaio was accused of prolonging the patrols for 17 months after a judge had ordered them stopped so that he could promote his immigration enforcement efforts in a bid to boost his successful 2012 re-election campaign.

He acknowledged extending the patrols, but insisted it wasn’t intentional, blaming one of his former attorneys for not properly explaining the importance of the court order and brushing off the conviction as a “petty crime.”

He accused then-President Barack Obama of trying to influence the 2016 sheriff’s race by announcing in court weeks before Election Day that it was willing to prosecute Arpaio.

But the charge itself wasn’t filed by prosecutors. It was recommended by the judge who presided over the profiling case. Lawyers in Trump’s Justice Department prosecuted the case at a five-day trial this summer.

The TV interviews and news releases that media-savvy lawman used over the years to help promote his immigration crackdowns and win re-election came back to bite him when the judge who found him guilty cited comments the sheriff made about keeping up the patrols, even though he knew he wasn’t allowed.

The criminal case sprang from the profiling lawsuit that ultimately discredited Arpaio’s immigration patrols and is expected to cost taxpayers $92 million by next summer.

Arpaio’s office was accused in other instances of wrongdoing in the profiling case, though none led to criminal charges.

The alliance between Trump and Arpaio centers heavily on immigration enforcement, such as getting local police officers to take part in immigration enforcement. They also have questioned the authenticity of Obama’s birth certificate and have a similar history in sparring with judges.

During the presidential campaign, Arpaio showered Trump with support. He appeared for Trump and gave a speech at the Republican National Convention.

“So Sheriff Joe was convicted for doing his job?” Trump asked supporters at Tuesday’s rally in Phoenix. “I’ll make a prediction. I think he’s going to be just fine, OK.”

Trump issued the pardon seven months after taking office, though it’s not unprecedented for a president to issue a pardon in their first year in office.

The most recent president to issue a pardon so early in his term was George H. W. Bush, who granted clemency after seven months as president, said Jeffrey Crouch, a professor of politics at American University who has written a book on presidential pardons.

Arpaio said he’ll discuss more about his case next week, but says he’ll remain active politically now that he’s no longer facing jail time.