The history of the Walled City can be traced back to the Song Dynasty (960–1279), when an outpost was set up to manage the trade of salt.In 1842, during Qing Emperor Daoguang's reign, Hong Kong Island was ceded to Britain by the Treaty of Nanjing. As a result, the Qing authorities felt it necessary to improve the fort in order to rule the area and check further British influence. The improvements, including the formidable defensive wall, were completed in 1847. The Walled City was captured by rebels during the Taiping Rebellion in 1854 before being retaken a few weeks later.
The Convention for the Extension of Hong Kong Territory of 1898 handed additional parts of Hong Kong (the New Territories) to Britain for 99 years, but excluded the Walled City, which at the time had a population of roughly 700. China was allowed to continue to keep officials there as long as they did not interfere with the defence of British Hong Kong. The following year, the Governor, Sir Henry Blake, suspected that the Viceroy of Canton was using troops to aid resistance to the new arrangements. On 16 May 1899, British forces attacked the Walled City, only to find the Viceroy's soldiers gone, leaving behind only the mandarin and 150 residents.