Why Did Khidr kill a boy in the Quran?

In Chapter 18 of the Quran, Allah says, “So they proceeded until they came across a boy, and the man killed him. Moses protested, ‘Have you killed an innocent soul, who killed no one? You have certainly done a horrible thing.’ He said, ‘Did I not tell you that you cannot have patience with me?'” (Quran 18:74-75). If one continues reading, the answer within itself is explained by Khidr to Prophet Moses, “And as for the boy, his parents were ˹true˺ believers, and we feared that he would pressure them into defiance and disbelief. So we hoped that their Lord would give them another, more virtuous and caring in his place” (Quran 18:80-81). Therefore, Muslims know from these verses that Khidr was a Prophet sent by God, and he was ordered to take the life of this boy by Divine command. It is never permissible for any Muslim to kill anyone unless it is for a just reason, but in this case, it is clear that it wasn’t Khidr’s order, it was the order of Allah, and Allah orders whatever He wills. And since Allah gave him and all of us life, He can take it back whenever He wills.

And Allah knows best.