In 1839, during the First Anglo-Afghan War, British-led Indian forces invaded Afghanistan and initiated a war with the Afghan rulers. Two years later, in 1842, the British were defeated and the war ended. The British again invaded Afghanistan in 1878, during the Second Anglo-Afghan War. The British were successful in installing an Amir - Abdur Rahman Khan and the Treaty of Gandamak was signed in 1880. Afghanistan ceded control of various frontier areas to the British Empire. In addition to having attained all of their geopolitical objectives the British withdrew.