You ask the question why? The answer is because they legally can. Whilst I sympathise completely with the dilemma these property owners find themselves in there is another point of view.
It’s called the enforcement of legality. Spain for so long did not enforce it own laws and rules. That created a culture of ignoring them especially among politicians, lawyers and developers. They got away with it for decades. Illegally built unbanisations eventually became legalised because nobody wanted the inevitable stink and unwelcome investigations.
Spain has changed, not fundamentally but in demolishing these properties they are sending a message. Not to the expat buyers but to their own people. It’s a cruel way to do it. However I cannot think of a better method of communicating that you cannot do it any more.
In the long term it may end up protecting future buyers and bring much needed confidence back into a broken and discredited market system.