The castle suffered heavy damage from the 1755 earthquake, particularly the Palace of the Alcaides and some of the walls. In 1809, it was integrated into the Lines of Torres Vedras, which were defensive lines established by the Duke of Wellington to protect against French invasion during the Peninsular War. Together with the Fort of São Vicente on a hill immediately to its north, the castle, with about 500 soldiers, guarded the main road between Coimbra and Lisbon. As part of the adaptation by the British Royal Engineers for this purpose the castle door was demolished. During the Liberal Wars (or Portuguese Civil War), sections of the walls facing east and some turrets on the north side were rebuilt. During the so-called Little Civil War in 1846, it served as barracks for the troops of the Count of Bonfim and was bombarded by the forces of the Duke of Saldanha, which caused the almost total ruin of the Palace. Despite its poor condition, the castle continued to function as a barracks until the end of the 19th century.