A hero police dog was accidentally killed by friendly fire when a pit bull attacked him during a raid on a house in upstate New York, the state police said.

State Police K9 Will, a 3-year-old Belgian Malinois, was with a contingent of cops to help execute a search warrant in the town of Greig on the western edge of the Adirondacks, according to authorities.

As deputies from the Lewis County Sheriff’s Office entered the home, the pit bull ran out and began biting Will, whose partner — Trooper Shaun Smith — shot the vicious beast in the leg around 11 a.m. Monday.

The bullet then apparently ricocheted off the gravel driveway and struck Will in the abdomen, entering his stomach.

The black-and-tan canine was rushed to the Countryside Veterinary Clinic in Lowville, where he died of the injury.

Will — named after Trooper William G. Doyle, who was fatally shot in 1967 by escapees from a mental institution — had been on the job since April, when he graduated from basic K9 school.

It was unclear what happened to the pit bull.