The increasing accumulation of pharmaceuticals in aquatic ecosystems can degrade the freshwater quality and endanger human health. In our work, we will discuss the removal of three pharmaceuticals that are most detected in aquatic environments: ketoprofen (KTP), diclofenac (DCF), and sulfamethoxazole (SMX). The adsorption process removed these toxic pollutants on a bio-sourced activated carbon (AC) prepared from walnut shells (Ws) from the Atlas El Kabir -Marrakech region. MEB, elemental mapping, EDX, FTIR, and BET surface area characterized the properties of prepared activated carbon. The activated carbon obtained had a high surface area (2256 m2/g). The experimental design methodology to optimize the adsorption process using the AZURAD® software. This approach was applied to remove three pharmaceutical pollutants in an aqueous solution by AC-Ws. The effect of four independent variables, including pH, the dose of AC-Ws, and temperature (T), on the PPs elimination efficiency was studied. Using the desirability function approach, commonly optimized conditions for adsorption of the three pharmaceuticals (DCF, SMX, and KTP) were assessed with pH 4, the dose of AC-Ws 0.28g/L, and a Temperature of 22 °C. At these conditions, the removal efficiency was 99.95%, 99.16%, and 99% for DCF, SMX, and KTP, respectively. Freundlich's model describes the adsorption of two pharmaceuticals efficiently: SMX and DCF, but the Langmuir model characterizes the adsorption of KTP. The pseudo-first-order model provided a satisfactory explanation for PPs kinetic adsorption over walnut shell adsorbent. The thermodynamic parameters revealed that the adsorption was spontaneous and exothermic for KTP and SMX, whereas DCF adsorbs randomly and endothermic on the activated carbon. The Boehm method determined the organic functions present on the surface of the adsorbent. The pharmaceutical adsorption mechanism over AC-Ws surface sites was further evaluated using the density functional theory (DFT) method. Therefore, the material prepared in this work possesses intrinsic characteristics that make it an excellent adsorbent to treat pharmaceutical contaminants in wastewater.