2

I am trying to loop through all sites on a multisite network, and for each site, delete all subscriber users. I have tried this WP-CLI command:

wp site list --field=url | xargs -n 1 -I ^ wp user list --url=^ --role=subscriber --field=ID | xargs -n 2 -I % ^ wp user delete % --url=^ --reassign=4

I can't find a way to pass the ^ value to the second xargs command. Anyone?

  • 1
    that's a lot of piped/chained commands, does it have to be a one liner with pipes? Couldn't you use a bash variable or two? – Tom J Nowell Jul 9 at 14:48
2

xargs is unnecessary, something similar to this will do the job without any piping or xargs:

sites=$(wp site list --field=url)
for site in $sites
do
  users=$(wp user list --url="$site" --role=subscriber)
  for user in $users
  do
    wp user delete $user --url="$site" --reassign=4
  end
end
  • 1
    Its been a while since I wrote a bash script, but why not? This is the script that works for me (maybe you can't put in line breaks in a comment?): #!/bin/bash sites=$(wp site list --field=url) for site in $sites do users=$(wp user list --url=$site --role=subscriber --field=ID) for user in $users do wp user delete $user --url=$site --reassign=4 done done – photocurio Jul 9 at 18:55

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.