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 absolutely must wish for death, let him say, 'Allah, keep me alive so long as life is better for me, and take me when death is better for me.'"

Bukhari 6351

Sahih

حدثنا ابن سلام أخبرنا إسماعيل بن علية عن عبد العزيز بن صهيب

عن أنس رضي الله عنه قال

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

البخاري 6351صحيح
Contents
Reference
Commentary
Ayat
Collection
Sahih Bukhari
Book
Supplications
Chapter
Supplication for death and life
Numbering
Darussalam 6351
Bukhari 6351