The recovery of precious silver (Ag) from wastewater, a dual pursuit of environmental sustainability and economic benefits, faces a formidable challenge in the selective extraction of Ag+ ions amidst a matrix of other metal ions. To address this, we developed a novel N, S co-doped hierarchical porous carbon (NSPC) derived from a renewable precursor of Camellia oleifera shell. As the electrode material for capacitive deionization (CDI), the NSPC exhibited a high electrosorption capacity of 480.5 mg g−1 for Ag+ ions under an operating voltage of 1.2 V. Owing to the strong reaction between Ag+ and S functional groups and the synergistic effect from a hierarchical porous structure and N doping, the formation of Ag2S and elemental Ag is facilitated, and the NSPC cathode could selectively remove nearly 100 % of Ag+ ions from saline water and a solution containing Ni2+, Co2+, Pb2+, Zn2+, and Fe3+ ions. Furthermore, the NSPC cathode showed a high anti-interference ability in the presence of anions (C10H14N2O82−, C4H4O62−, C5H7O5COO− and HCO3−) and excellent reusability until 9 cycles. The impressive results show that the biomass-derived NSPC has the potential for rapid and selective recovery of silver from wastewater and offers a solution for environmental remediation and metal resource recovery.