The Viceroyalty of New Spain (established 1535, capital Mexico City) was not "ended by Spain." Rather, it dissolved through a cascade of independence movements:
Internal crises (Napoleonic invasion of Spain, 1808), Enlightenment ideas, creole resentment, and military uprisings drove the collapse. Spain lost control gradually, not by voluntary decree.