How to force quit of a detached screen session

Here is how to terminate a stuck screen command using a script. As part of my job I deal with a legacy Linux setup comprising a range of screens. Every now and then some of them freeze due to memory issues and attaching a session doesn't work anymore. I was wrapping my head around how to avoid tedious manual resets of the dead screens. Turns out all can be easily automated.

Googling around quickly reveals how to terminate a screen in the correct way:
 screen -S session_name -X quit  
That however won't work when the screen is totally inaccessible. Not even when an explicit kill attempt is made:
 screen -S session_name -X kill  
Clearly, the screen command is not as powerful as one would wish. The only thing which did work for me was the old good kill -9

Now, I wanted this to be automated, so the usual sequence of screen -ls to get to the right pid and then manual copy-paste to the kill command was exactly what I was looking to skip. Here is what I ended up with instead:
 PID=`pgrep -f "SCREEN.*$1"`  
 kill -9 $PID  
 screen -wipe $1  
As you can see, the pid is obtained automatically by the means of a regular expression. The argument ($1) is the session_name. The SCREEN part of the regex is static and identifies a process running as a screen. So basically, the regular expression merely says "Get me a screen called ..", whereas pgrep fetches the pid.

Note that it is not sufficient to just kill the running process, as the associated screen still hangs around and is flagged as dead. It is a good idea to apply screen -wipe to remove the session entirely. 

These few commands form a base of a larger restart script I use to manage the whole lot.