Caution: Translations of Quran and Ḥadīth may lead to possible misapplications and misinterpretations. This site is intended for students of sacred knowledge that are proficient in comprehending classical Arabic and have a strong foundation in Islamic sciences. Also note that religious injunctions rely on several aspects beyond what one may glean through reading individual aḥādīth.
quran:65:2

And when they have ˹nearly˺ fulfilled their term, either retain them according to acceptable terms or part with them according to acceptable terms. And bring to witness two just men from among you and establish the testimony for ˹the acceptance of˺ Allah. That is instructed to whoever should believe in Allah and the Last day. And whoever fears Allah - He will make for him a way out  

Then, when they have reached their term, ˹when˺ they are near the end of their prescribed period, retain them, by taking them back, honourably, without coercion, or separate from them honourably, leave them to conclude their waiting period and do not compel them to go back ˹to you˺. And call to witness two just men from among yourselves, ˹to witness˺ the retraction or the separation, and bear witness for the sake of God, and not ˹merely˺ for the sake of what is being witnessed or for the sake of the man. By this is exhorted whoever believes in God and the Last Day. And whoever fears God, He will make a way out for him, from the distress of this world and the Hereafter;
القرآن:٦٥:٢

فَإِذَا بَلَغْنَ أَجَلَهُنَّ فَأَمْسِكُوهُنَّ بِمَعْرُوفٍ أَوْ فَارِقُوهُنَّ بِمَعْرُوفٍ وَأَشْهِدُوا ذَوَيْ عَدْلٍ مِنْكُمْ وَأَقِيمُوا الشَّهَادَةَ لِلَّهِ ۚ ذَٰلِكُمْ يُوعَظُ بِهِ مَنْ كَانَ يُؤْمِنُ بِاللَّهِ وَالْيَوْمِ الْآخِرِ ۚ وَمَنْ يَتَّقِ اللَّهَ يَجْعَلْ لَهُ مَخْرَجًا  

{فإذا بلغن أجلهن} قاربن انقضاء عدتهن {فأمسكوهن} بأن تراجعوهن {بمعروف} من غير ضرار {أو فارقوهن بمعروف} أتركوهن حتى تنقضي عدتهن ولا تضاروهن بالمراجعة {وأشهدوا ذوي عدلٍ منكم} على المراجعة أو الفراق {وأقيموا الشهادة لله} لا للمشهود عليه أو له {ذلكم يوعظ به من كان يؤمن بالله واليوم الآخر ومن يتق الله يجعل له مخرجاً} من كرب الدنيا والآخرة.

See similar narrations below:

Collected by Dārimī
darimi:2767ʿUthmān b. Muḥammad > Muʿtamir > Kahmas b. al-Ḥasan > Abū al-Salīl > Abū Dhar

[Machine] The Messenger of Allah ﷺ said, "Verily, I know a verse that if people adhere to it, it would be sufficient for them. It is when their appointed term arrives, they are to either retain them according to acceptable terms or separate them according to acceptable terms and bring forth witnesses from among your men of justice. And establish the testimony for Allah. That is advised for whoever believes in Allah and the Last Day. And whoever fears Allah - He will make for him a way out."  

الدارمي:٢٧٦٧حَدَّثَنَا عُثْمَانُ بْنُ مُحَمَّدٍ حَدَّثَنَا مُعْتَمِرٌ عَنْ كَهْمَسِ بْنِ الْحَسَنِ عَنْ أَبِي السَّلِيلِ عَنْ أَبِي ذَرٍّ قَالَ

قَالَ رَسُولُ اللَّهِ ﷺ إِنِّي لَأَعْلَمُ آيَةً لَوْ أَخَذَ بِهَا النَّاسُ لَكَفَتْهُمْ {فَإِذَا بَلَغْنَ أَجَلَهُنَّ فَأَمْسِكُوهُنَّ بِمَعْرُوفٍ أَوْ فَارِقُوهُنَّ بِمَعْرُوفٍ وَأَشْهِدُوا ذَوَىْ عَدْلٍ مِنْكُمْ وَأَقِيمُوا الشَّهَادَةَ لِلَّهِ ذَلِكُمْ يُوعَظُ بِهِ مَنْ كَانَ يُؤْمِنُ بِاللَّهِ وَالْيَوْمِ الْأخِرِ وَمَنْ يَتَّقِ اللَّهَ يَجْعَلْ لَهُ مَخْرَجًا}