The Messenger of Allah ﷺ said, "Do not call for death, nor wish for it. Whoever feels he must make such a supplication, 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 reported it in "Illness," chapter on the sick person wishing for death, no. 5671, 6351, and Muslim reported it in "Remembrance and Supplication," chapter on the dislike of wishing for death because of harm that has befallen one, no. 2680/10, from the hadith of Thabit Bunani, through him. It is also in Kubra, no. 1948.Yunus is Ibn Ubayd.