In this study, we developed an in-situ deposition method to load small-sized AgI nanoparticles as an oxidative photocatalyst (OP) onto phosphorus (P) and potassium (K) co-doped two-dimensional porous g-C3N4 (PK-CN-N) as a reductive photocatalyst (RP), forming an S-scheme heterojunction photocatalyst AgI/PK-CN-N. PK-CN-N achieved morphology control and element doping, leading to surface functionalization and heterostructure formation for the AgI/PK-CN-N photocatalyst. Notably, the 50AgI/PK-CN-N composite demonstrated an approximately 95 % removal efficiency for RhB within 30 minutes, 17.6 times higher than pure g-C3N4, surpassing most reported g-C3N4-based photocatalysts. The enhanced photocatalytic efficiency is attributed to the surface modification strategy and heterostructure formation of g-C3N4, which broadens the visible light response range, increases the number of active sites, improves the separation of photogenerated electron-hole pairs, and enhances REDOX capabilities. The band structure of the composite was elucidated through Mott-Schottky analysis and UV–vis DRS. The formation of the AgI/PK-CN-N S-scheme heterojunction and its charge transfer mechanism was confirmed through XPS, band structure analysis, KPFM, and EPR spectroscopy. Experimental data confirmed the presence of a strong and effective interface electric field between the two semiconductors, leading to band bending and accelerated charge separation. Additionally, the introduction of small-sized AgI semiconductors enhanced the exposure of the coupling interface, further improving catalytic performance. The reusability and lifespan of the catalyst were also analyzed, showing that after four cycles, the photocatalytic activity remained above 92 %.