Especially if your visa is single-entry, using it for a completely different purpose than the one you applied for will be risky.
The visa is legally valid for entering Spain, but the border guards will (as they are supposed to) question you about your plans when you enter, and that's when you're at risk of getting the visa annulled pursuant to the Visa Code, article 34(1):
- A visa shall be annulled where it becomes evident that the conditions for issuing it were not met at the time when it was issued, in particular if there are serious grounds for believing that the visa was fraudulently obtained. A visa shall in principle be annulled by the competent authorities of the Member State which issued it. A visa may be annulled by the competent authorities of another Member State, in which case the authorities of the Member State that issued the visa shall be informed of such annulment.
The trouble is that in order to get Germany to issue a visa, you'd have to present a plan for going to Germany as your main destination with the visa application -- otherwise the German consulate would have rejected the application. So the border guards in Spain (or wherever you enter) will know that you filed such a plan with your application. If the tickets and other documentation you have to show at entry are for a completely different trip, the border guards may get the idea that the trip you originally applied for was a lie from the beginning and therefore decide that your visa should be annulled as fraudulently obtained.
It is not certain that this will happen -- if you manage to convince the border guards that you really truly did intend to go to Germany at the time you applied but changed your mind afterwards, then the above section won't apply to you, and you should be fine.
But still it feels like something of a gamble.
If you have time, you should probably make a new application to the Spanish consulate and ask them to revoke the German one. If you don't have time for this and have already paid for tickets to Spain, then an argument for chancing it could be made. Hey, it might work! But note that if your visa does get annulled (and you're sent back), this may make it harder for you to obtain another visa in the future.