The main reason Spain agreed so readily to sell Florida to the United States after so many years of actively colonizing it was:
Spain was busy fighting rebels in Latin America.
During this period, Spain was focused on suppressing independence movements in its Latin American colonies, which made it difficult for the country to maintain control over Florida.