During the early days of the Peruvian War of Independence, he joined forces with the Royalists, taking care of the Real Felipe Fortress, in the main port of the Viceroyalty, Callao. Viceroy José de la Serna abandoned the capital on June 6, 1821, leaving him with explicit orders to resist and wait for reinforcements. He successfully stopped all attempts to capture the fort for nearly 4 months, until the arrival of General José Canterac and a powerful division sent by Viceroy de la Serna gave him the orders to surrender the fort due to the lack of supplies and troops. On September 19, the garrison surrendered, in the Baquijano Capitulation, only two days after de la Mar finally submitted his left foot to amputation, having initially refused treatment of a gangrenous toe.