Praise be to Allah.
The basic principle is that the actions of the Prophet (peace and blessings of Allaah be upon him) before his mission began are outside of the legislative Sunnah, and that we are not required to follow him or take him as an example in that regard, except in cases where sharee’ah – after his mission began – affirmed that something was prescribed or Islamically acceptable, either by way of being obligatory or mustahabb, such as fulfilling promises, helping the destitute, honouring guests, helping people when calamity strikes and so on. In that case we are obliged to follow his example, because it became a law for us after his Prophethood began, and not just because he (peace and blessings of Allaah be upon him) did it before he was a Prophet. Based on this, there are some actions that he did before his mission began that we are not obliged to follow, either because there is no proof that it is prescribed for us or because there are reports to show that it was abrogated after his mission began.
Shaykh al-Islam Ibn Taymiyah (may Allaah have mercy on him) said: The things that happened before Prophethood are not mentioned as things to be followed or that are legislated, as is the case with his actions after Prophethood, because the Muslims are unanimously agreed that what is obligatory with regard to believing in him (peace and blessings of Allaah be upon him) and following what he brought, is that which has to do with what happened after he became the Prophet.
Hence whoever stays away from Jumu’ah prayer and prayers in congregation, and lives alone in caves and mountains, where there is no Jumu’ah or prayer in congregation, and claims to be following the example of the Prophet (peace and blessings of Allaah be upon him) because he was used to worship alone in the cave of Hira’ before his Prophethood, thus forsaking the acts of worship that were prescribed and enjoined by Allaah and His Messenger, and following the example of what he used to do before Prophethood, is wrong. That is because, after Allaah honoured him with Prophethood, the Prophet (peace and blessings of Allaah be upon him) no longer did what he used to do before that, of worshipping alone in the cave of Hira and so on.
None of his Sahaabah after him went to the Cave of Hira’, and they did not stay away from Jumu’ah prayer or prayers in congregation in isolated places, and none of them observed any kind of forty-day retreat, as some of the later Muslims did. Rather they worshipped Allaah by means of the prescribed acts of worship that the Prophet (peace and blessings of Allaah be upon him) ordained for them. End quote from Majmoo’ al-Fataawa (18/10).
With regard to the muhaddithoon (scholars of hadeeth), they paid attention to what was narrated from the Prophet (peace and blessings of Allaah be upon him) at all times, even if it was before his mission began. That is part of the Sunnah according to the definition of the muhaddithoon, who regard everything that was narrated from the Prophet (peace and blessings of Allaah be upon him) as Sunnah, so they narrated it in their books on that basis.
See: Af’aal al-Rasool sall-Allaahu ‘alayhi wa sallam wa Dalaalatuhaa ‘ala al-Ahkaam, by Dr. Muhammad al-‘Aroosi (149).
And Allaah knows best.