Four Afghan policemen and two of their Spanish counterparts were killed in December last year when Taliban insurgents burst into the embassy after a car bomb blew off the gate of the compound, sparking a shoot-out inside.
Relatives of one of the Spanish policemen who died and seven others who were injured filed a complaint accusing Ambassador Emilio Perez de Agreda and his then deputy Oriol Sola of involuntary manslaughter.
According to court papers published Wednesday, the plaintiffs allege the ambassador failed to act on warnings of security flaws at the embassy.
Since it was built in 2008, there had been warnings about flimsy sentry boxes, or doors that were only made out of wood, which were later reinforced, according to the papers.
The building is outside the secure "green zone" in the Afghan capital, where many other Western embassies are located.
The ambassador, though, had his home in the "green zone" as his predecessor had "refused to live in the embassy due to the potential danger," the writ said.
When the attack happened, Perez de Agreda was on holiday in Spain, and Sola was in charge.
He "didn't respect any security measure either," the plaintiffs alleged, according to the papers.
"On the contrary, he constantly put the building at risk, letting traders into the embassy, allowing cars, vans and trucks in with jewels, carpets and other items without giving security teams enough advance warning to check people and products", it added.
On December 11th 2015 - the day of the attack - French intelligence services warned of the risk of an attack against the Spanish embassy, but no special security measures were taken, the writ further alleges.
According to judge Santiago Pedraz, the allegations could be a "penal offence," and will be investigated.
A spokesman for the foreign ministry said Perez de Agreda is still ambassador in Afghanistan, but Sola has since left due to "normal staff rotation."
He said both the ministry and ambassador were "fully available to collaborate with judicial authorities in clarifying the facts."