Accessing old version of referenced table in trigger function - Postgresql
I didn't look at the code, but obviously the row is deleted from grouplayer before the BEFORE trigger on layer is executed.

Perhaps you can perform the task in a BEFORE trigger on layer.