First, An Dehai is too arrogant.
An Dehai, relying on Cixi's love for him, was lawless and ran amok in the palace. The palace ministers had long disliked him.
Second, Emperor Tongzhi regarded him as a thorn in his side and hated him deeply.
As the eyes of Cixi in the palace, An Dehai often spoke ill of the Emperor Tongzhi in front of Cixi, which led to his punishment, and the Emperor Tongzhi held a grudge against him.
Third, it is the direct cause. An Dehai didn't leave the palace.
With the acquiescence of Cixi, An Dehai bought wedding supplies and went out of the palace. In the name of purchasing, he claimed to be an imperial envoy and amassed wealth. As a result, Ding Baozhen found that the report was given to the Emperor Tongzhi who was not pleasing to the eye. In addition, An Dehai is the default of Cixi, and there is no official document leaving the palace. According to the rules set by the emperor shunzhi, eunuchs are not allowed to leave the palace without approval. What's more, he claimed to be an imperial envoy and was punished according to law, so he was prosecuted.
Cixi was very angry about An Dehai's murder, but even if Cixi liked An Dehai again, she had to consider the influence of North Korea. She couldn't conflict with the minister of the DPRK, so she ordered Ding Baozhen to solve the remaining eunuchs, and Ding Baozhen was promoted constantly.
These are the reasons why Ding Baozhen killed An Dehai, the favorite of Cixi, and was promoted instead of being punished.
The above is just a personal opinion.