Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Simple PHP script to delete all products from Shopify store using a private App(Api key and Pass). Just replace your App info and enjoy. Thanks
<?php
/**
*
*/
class ShopifyProducts{
public function getProducts($count = false, $page){
$apikey = 'API_KEY'; //Replace with your's
$pass = 'PASS'; //Replace with your's
$store = 'STORE_URL'; //Replace with your's
if ($count == true) {
$products_count = file_get_contents('https://'.$apikey.':'.$pass.'@'.$store.'/admin/products/count.json');
$data = json_decode($products_count);
return $data->count;
}
$products = file_get_contents('https://'.$apikey.':'.$pass.'@'.$store.'/admin/products.json?limit=250&page='.$page);
$data = json_decode($products);
return $data;
}
}
$obj = new ShopifyProducts();
$count = $obj->getProducts(true, null);
$pages = ceil((int)$count/250);
echo "<br>Total no of pages: ".$pages;
for ($i=1; $i <= $pages; $i++) {
echo "<br>Deleting products of Page No.: ". $i;
$products = $obj->getProducts(false, $i);
foreach ($products->products as $index => $product) {
echo "<br>";
print_r($product->id);
$url = "https://API_KEY:PASS@STORE_URL/admin/products/".$product->id.".json"; //Replace with your's
//Initiate cURL
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "DELETE");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$response = curl_exec($ch);
echo "<br><h1>Response</h1>";
var_dump($response);
curl_close($ch);
}
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment