Charles IV inherited the Spanish crown when he was 40 years old but he was not interested in politics so Manuel Godoy was appointed as prime minister.
This reign shows the decline of the Old Regime in Spain due to the influence of French Revolution.
As soon as the Bastille was stormed, Spanish borders were closed and enlightenment reforms stopped. These measures were taken to avoid revolution spread. After Louis XVI execution in 1793, Spain and France were in war for two years so Basque Country and Navara were invaded and Godoy signed the Peace of Basel in 1795.
Once Napoleon seized power, Spain allied with France suffering a severe defeat at the naval battle of Trafalgar in 1805.
Later both countries signed the Treaty of Fontainebleau in 1807 to invade and divide Portugal which did not follow the continental blockade. This was an excellent excuse to invade Spain too as from the beginning French troops occupied strategic points of the Peninsula.
As a reaction to this, the Aranjuez Mutiny broke up. The popular revolt against Manuel Godoy´s policies was instigated by the heir to the throne, Ferdinand.
Therefore, Godoy was removed and Charles IV abdicated in favour of Ferdinand VII. Napoleon took advantage of this family affairs and called Charles IV, Ferdinand VII and all the masculine members of the Spanish Royal Family for a meeting in France to solve the disputes. Once they were in France, Napoleon force Charles and Ferdinand´s abdications in his favor and he yielded the Spanish Crown to his brother Joseph Bonaparte. This is known as the Abdications of Bayonne.