San Francisco City Attorney Dennis Herrera filed a lawsuit against Pacific Gas and Electric Co. on Wednesday accusing the utility of causing a January 2016 landslide that destroyed or damaged six homes near Mount Davidson and has cost the city nearly $8 million.

The suit, filed in San Francisco Superior Court, alleges that negligent and shoddy workmanship by PG&E during a gas-line relocation project set off a chain reaction that damaged nearby city water mains. The damaged mains, in turn, caused critical underground leaks that saturated and softened the sloping earth beneath Casitas Avenue, resulting in the landslide.

Five homes were damaged as the saturated land beneath their foundations slowly shifted, and a sixth house had to be red-tagged and demolished after the city determined it posed an “imminent risk of sliding down the hill,” creating a threat to the homes below.

The city is seeking to recoup from PG&E all of the money it paid in homeowners’ claims related to the incident. To date, that includes $6.7 million to the six homeowners and $1.1 million in related construction costs and attorneys’ fees. The city says in its suit that PG&E has refused to correct its construction defects and has denied any responsibility for the damage it allegedly caused.

“Homeowners were faced with a dangerous situation when their houses were suddenly threatened by this landslide,” Herrera said in a statement Wednesday. “PG&E’s shoddy work caused this problem, but the company is trying to shirk its responsibility.”

The city’s suit claims that PG&E crews dug a trench to access the utility’s gas lines and move them to a spot adjacent to the city’s water mains and they then used improper material to fill in the trench. The material PG&E used, the city alleges, was “clayey sand and gravel,” which didn’t compact in a way that would keep the underground pipes safe from damage.

When garbage trucks or other heavy vehicles rolled over Casitas Avenue, their weight pressed against the poorly compacted material, putting pressure on the water main below, the suit alleges. Eventually, it said, the water main succumbed to “fatal stresses,” breaking at the point where the main connected to a house at 234 Casitas Ave.

Water spurted from the pipe, soaked into the ground and flowed downhill, creating the conditions that led to the landslide, the city contends. Because the problem was occurring beneath the road’s surface, it went undetected, the suit said.

San Francisco officials initially suspected the landslide was caused by a leaking water main, but a subsequent investigation by the city pointed to PG&E’s relocation project, city officials said. Exactly when the pipe began to leak is unclear, but permits for the Casitas Avenue work filed as evidence along with the lawsuit show PG&E was allowed to perform its work between January and May 2015.

PG&E spokeswoman Tamar Sarkissian said no one was available at the utility Wednesday to respond to questions about the gas-line relocation work, including why the utility wanted to move the lines in the first place.

“We are aware that a lawsuit has been filed, (but) the fires are still burning across Northern California,” Sarkissian said in an email. “Our primary focus is the life safety and well-being of the customers and communities affected by the devastating wildfires. We are working diligently to get their electric and gas service restored for their immediate needs.”

Dominic Fracassa is a San Francisco Chronicle staff writer. Email: dfracassa@sfchronicle.com Twitter: @dominicfracassa