Older walls were torn down to enable the expansion of the city under the grid plan of Carlos María de Castro.
[3] To defend the almudaina or Muslim citadel of Mayrit, Umayyad Emir of Cordoba Muhammad I ordered the walls to be built between the years 860 and 880[4] near the site currently occupied by the Royal Palace.
[5] According to historian Jerónimo de Quintana, the walls were "very strong of masonry and mortar, raised and thick, twelve feet [almost three and half meters] in width, with large cubes, towers gatehouses and moats.
[7] A 70 m (230 ft) long section exists under the Plaza de la Armería, formed by the main façades of the Royal Palace and Almudena Cathedral.
They were built as an extension of the original Muslim Walls to accommodate the new districts that emerged after the city passed to the Crown of Castile.
Some of the walls are integrated into the structure of various buildings of El Madrid de los Austrias, the Habsburgs's historic center of the city.
[11] After the construction of the Medieval Walls, the city continued to grow eastward, with the population increasing from 5,000 to 12,000 inhabitants from the mid-15th to early 16th century.
These were not defensive walls, but essentially served fiscal and surveillance purposes: to control the access of goods to the city, ensure the collection of taxes, and to monitor who went in and out of Madrid.