if its only a small bathroom with cheap bathroom carpet fitted,i would replace it.
or
apply LOTS OF defoamer powder, over the carpet clean ,using acid rinse.
this will need to be done a few times to remove the foam.
Lee G
ps
i would replace the carpet.