Anas bin Malik reported,

The Prophet ﷺ said, "None of you should wish for death because of some harm that has befallen him. If he must do so, let him say, 'O Allah, keep me alive as long as life is better for me, and take my soul when death is better for me.'"

Azami 14652

Sahih

عن أنس بن مالك

قال النبي ﷺ: لا يتمنين أحدكم الموت من ضر أصابه، فإن كان لا بد فاعلا، فليقل: اللهم أحيني ما كانت الحياة خيرا لي، وتوفني إذا كانت الوفاة خيرا لي.

الأعظمي 14652صحيح
Contents
Reference
Commentary
Ayat
Collection
Jami al-Kamil
Book
Superior moral conduct
Chapter
Etiquette of speech, words, and correspondence
Section
What is disliked of wishing
Numbering
Jami al-Kamil 14652 (2nd edition)
Azami 14652