Mexican independence was officially achieved on September 27, 1821, under the Treaty of Córdoba. Spain did not recognize the treaties, arguing that the viceroy Juan O'Donojú had no authority to recognize the independence of any overseas province.This situation was dangerous to the newly acquired independent status of the nation, which had not yet been recognized by any of the European powers that could support it, and the threat of Spanish reconquest was a constant worry to the leaders of the nascent regime.
Thus Mexico became a monarchy under emperor Augustine Iturbide .However it did not get recognised by Spain though other nations like France and Britian recognised the same .
Again ,one of the most important ports to gain entry in the region ,San Juan de Ulúa remained in Spanish hands .
By 1825 the situation had changed ,the Mexican first empire had been toppled by revolution and Spain had lost its port in San Juan de Ulua .
Despite the victory of Mexico over the last Spanish bastion in Ulúa, Spain refused to recognize the Treaty of Córdoba and hence the independence of Mexico.

San Juan de Ulua .
The Mexican government, led by Guadalupe Victoria, came to the conclusion that Spain, by its refusal to recognize the treaties, still posed a threat, and could use Cuba as a platform to launch a campaign to recover Mexico. Lucas Alamán, who was then the Mexican Minister of Foreign Affairs, assessed the threat posed by the military forces stationed in Cuba to Mexico. Since 1824, Alaman had held the belief that Mexico should seize Cuba, arguing that "Cuba without Mexico is aimed at imperialist yoke; Mexico without Cuba is a prisoner of the Gulf of Mexico." He believed that the Mexican forces, with the support of foreign powers such as France or England (which had been the first European power to recognize the independence of Mexico on July 16, 1836), could overcome the Spanish in Cuba.He also encouraged the government to take heavy loans to achieve the same .These loans were taken from the USA .
The United States insisted upon the retention of Cuba by the Spanish government. To advance its ambitions to control the island and to prevent Spanish reconquest of the mainland, the Mexican government employed Commodore David Porter of the United States to command the Mexican navy in an attack on the maritime lines of Spain patrolling the island of Cuba. This was an effort to protect the Mexican territorial sea and ensure the continued success of its independence movement on all fronts. Thus began patrols of the Mexican squadron in Spanish waters, which culminated in the unsuccessful Battle of Mariel on February 10, 1828, in which Porter commanded the brig Guerrero, mounting 22 guns, and one of the finest vessels in the small Mexican Navy. Porter's son, midshipman David Dixon Porter was killed.The survivors who surrendered were imprisoned in Havana until they could be exchanged. Commodore Porter chose to return to the USA where congress strongly commented against Mexico's 'Barbaric conquest attempts of Cuba ' which had cost the nation a 'loyal' gentleman .To further rub salt on the wounds ,many outstanding loans to the USA remained unpaid .
All this strained its relations with Spain and the United states which thus would eventually culminate in the reconquista .