Remove all policies assigned to a user (or the anonymous user if
    ``user`` is ``None``).

def clear_user_policies(user):
    """Remove all policies assigned to a user (or the anonymous user if
    ``user`` is ``None``).

    """
    if user is None:
        try:
            pset = PermissionSet.objects.get(anonymous_user=True)
            pset.anonymous_user = False
            pset.save()
        except ObjectDoesNotExist:
            return
    else:
        pset = user.permissionset.first()
    if pset:
        pset.refresh()
        if user is not None:
            pset.users.remove(user)
        if pset.users.count() == 0 and not pset.anonymous_user:
            pset.delete()