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.
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.
And Allah knows best.