This verse is from Soorat al-Kahf, which is a Makkan soorah (revealed in Makkah). It was revealed concerning the kuffaar idol-worshippers whose devils lead them astray and made their actions seem good to them, so they thought that they were guided and that the Muslims were misguided. Allah, may He be exalted, tells us about that in the verse (interpretation of the meaning): “And verily, they (Satans / devils) hinder them from the Path (of Allah), but they think that they are guided aright!” [az-Zukhruf 43:37]. And He, may He be exalted, said (interpretation of the meaning): “A group He has guided, and a group deserved to be in error; (because) surely they took the Shayatin (devils) as Awliya (protectors and helpers) instead of Allah, and consider that they are guided” [al-A‘raaf 7:30]. And Allah, may He be exalted, stated that they are disbelievers when He said in the same passage (that you asked about): “They are those who deny the Ayat (proofs, evidences, verses, lessons, signs, revelations, etc.) of their Lord and the Meeting with Him (in the Hereafter).” [al-Kahf 18:104].
The fact that these verses were revealed concerning the mushrikeen is something about which there can be no doubt, because this verse is Makkan (revealed in Makkah), as we stated above. This does not mean that it cannot include others; it also includes the Jews and Christians, according to the wording, the meaning and the ruling. And it includes the followers of innovation and misguidance, commensurate with what their beliefs and actions include of misguidance and things that are contrary to sharee‘ah. So the nullification of their good deeds – in these verses – is complete in the case of the kuffaar, mushrikeen and apostates; as for the followers of innovation and misguidance among the Muslims, their deeds are not nullified completely; rather those that are innovated and contrary to sharee‘ah are nullified.
Ibn Katheer (may Allah have mercy on him) said: al-Bukhaari narrated that Mus‘ab said: I asked my father – i.e., Sa‘d ibn Abi Waqqaas – “Say (O Muhammad SAW): Shall We tell you the greatest losers in respect of (their) deeds?”: does this refer to the Harooris (Khaarijis)? He said: No; they are the Jews and the Christians. As for the Jews, they rejected Muhammad (blessings and peace of Allah be upon him); as for the Christians, they disbelieved in Paradise and said that there is no food or drink in it. The Harooris are the one who broke Allah’s Covenant after ratifying it (cf. al-Baqarah 2:29). and Sa‘d (may Allah be pleased with him) used to call them the evildoers (al-faasiqeen).
‘Ali ibn Abi Taalib, ad-Dahhaak and others said: They are the Harooris, i.e., the Khaarijis.
What is meant by these words of ‘Ali (may Allah be pleased with him) is that this verse includes the Harooris as well as the Jews, Christians and others; it does not mean that it was revealed specifically concerning the former and not the latter. In fact it is more general in meaning than this; this verse was revealed in Makkah, before the message was addressed to the Jews and Christians, and before the Khaarijis even existed. It is general in meaning and applies to everyone who worships Allah in any way that is not pleasing to Him, thinking that he has got it right and that his deeds are acceptable, when in fact he has got it wrong and his deeds are rejected, as Allah, may He be exalted, says (interpretation of the meaning):
“Some faces, that Day, will be humiliated (in the Hell-fire, i.e. the faces of all disbelievers)
Labouring (hard in the worldly life by worshipping others besides Allah), weary (in the Hereafter with humility and disgrace).
They will enter in the hot blazing Fire”
“And We shall turn to whatever deeds they (disbelievers, polytheists, sinners, etc.) did, and We shall make such deeds as scattered floating particles of dust”
“As for those who disbelieve, their deeds are like a mirage in a desert. The thirsty one thinks it to be water, until he comes up to it, he finds it to be nothing”
And in this verse, He says: “Say: Shall We tell you” i.e., shall We inform you
“the greatest losers in respect of (their) deeds”. Then He explains who they are and says:
“Those whose efforts have been wasted in this life” i.e., they did invalid deeds that were not in accordance with sharee‘ah, and were not prescribed, pleasing to Allah or acceptable.
“while they thought that they were acquiring good by their deeds” i.e., they believed that they were doing something good, and that they were accepted and loved.
End quote from Tafseer Ibn Katheer, 5/201, 202
It is worth noting that Shaykh al-Islam Ibn Taymiyah (may Allah have mercy on him) commented on the verses from al-Ghaashiyah – which are mentioned above in the quotation from Ibn Katheer – and said that they refer to the Day of Resurrection, and that the faces of the disbelievers on that day will be humiliated, that is disgraced. But that is not in this world. See Majmoo‘ al-Fataawa, 16/217-220
Thus it is known that the verse does not include the one who disobeys Allah, may He be exalted, and shaves his beard. Rather the verses speak about the worship of anything other than Allah, and it is known that the verse was originally revealed about the disbelievers, and the inclusion of the followers of innovation under that heading is done of the basis of analogy.
The hadeeth of ‘Abdullah ibn ‘Umar (may Allah be pleased with him) – which states that the Messenger of Allah (blessings and peace of Allah be upon him) said: “O Allaah, have mercy on those who shaved their heads.” They said: And those who cut their hair, O Messenger of Allaah? He said: “May Allaah have mercy on those who shaved their heads.” They said: And those who cut their hair, O Messenger of Allaah? He said: “And those who cut their hair” (narrated by al-Bukhaari, 1640; Muslim, 1301) – has nothing to do with shaving or cutting the beard; rather it undoubtedly has to do with exiting ihram following ‘Umrah or Hajj by shaving the hair of the head. We do not think that any Muslim would dare to say that shaving (when exiting ihram) includes shaving the beard. The one who says that is extremely ignorant and he should be taught and disciplined, because he has spoken about Allah, may He be exalted, without knowledge. If the one who said that is someone who claims to have knowledge, then undoubtedly his saying that is a mockery and is toying with the verses of Allah and distorting the words.
And Allah knows best.