Los Angeles County voters have removed Sheriff Jim McDonnell from office, electing former deputy Alex Villanueva in a startling upset that could upend the dynamics of power between local political leaders and the largest sheriff’s department in the country.

Villanueva leads by a slim margin of 50.15 percent over 49.85 percent of the vote — or a difference of 4,927 votes, with 100 percent of precincts reporting. The results, though not fully certified, offer multiple firsts.

Villanueva is to become the first partisan Democrat elected L.A. sheriff and the first Spanish-fluent sheriff since 1890, while McDonnell would become the first elected sheriff unseated in Los Angeles in a century. McDonnell, who spent nearly 30 years in the LAPD in every rank but chief, did not make any statements on social platforms through the afternoon.

[Update, 9:32 pm: Thousands of provisional ballots are still being counted, but an LAT analysis of results so far show Villanueva garnering strong support in heavily Latino areas of the county, as well as winning in precincts that McDonnell won in the primary, including West Hollywood, which is policed by the Sheriff’s Department.]

Speaking to reporters on Wednesday afternoon, Villanueva said he was confident the results would hold. “I can’t see a 90-degree turn where it reverses course,” he said.

I am so thankful to have the family that I have and to be Sheriff of the finest law enforcement agency in the nation. God bless the #LASD @LASDHQ https://t.co/Hq5UFtDsC6 — Jim McDonnell (@LACoSheriff) November 7, 2018

McDonnell’s loss shakes up local politics. He had the backing of top Democratic figures in Los Angeles, including Mayor Eric Garcetti and District Attorney Jackie Lacey, who are still in office. Now the mayor, the county Board of Supervisors, and the LAPD will have to work and collaborate with a sheriff who is a political outsider to the L.A. establishment.

Before the vote, Villanueva and McDonnell had split the support of the Sheriff’s Department’s two unions.

The Professional Peace Officers Association, a union representing about 6,000 department employees including sergeants and lieutenants, spent at least $750,000 to support McDonnell. While the Association of Los Angeles County Sheriff’s Deputies, those most on the streets, endorsed Villanueva and spent more than $1.3 million on mailers for the challenger.

McDonnell was the stronger fundraiser overall, drawing about $1.2 million from individual donors, mostly in Los Angeles, Long Beach and Beverly Hills. He was supported by at least $954,000 in additional dollars from an independent committee funded by business executives and the union that endorsed him.

RELATED: Self-Styled Whistleblower vs. Outsider’s Insider: Untangling the Race for L.A. County Sheriff

Villanueva raised less than $160,000 from individuals in La Habra Heights where he lives, and areas in eastern parts of the county.

The deputies union has been deeply unhappy with some of McDonnell’s reform efforts, including his attempt to share the names of 300 deputies who may have credibility problems with the district attorney, called the “Brady List.” Villanueva slammed it as “a fake list,” and the union sued to block the sheriff’s move.

The upset results also draw an unprecedented electoral line in L.A. over law enforcement’s relationship with federal immigration authorities and ICE, the Immigration and Customs Enforcement agency, which is accused of human-rights abuses in detaining immigrants.

McDonnell faced criticism from immigration advocates for not supporting the state’s so-called sanctuary law. Backed by Kevin de Leon in the state Senate, SB54 dramatically limits local law enforcement agencies such as police and sheriff’s departments from having any contact with ICE except in “narrow circumstances.”

SB54 sparked a push-back from conservative local city councils across California, some of which are suing the state over the sanctuary law. Villanueva, staking a position to the left on the matter, has said he would “kick ICE out” of local jails. In the campaign, he pledged to uphold SB54.

Immigration and minority advocates worked heavily to support the sanctuary state law.

Hussam Ayloush, executive director of the greater Los Angeles chapter of CAIR (Council on American-Islamic Relations), said on Wednesday that the L.A. sheriff election results are a rebuke of the national climate against immigrants.

“It’s a never good idea to partner local and state law enforcement agencies with federal immigration agencies, especially now under an administration that is openly and blatantly racist and anti-immigrant,” Ayloush told L.A. Taco. “We feel that such partnerships undermine not only the immigrant communities, but also the important work of local law enforcement agencies within those communities.”

“We’ve heard positive comments made by Mr. Villanueva during his campaign that we hope he would uphold and create the necessary distance between both levels of agencies,” he added.

The new sheriff’s stand, if it holds, could place L.A. County in the crosshairs of President Trump’s hard-right Justice Department. Trump fired U.S. Attorney General Jeff Sessions on Wednesday, after elections results showed Democrats retaking the House of Representatives.

The L.A. Sheriff’s Department’s new leader will face multiple critical issues immediately: scandals involving deputy-involved shootings, abuse of power, conditions at the overcrowded jails, low morale in the rank-and-file, and the presence of “cliques” among deputies that critics say operate as gangs with badges.

RELATED: Feinstein Defeats De Leon, Hopes for Rent Control Crushed, Gas Tax Survives, Newsom to Governor

LA Taco reporter Philip Iglauer contributed to this report. * Story updated.