SAN JUAN, P.R. — After seven months and close to $2.5 billion, almost everybody in hurricane-ravaged Puerto Rico had their lights back on — until a freak accident on Wednesday plunged the entire island once again into darkness.

The Puerto Rico Electric Power Authority had boasted Wednesday morning that less than 3 percent of its customers remained without power, substantially concluding what some estimates called the biggest power failure in United States history. The island of 3.4 million residents was open for business again, government officials said.

It was only a few hours later that an excavator working near a fallen 140-foot transmission tower on the southern part of the island got too close to a high-voltage line. The resulting electrical fault knocked out power to nearly every home and business across the storm-battered American territory, authorities said, a catastrophic failure that could take up to 36 hours to restore.

It was the first time since Hurricane Maria left the island’s power grid in ruins on Sept. 20 that nearly all of the electric company’s 1.5 million customers found themselves in the dark, although another failure less than a week ago had cut power to 870,000 users. Only small pockets generated by microgrids were spared by the latest power loss.