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:51:56

And I did not create the jinn and mankind except to worship Me.  

And I did not create the jinn and mankind except that they may worship Me: the fact that disbelievers do not worship ˹God˺ does not contradict this ˹statement˺, since a purpose does not have to be realised ˹in an act, for it to be valid˺, as when you may say: ‘I sharpened this pencil in order to write with it’, even though you might not actually write with it.
القرآن:٥١:٥٦

وَمَا خَلَقْتُ الْجِنَّ وَالْإِنْسَ إِلَّا لِيَعْبُدُونِ  

{وما خلقت الجن والإنس إلا ليعبدون} ولا ينافي ذلك عدم عبادة الكافرين، لأن الغاية لا يلزم وجودها كما في قولك: بريت هذا القلم لأكتب به، فإنك قد لا تكتب به.