Wp user delete (WP-CLI): verschil tussen versies
Naar navigatie springen
Naar zoeken springen
Regel 61: | Regel 61: | ||
Success: Removed user 974 from example.com. | Success: Removed user 974 from example.com. | ||
Success: Removed user 975 from example.com. | Success: Removed user 975 from example.com. | ||
+ | </pre> | ||
+ | |||
+ | == This is better == | ||
+ | |||
+ | <pre> | ||
+ | # | ||
+ | # Delete all customers in the current WooCommerce site | ||
+ | ################################################################################ | ||
+ | # | ||
+ | # IT DOESN'T WORK YET: SOMETHING WRONG WITH OFFSET OR COUNT??? | ||
+ | # | ||
+ | ################################################################################ | ||
+ | # Prologue | ||
+ | ################################################################################ | ||
+ | # | ||
+ | echo "" | ||
+ | echo "##########################################################################" | ||
+ | echo "# wp_wc_delete_all_customers.sh" | ||
+ | echo "##########################################################################" | ||
+ | |||
+ | |||
+ | ################################################################################ | ||
+ | # delete_customer() | ||
+ | ################################################################################ | ||
+ | # | ||
+ | delete_customer() | ||
+ | { | ||
+ | # | ||
+ | # echo ">>> delete_customer" | ||
+ | echo " $(wp user delete "$1" --yes)" | ||
+ | # echo "<<< delete_customer" | ||
+ | # | ||
+ | } | ||
+ | |||
+ | |||
+ | ################################################################################ | ||
+ | # main | ||
+ | ################################################################################ | ||
+ | # | ||
+ | # | ||
+ | # Export delete_customer() | ||
+ | ######################################## | ||
+ | # | ||
+ | # Otherwise, it can't be used with parallel | ||
+ | # | ||
+ | export -f delete_customer | ||
+ | |||
+ | |||
+ | # Estimate total number of iterations | ||
+ | ######################################## | ||
+ | # | ||
+ | customer_count=$(wp user list --format=count) | ||
+ | iterations_needed=$((customer_count/100+1)) | ||
+ | # | ||
+ | echo "Total number of customers: $customer_count" | ||
+ | echo "Expected number of iterations: $iterations_needed" | ||
+ | |||
+ | |||
+ | # Assign first batch of order IDs | ||
+ | ######################################## | ||
+ | # | ||
+ | # The warning is PHP related. I don't think it's relevant for me | ||
+ | # | ||
+ | customer_ids=$(wp user list --role=customer --field=id --user=4) | ||
+ | offset=0 | ||
+ | |||
+ | |||
+ | # Loop | ||
+ | ######################################## | ||
+ | # | ||
+ | while [ -n "$customer_ids" ]; do | ||
+ | # | ||
+ | # | ||
+ | # Debug | ||
+ | ######################################## | ||
+ | # | ||
+ | echo "Iteration $((offset + 1)) of $iterations_needed" | ||
+ | |||
+ | |||
+ | # Delete all orders in this batch | ||
+ | ######################################## | ||
+ | # | ||
+ | echo "$customer_ids" | parallel delete_customer | ||
+ | |||
+ | |||
+ | # Get the next set of IDs | ||
+ | ######################################## | ||
+ | # | ||
+ | ((offset++)) | ||
+ | customer_ids=$(wp user list --role=customer --field=id --user=4 --offset=$((offset + 100))) | ||
+ | # | ||
+ | # | ||
+ | done | ||
</pre> | </pre> | ||
Huidige versie van 9 nov 2023 om 16:05
$wp help user delete NAME wp user delete DESCRIPTION Deletes one or more users from the current site. SYNOPSIS wp user delete <user>... [--network] [--reassign=<user-id>] [--yes] On multisite, `wp user delete` only removes the user from the current site. Include `--network` to also remove the user from the database, but make sure to reassign their posts prior to deleting the user. OPTIONS <user>... The user login, user email, or user ID of the user(s) to delete. [--network] On multisite, delete the user from the entire network. [--reassign=<user-id>] User ID to reassign the posts to. [--yes] Answer yes to any confirmation prompts. EXAMPLES # Delete user 123 and reassign posts to user 567 $ wp user delete 123 --reassign=567 Success: Removed user 123 from http://example.com # Delete all contributors and reassign their posts to user 2 $ wp user delete $(wp user list --role=contributor --field=ID) --reassign=2 Success: Removed user 813 from http://example.com Success: Removed user 578 from http://example.com
Remove all customer accounts
Example:
- Remove all customer accounts
- Remove all associated content
- Switch
--yes
: To confirm that all content should be deleted (there is no intermediary stage like a trashcan for this)
wp user delete $(wp user list --role=customer --field=ID) --yes Success: Removed user 979 from example.com. Success: Removed user 977 from example.com. Success: Removed user 980 from example.com. Success: Removed user 976 from example.com. Success: Removed user 974 from example.com. Success: Removed user 975 from example.com.
This is better
# # Delete all customers in the current WooCommerce site ################################################################################ # # IT DOESN'T WORK YET: SOMETHING WRONG WITH OFFSET OR COUNT??? # ################################################################################ # Prologue ################################################################################ # echo "" echo "##########################################################################" echo "# wp_wc_delete_all_customers.sh" echo "##########################################################################" ################################################################################ # delete_customer() ################################################################################ # delete_customer() { # # echo ">>> delete_customer" echo " $(wp user delete "$1" --yes)" # echo "<<< delete_customer" # } ################################################################################ # main ################################################################################ # # # Export delete_customer() ######################################## # # Otherwise, it can't be used with parallel # export -f delete_customer # Estimate total number of iterations ######################################## # customer_count=$(wp user list --format=count) iterations_needed=$((customer_count/100+1)) # echo "Total number of customers: $customer_count" echo "Expected number of iterations: $iterations_needed" # Assign first batch of order IDs ######################################## # # The warning is PHP related. I don't think it's relevant for me # customer_ids=$(wp user list --role=customer --field=id --user=4) offset=0 # Loop ######################################## # while [ -n "$customer_ids" ]; do # # # Debug ######################################## # echo "Iteration $((offset + 1)) of $iterations_needed" # Delete all orders in this batch ######################################## # echo "$customer_ids" | parallel delete_customer # Get the next set of IDs ######################################## # ((offset++)) customer_ids=$(wp user list --role=customer --field=id --user=4 --offset=$((offset + 100))) # # done