Your result
Spain — Naturalization
Possible
Time in Spain without a permit does not count towards naturalisation, however many years of it there are — but it is very often the thing that qualifies you for a permit, and that is the step to take.
Spanish nationality by residence requires residence that is legal. Undocumented time, however long, settled and well-documented, builds nothing towards the ten or two years. What it can build is arraigo — the route by which Spain regularises people already living there. Since the 2025 regulation the qualifying period is two years rather than three, and the routes were reorganised and broadened: there are versions based on work, on social ties, on family, and on committing to training in an occupation Spain is short of.
That is where your accumulated time does count. It is the evidence for arraigo, even though it is not evidence for nationality.
So the sequence is: regularise first, then start the nationality clock from the day your permit begins. It is worth knowing that the clock ahead of you depends on the nationality you held at birth — two years for nationals of Latin American countries, Andorra, Portugal, the Philippines and Equatorial Guinea, ten for everyone else.
This is a situation where proper advice is worth more than anything we can tell you on a page, because which arraigo route fits depends on details of your work history, your family and your documentation that a quiz cannot see.