Umar ibn al-Khattab and Uthman ibn Affan ruled that people who had committed adultery but claimed that they did not know it was forbidden in Islam should not be punished.
It is mentioned in al Musannaf Abdul Razzaq
باب لا حد إلا على من علمه
13642 – عبد الرزاق عن معمر عن عمرو بن دينار عن ابن المسيب أن عاملا لعمر – قال معمر: وسمعت غير عمرو يزعم أن أبا عبيدة ابن الجراح – كتب إلى عمر أن رجلا اعترف عبده بالزنا، فكتب إليه أن يسأله: هل كان يعلم أنه حرام؟ فإن قال: نعم، فأقم عليه حد الله، وإن قال: لا، فأعلمه أنه حرام، فإن عاد فاحدده.
13643 – عبد الرزاق عن ابن عيينة عن عمرو بن دينار عن ابن المسيب قال: ذكروا الزنا بالشام، فقال رجل: زنيت، قيل:
ما تقول؟ قال: أو حرمه الله، قال : ما علمت أن الله حرمه، فكتب إلى عمر بن الخطاب، فكتب: إن كان علم أن الله حرمه فحدوه ، وإن كان لم يعلم، فعلموه ، وإن عاد فحدوه
Chapter: There is no punishment except for those who know it
13642 – Abdulrazaq from Ma’mar from Amr ibn Dinar from Ibn al-Musayyab that a governor for Umar – (Ma’mar said: I heard someone other than Amr claim that it was Abu Ubaydah ibn al-Jarrah -) wrote to Umar that a man’s slave confessed to adultery, so he wrote to him to ask him: Did he know that it was forbidden? If he said: Yes, then establish the punishment of Allah upon him, and if he said: No, then teach him that it is forbidden, and if he repeats it, then punish him.
13643 – Abdulrazaq from Ibn Uyayna from Amr ibn Dinar from Ibn al-Musayyab said: They mentioned adultery in Syria, and a man said: I committed adultery, they said: What do you say? He said: Or did Allah forbid it? He said: I did not know that Allah forbade it, so he wrote to Umar ibn al-Khattab, and he wrote: If he knew that Allah forbade it, then punish him, and if he did not know, then teach him, and if he repeats it, then punish him.
[al Musannaf Abdul Razzaq 7/403-404, al Muhalla Ibn Hazam 12/107, Umar ibn al Khattab by Dr Ali Muhammad as-Salabi 286]Qaadi Shawkani and Ibn hajr mentioned:
وعن عثمان أنهما عذرا جارية زنت وهي أعجمية وادعت أنها لم تعلم التحريم.
And it is narrated from Uthman that they acquitted a slave girl who had committed adultery who was ajmi and claimed she didn’t know about the prohibition.
(Nayl al awtar 7/272 and Talkhees al Habeer vol 4 book “Hadd al Zina” hadith no. 2059, It is mentioned in Ila al Sunan vol 11 page 470 that silence of al Hafidh Ibn hajar in Talkhees al Habeer is evidence that it is saheeh or hasan.)