MV Actor removed when state removed

I am trying to create a state whereby the actor who is affected by it is removed from the party when the state is removed. This should occur either when the state is forcibly removed, the actor dies or the battle ends. I am using Yanfly's Buffs & States Core and know that the code has to go in both the <Custom Remove Effect> section, the <Custom Leave Effect> and possibly the <Custom Victory Effect> section, but I can't work out what the script should be.
Does anyone know the JavaScript for doing this?
The super handy spread sheet of javascript equivalents to event commands tells us, that the script call to remove an actor from the party is:
JavaScript:
$gameParty.removeActor(actorId);
Now we only need to figure out the actorId of the guy who has the state. I believe we can access that with user._actorId.
So put together it would be:
JavaScript:
$gameParty.removeActor(user._actorId);
Thanks!

I think I've got it just about working. For some reason, the actor remains after being knocked out, but I can more-or-less fix that using turn-spanning battle events that remove each actor from the party if they have 0 HP while afflicted with the state and Yanfly's Base Troop Events Plugin.
For some reason, the actor remains after being knocked out
Custom Remove Effects don't execute when the state is removed by death. You have to work around it another way (you should be able to add a Custom Apply Effect to state 1 that checks for the presence of the other state and works accordingly. I'm not positive whether state 1 is added before the other states are stripped, I'd have to look at the code, but it's worth trying).
Got it. Adding the following code to the Knockout state seems to work:

<Custom Apply Effect>
if (user.isStateAffected(57)) {
$gameParty.removeActor(user._actorId);
}
</Custom Apply Effect>

Thanks!

Original thread: https://forums.rpgmakerweb.com/threads/actor-removed-when-state-removed.143592/