3

I want to remove all users from the WordPress site using WP CLI except one (me, as administrator).

This command will remove all users:

wp user delete $(wp user list --field=ID --number=10)

Unfortunately (or not) no one has a role except the administrator. So I cannot filter based on this information.

1 Answer 1

6

Found out you can pass the --exclude option to wp user list to exclude a user by their ID from the list. So let's assume your user ID is 2 then you could do the following:

wp user delete $(wp user list --field=ID --exclude=2)

And just to be sure all content gets reassigned to you that would be the following command.

wp user delete $(wp user list --field=ID --exclude=2) --reassign=2
5
  • 1
    If I need to exclude more then one user I can use something like --exclude=2,3?
    – natas
    Dec 5, 2019 at 12:16
  • @natas – Yes, exactly. You can simply try it out yourself with wp user list against wp user list --exclude=2,3,4,5.
    – leymannx
    Dec 5, 2019 at 13:15
  • what to add in --field=id May 2, 2021 at 19:21
  • WARNING: Don't use this if you intend to delete Users on a staging server. Perhaps I can save some troubles for others. I did not know it because I have a different coding background, but I cd to my staging folder and used wp user delete but it started to delete from my production server. It would have been correct to use wp_staging. Dec 4, 2023 at 18:10
  • LOOOOOOOOOOOOOL
    – leymannx
    Dec 4, 2023 at 18:18

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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