Anas bin Malik reported,

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

Azami 10954

Sahih

عن أنس قال

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

الأعظمي 10954صحيح
Contents
Reference
Commentary
Ayat
Collection
Jami al-Kamil
Book
Supplications, remembrances, and prayer upon the chosen Prophet ﷺ
Chapter
Rulings on supplication
Section
Prohibition of supplicating for death
Numbering
Jami al-Kamil 10954 (2nd edition)
Azami 10954