Delete all Product Variations Using REST API
Recently I have found this question on the internet and since I was doing exactly the same when developing WooCommerce functionality for my crossposting plugin, I decided to create a small guide for you.
The long story short we will need two REST API requests here:
- To get all variations of a specific product,
- To delete all of them in a single batch request.
I think the most clear way here would be to create a function for that, let’s say for example rudr_delete_all_variations()
or something.
Let’s do it:
function rudr_delete_all_variations( $product_id, $store ) {
// first of all let's get variations of a product
$response = wp_remote_get(
add_query_arg(
array(
'per_page' => 50,
'page' => 1,
'status' => 'any', // publish or private
),
"{$store[ 'url' ]}/wp-json/wc/v3/products/{$product_id}/variations"
),
array(
'headers' => array(
'Authorization' => 'Basic ' . base64_encode( "{$store[ 'login' ]}:{$store[ 'pwd' ]}" )
)
)
);
$variations = json_decode( wp_remote_retrieve_body( $response ), true );
if( $variations ) {
return false;
}
$variations_ids = wp_list_pluck( $variations, 'id' );
// now remove the variations in a single batch
wp_remote_post(
"{$store[ 'url' ]}/wp-json/wc/v3/products/{$product_id}/variations/batch",
array(
'headers' => array(
'Authorization' => 'Basic ' . base64_encode( "{$store[ 'login' ]}:{$store[ 'pwd' ]}" )
),
'body' => array( 'delete' => $variations_ids )
)
);
}
What we’re doing inside the function:
$store
is the array with store information, which contains URL, username of an administrator and an application password of the same user.- I am using
wp_remote_get()
HTTP API function to send a GET request andwp_remote_post()
to send a POST request, that should be clear I guess or of course in both cases I could usewp_remote_request()
. wp_list_pluck()
by the way is an amazing function which allows us to convert an array of arrays with variation data likeArray( 'id' => 1, 'date_created' => ...
into just an array with variation IDsArray( 1, 5, 15 ...
.- Is it possible to run this function for all the available variations across the store? I guess so, but I assume creating custom REST API route would be a better and more performant decision than creating an additional REST API request which returns all the products.

Misha Rudrastyh
Hey guys and welcome to my website. For more than 10 years I've been doing my best to share with you some superb WordPress guides and tips for free.
Need some developer help? Contact me