Is it normal that removing a sibling group changes the visibility of another group?

I have a ExtendScript script that works on PSD files, which have 2 main groups, say, A and B, and usually it also has another art layer. So, the structure of my PSDs look like this (although not always like this)

A/
B/
mylayer

Now, the script might work on A and B separately, depending on an option. So, first, it usually removes B and works on A, then removes A and works on B. Of course, it works on a duplicate.

Now, what I’ve observed is that, if both A and B are invisible in photoshop before I run the script, then I execute the script and B is removed, A becomes visible again. Of course, I don’t want this, because I have some logic that strictly depends on the original visibility of the layers. This seems to happen only with the top groups A and B, not the subgroups, that is, if I make any of the subgroups of A or B invisible, their visibility will not change, thankfully. But still, I want my script not to modify the visibility of A and B.

From what I’ve observed, what is causing this issue is really the removal of B, i.e. when I call remove() on B. I thought that maybe Photoshop requires at least one layer to be visible, but mylayer is always visible, so that cannot be the reason. Moreover, when I remove A, the visibility of B does not change. So, it happens only to the top group. In fact, I was testing this hypothesis by introducing a new group above A and, after this, the visibility of A doesn’t change, only of the newly introduced group.

Do you know if this is a known issue with Photoshop scripting or how to avoid it? I could save the original visibility of the groups before the removal and restore it later, but I think this should not happen in the first place, so maybe I’m doing something wrong. Anyway, I’d like to know why Photoshop does this, if I’m not doing something wrong.

Here’s my current setup, but this probably happens with other versions

  • photoshopVersion: 26.3.0
  • scriptingVersion: 26.3