The first woman to head Spain's CNI intelligence agency, Paz Esteban, has lost her job in a deepening scandal over phone-hacking software found on the phones of top politicians.
Prime Minister Pedro Sánchez, two other ministers and 18 Catalan separatists were all targeted by Pegasus software.
The controversial software has been used to access the phones of prominent figures around the world
But Mr Sánchez was the first leader to be confirmed as a target.
Defence Minister Margarita Robles, who was herself a victim of the phone-hack, said the government had decided to remove the spy chief, who has been with the CNI for almost 40 years.
"You speak of dismissal, I speak of substitution," she told reporters.
Ms Paz, 64, has run the CNI since 2019 and will be replaced by the defence minister's deputy, Esperanza Casteleiro.
How scandal engulfed Spanish politics
More than 60 Catalan separatist figures accused Spain of spying on their phones last month, following revelations by Citizen Lab research centre in Canada. The allegations prompted the pro-independence ERC party to withdraw its support from the Socialist-led government.
It then emerged that the prime minister and defence minister were spied on too, along with Interior Minister Fernando Grande-Marlaska.
Pedro Sánchez's phone was hacked twice in May 2021 and officials said there was at least one data leak from it. The government said it was "illicit and external" and involved non-official bodies without state authorisation. However, it refused to comment on speculation in Spanish media that Morocco may have been behind the hack, which took place at the time of a diplomatic row.
Last week Ms Robles told a parliamentary commission that the spy agency had always acted within the law and that 18 Catalan separatists had been spied on with court approval.
Among those targeted was Catalan President Pere Aragonès. Ms Robles told a press conference on Tuesday that mistakes had been made although she defended the CNI's international prestige. "In this country no-one is investigated for their political ideas," she stressed.
Critics said Paz Esteban was being removed as a scapegoat offered to pro-independence parties to ensure the left-wing government's survival.
"It's a genuine affront to our country," complained opposition Popular Party president Alberto Núñez Feijóo.
What is Pegasus?
Pegasus spyware was developed by private Israel-based firm NSO Group and it can infect iPhones and Android phones. Data can be extracted and cameras and microphones can be secretly activate to record calls.
NSO said Pegasus had been intended for use against criminals and terrorists and only made available to countries with good human rights records. It is now facing legal action from companies including Apple and Microsoft.
Its use is now being investigated by the European Parliament for alleged breach of EU law. Hungarian journalists accuse their government of targeting them and the Polish government has admitted using it as a surveillance tool, but not for political purposes. Saudi Arabia, Morocco and Azerbaijan have all been linked to the scandal.