The removal of four pharmaceutically active compounds, i.e., anti-inflammatory painkiller diclofenac (DCF), lipid-regulating agent clofibric acid (CFA), epilepsy drugs carbamazepine (CBM), and broad-spectrum anti-bacterial agent triclosan (TCS), present in sewage sludge was investigated using anaerobic digestion processes in the mesophilic and thermophilic modes. Sludge retention times (SRTs) were set at 10, 15, and 20 days, respectively, for the mesophilic mode and 7, 15, and 20 days, respectively, for the thermophilic mode. The effective isolation and purification pre-treatment to extract the target compounds from the sewage sludge samples were firstly established, followed by gas chromatography–mass spectrometer analysis to identify and quantify them. The removal efficiencies of the target compounds could be raised to a certain extent with the increase on SRTs, especially under the mesophilic condition. The removal of CFA and TCS under thermophilic condition hardly varied when the SRTs were above 15 days. All the compounds could be partly removed from the sewage sludge under the two temperature conditions, particularly TCS that was reduced by about 74 %. Besides, CFA, firstly reported in this study, could be reduced by maximal 65 %. On the whole, the thermophilic mode was more conducive to the removal of CBM and TCS, but did not have a noticeable effect on the removal of CFA. Furthermore, DCF could be better removed in the mesophilic mode. In addition, NH3–N accumulation in this measured range might positively affect the removal of the selected compounds, particularly in the mesophilic mode.