Skip to content

Instantly share code, notes, and snippets.

@btastic
Created May 22, 2020 08:00
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save btastic/362ac1e5b0991f1b7bc19fca1aa1796e to your computer and use it in GitHub Desktop.
Save btastic/362ac1e5b0991f1b7bc19fca1aa1796e to your computer and use it in GitHub Desktop.
// reference commercetools.NET.SDK via nuget
using System;
using System.Threading.Tasks;
using commercetools.Categories;
using commercetools.Common;
using commercetools.ProductProjections;
using commercetools.ProductProjectionSearch;
using commercetools.Products;
using commercetools.Products.UpdateActions;
namespace ctdeleteallproducts
{
class Program
{
static async Task Main(string[] args)
{
IClient client = new Client(new Configuration()
{
OAuthUrl = "https://auth.sphere.io/oauth/token",
ApiUrl = "https://api.sphere.io",
ClientID = "client_id_here",
ClientSecret = "client_secret_here",
ProjectKey = "project_key_here"
});
var categoriesResponse = await client.Categories().QueryCategoriesAsync(limit: 500);
var categories = categoriesResponse.Result;
foreach (var category in categories.Results)
{
await client.Categories().DeleteCategoryAsync(category);
}
Response<ProductProjectionQueryResult> result = await client.ProductProjectionSearch()
.SearchProductProjectionsAsync();
var productsResponse = await client.Products().QueryProductsAsync(limit: 500);
if(!productsResponse.Success)
{
return;
}
var products = productsResponse.Result;
foreach (var product in products.Results)
{
var unpublishResult = await client.Products().UpdateProductAsync(product, new UnpublishAction());
var unpublishedProduct = await client.Products().GetProductByIdAsync(product.Id);
var ya = await client.Products().DeleteProductAsync(unpublishedProduct.Result);
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment