عَنْ أَنَسَ بْنِ مَالِكَ رَضِيَ اللَّهُ عَنْهُ يُحَدِّثُ عَنِ النَّبِيِّ صَلَّى اللَّهُ عَلَيْهِ وَآلِهِ وَسَلَّمَ أَنَّهُ قَالَ: ((لَا يَتَمَنَّ أَحَدُكُمْ الْمَوْتَ مِنْ ضُرٍّ أَصَابَهُ، فَإِنْ كَانَ لَا بُدَّ فَاعِلًا فَلْيَقُلْ: اللَّهُمَّ أَحْيِنِي مَا كَانَتِ الْحَيَاةُ خَيْرًا لِي، وَتَوَفَّنِي مَا كَانَتِ الْوَفَاةُ خَيْرًا لِي))
It is narrated from Sayyiduna Anas bin Malik (may Allah be pleased with him) that the Noble Prophet (peace and blessings be upon him) said: None of you should wish for death because of any hardship; and if he has no other option, then he should say this supplication: "O Allah! Keep me alive as long as life is better for me, and cause me to die when death is better for me."