Apologies for not being clearer. We know that
aP removeaction 0
means, remove the action with the ID 0. No problems there. Now, due to a bug its kinda hard to tell what the ID of an action is, but we do know that the first action to be added to a unit gets the ID 0, the second one gets ID 1 and so on.
So the first time you add the action the action ID is 0 and your removeaction command will work. The second time the action is added, even though its the same action, the ID is 1. So your command to removeaction 0 will not do anything because there is no action 0, there is only an action 1.
If you were to add this line to your script, next to the existing removeaction line
aP removeaction 1
you would find that the second time the action would be removed. But not the third time, for that you would need
aP removeaction 2
and so on. What we want is a removeallactions command, but I don't think it exists. Maybe you could try
"aP removeaction _x" forEach [0,1,2,3,4,5,6,7,8,9]
syntax not guaranteed. Anyway, somebody must know a better way, I'm just making this up.
Does that help? The summary is add this line
aP removeaction 1
and see what happens.