O’Connor joined the United Army of Liberation in Peru in 1824, and six months later Bolívar appointed him chief of staff. He fought at the Battle of Junín in August 1824 against heavy odds and chose the site of the Battle of Ayacucho. In 1825 Antonio José de Sucre chose him to direct the Campaign in Upper Peru, the final operation of the war, the pursuit and elimination of general Pedro Antonio Olañeta, the last royalist commander to offer resistance.