The father of 13 who was gunned down in a Brooklyn bodega last week was not the victim of a robbery gone bad — but was instead the target of a revenge killing that was part of a war among his family that stretches back to Yemen.

The shooter was apparently victim Swadh Maged’s nephew, who was seeking revenge on the deli man for fatally gunning down his father — Maged’s brother — with an AK-47 in 2010, law-enforcement sources said Wednesday.

That shooting four years ago in Yemen was itself an act of retribution, because Maged believed his brother had killed Maged’s daughter years earlier, sources said.

Maged moved to the United States 10 years ago.

When the unidentified alleged deli killer moved to Brooklyn a few months ago, it appeared, at first, that he was going to leave his enmity for his uncle behind, the sources said.

“It had seemed like what had happened in Yemen, stayed in ­Yemen,” said a law-enforcement source.

But investigators believe that the killer was waiting to exact his revenge.

At about 6:45 a.m. Thursday, the armed man calmly strolled into the Deli Grocery on Central Avenue, raised his pistol and executed Maged at close range, the sources said.

That night, the killer took a flight from Kennedy Airport to Dubai in the United Arab Emirates. Investigators believe he was headed back to Yemen.