Piwi-interacting RNAs (piRNAs) are an animal-specific class of germline-enriched small non-coding RNAs that shape transcriptome, as well as ensure genomic integrity and fertility by regulating transposons and other selfish genetic elements. In Caenorhabditis elegans mature piRNAs are 21-nucleotides long, begin with a monophosphorylated uridine, and they associate with PRG-1 to form piRISCs that scan the transcriptome for non-self sequences. However, these piRNAs are born as longer 5-capped transcripts, where PARN-1, a 3-5 exoribonuclease, contributes to the formation of the mature 3-end. But, till date, the 5-processing events remain elusive. We demonstrate that the recently identified endoribonuclease activity of XRN-2 is involved in the processing of the 5-end of precursor piRNAs in worms. Depletion of XRN-2 results in reduced mature piRNA levels, with concomitant increase in levels of the 5-capped precursors. We also reveal that the piRNAs born as longer precursor molecules (>60 nt), prior to 5-end processing, undergo ENDU-1-mediated endoribonucleolytic processing of their 3-ends. Our in vitro RNA-protein interaction studies unravel the mechanistic interactions between XRN-2 and PRG-1 towards the formation of mature 5-ends of piRNAs. In vivo experiments employing prg-1 mutant worms indicate that XRN-2 has the potential to perform clearance of precursors that are not bound and protected by PRG-1. Finally, we also demonstrate that XRN-2 is not only important for the generation of mature piRNAs and piRNA-dependent endo-siRNAs, but through yet unknown pathways, it also affects piRNA-independent endo-siRNAs that shape transcriptome, as well as contribute to genomic integrity via regulation of transposable elements.