It is permissible to shave or trim the beard if medical treatment requires that
Question

There is a person who we think is good – and Allah knows best – but he suffers from depression which is accompanied by a behavioural disorder called Trichotillomania (hair pulling disorder, compulsive urge to pull out one’s own hair). In some cases the person passes the hairs between his lips or folds it up between his fingers, or may go as far as eating it. This person was known to have let his beard grow long for some time, in accordance with the Sunnah, but after he was affected by this disease, his beard in particular became sparse and gaps began to appear in it because of pulling out the hair.
Is it permissible for me as a doctor to tell him to shave off his beard or trim it temporarily, as part of the treatment?
Is it permissible for him to shave it off or trim it temporarily, until he recovers from this depression, by Allah’s leave?
Praise be to Allah.We do not think that there is anything wrong, from a shar‘i point of view, with trim or shaving off the beard of a mental patient, if the doctor is certain or thinks it most likely that shaving it will be a means of treating his sickness and bringing about healing, by Allah’s leave. This is based on the following:
According to both modern and ancient medicine, mental or psychological diseases come under the heading of real diseases that may affect a person and bring about physical or mental imbalance and disorder. It is not as some people think, that regarding psychological symptoms as a disease is a kind of exaggeration, because these symptoms do not cause the same level of distress and trouble as other types of disease.
Hence the fuqaha’ in many cases have ruled that the one who is affected by compulsive thoughts or behaviours is not to be blamed, and that such thoughts and behaviours come under the heading of psychological or mental illness. It says in Radd al-Muhtaar: It was narrated from al-Layth: Divorce issued by a person who is affected by compulsive thoughts does not count as such. He said: What is meant is the one who is not in control of his thoughts. … And it was narrated from al-Haakim that he is the one who is affected by compulsive thoughts, and speaks at random.  End quote.
We have discussed this previously on our website in question no. 39684
In fact they said that it is permissible for one who is affected by compulsive thoughts to avail himself of concessions, if that will treat his problem and lead to healing from it. It says in I‘aanah at-Taalibeen (4/250) by ad-Dimyaati ash-Shaafa‘i: What is preferable for the one who is suffering from compulsive thoughts is to avail himself of lighter options and concessions, so that his problem will not be exacerbated, which could lead to him going beyond the limits set by sharee‘ah because of this compulsive thinking. For example, a person may be affected with compulsive thoughts that make him always doubt whether he formed the intention to do wudoo’ or whether he recited al-Faatihah behind the imam or not, so he begins to spend more time in wudoo’ or prayer. In that case he may forego forming the niyyah (intention), and follow Imam Abu Haneefah with regard to this matter, because in Abu Haneefah’s view the niyyah is Sunnah; and he may follow his opinion in foregoing recitation of al-Faatihah when praying behind the imam, until the compulsive thoughts cease. End quote.
When Allah, may He be glorified and exalted, said that there was no blame on the sick, He said (blessings and peace of Allah be upon him): “…nor any blame upon the sick…” [an-Noor 24:61], He made the need for medical treatment a reason for lifting the prohibition. Allah, may He be glorified, says (interpretation of the meaning): “He has explained to you in detail what is forbidden to you, except under compulsion of necessity” [al-An‘aam 6:119].
Thus it appears to be the case that we should be inclined to absolve your sick friend of any blame in his situation, if he trims his beard as much as is necessary to ward off this harm from him and make it so that he will not be able to pluck it, and thus he will no longer be harmed because of it. But if trimming it does not serve any benefit and his sickness cannot be dealt with in that manner, then we do not think that there is anything wrong with asking him to shave his beard, until he has gotten over this problem. Then he may return to the Sunnah of the Prophet (blessings and peace of Allah be upon him), as soon as his treatment is over. By doing so he should seek reward with Allah, may He be exalted, (for the troubles he is going through), call upon Him and ask Him to forgive him and to decree for him healing from every disease.
All of that is – as we stated at the beginning of this answer – on condition that the doctor should think it most likely that shaving or trimming the beard will be a means of healing, by Allah’s leave, and is not just a matter of imagination or speculation.
For more information, please see fatwa no.  70319 .
And Allah knows best.