Create a gist now

Instantly share code, notes, and snippets.

Embed
What would you like to do?
A quick hack to destroy all sample data in an OpenCart catalog.
<?php
$options = getopt('p:');
$prefix = empty($options['p'])
? realpath('.')
: realpath($options['p']);
if (empty($prefix)) {
die("Bad prefix. Try again.\n");
}
require $prefix . '/admin/config.php';
require $prefix . '/system/database/' . DB_DRIVER . '.php';
require $prefix . '/system/library/db.php';
$db = new DB(DB_DRIVER, DB_HOSTNAME, DB_USERNAME, DB_PASSWORD, DB_DATABASE);
$tables = array(
'address',
'category',
'category_description',
'category_to_store',
'coupon',
'customer',
'download',
'download_description',
'manufacturer',
'manufacturer_to_store',
'product',
'product_description',
'product_discount',
'product_featured',
'product_image',
'product_option',
'product_option_description',
'product_option_value',
'product_option_value_description',
'product_related',
'product_special',
'product_to_download',
'product_to_store',
'review',
'store',
'store_description',
'product_tags',
'order',
'order_download',
'order_history',
'order_option',
'order_product',
'order_status',
'order_total',
'product_to_category',
'coupon_description',
'coupon_product',
);
foreach ($tables as $table) {
$sql = sprintf('TRUNCATE TABLE %s%s', DB_PREFIX, $table);
printf('%s %s ', $sql, str_repeat('.', 73 - strlen($sql)));
$db->query($sql);
echo "Done!\n";
}
@ekancepts

This comment has been minimized.

Show comment
Hide comment
@ekancepts

ekancepts Dec 10, 2012

//For opencart v1.5.4.1

query($sql); echo "Done!
"; } ?>

ekancepts commented Dec 10, 2012

//For opencart v1.5.4.1

query($sql); echo "Done!
"; } ?>
@dolansgists

This comment has been minimized.

Show comment
Hide comment
@dolansgists

dolansgists Jul 1, 2014

I know I'm over a year late but thanks for putting this up.

dolansgists commented Jul 1, 2014

I know I'm over a year late but thanks for putting this up.

@salmanindia

This comment has been minimized.

Show comment
Hide comment
@salmanindia

salmanindia Dec 2, 2014

its is same for opencart 1.5.6 and 2.0

salmanindia commented Dec 2, 2014

its is same for opencart 1.5.6 and 2.0

@salmanindia

This comment has been minimized.

Show comment
Hide comment
@salmanindia

salmanindia Dec 2, 2014

TRUNCATE TABLE `oc_product`;
TRUNCATE TABLE `oc_address`;
TRUNCATE TABLE `oc_category`;
TRUNCATE TABLE `oc_category_description`;
TRUNCATE TABLE `oc_category_to_store`;
TRUNCATE TABLE `oc_coupon`;
TRUNCATE TABLE `oc_customer`;
TRUNCATE TABLE `oc_download`;
TRUNCATE TABLE `oc_download_description`;
TRUNCATE TABLE `oc_manufacturer`;
TRUNCATE TABLE `oc_manufacturer_to_store`;
TRUNCATE TABLE `oc_product`;
TRUNCATE TABLE `oc_flipkart_product`;    
TRUNCATE TABLE `oc_product_description`;
TRUNCATE TABLE `oc_product_discount`;
TRUNCATE TABLE `oc_product_image`;
TRUNCATE TABLE `oc_product_option`;
TRUNCATE TABLE `oc_product_option_value`;    
TRUNCATE TABLE `oc_product_related`;
TRUNCATE TABLE `oc_product_special`;
TRUNCATE TABLE `oc_product_to_download`;
TRUNCATE TABLE `oc_product_to_store`;
TRUNCATE TABLE `oc_review`;
TRUNCATE TABLE `oc_store`;
TRUNCATE TABLE `oc_order`;
TRUNCATE TABLE `oc_order_download`;
TRUNCATE TABLE `oc_order_history`;
TRUNCATE TABLE `oc_order_option`;
TRUNCATE TABLE `oc_order_product`;
TRUNCATE TABLE `oc_order_status`;
TRUNCATE TABLE `oc_order_total`;
TRUNCATE TABLE `oc_product_to_category`;    
TRUNCATE TABLE `oc_coupon_product`

salmanindia commented Dec 2, 2014

TRUNCATE TABLE `oc_product`;
TRUNCATE TABLE `oc_address`;
TRUNCATE TABLE `oc_category`;
TRUNCATE TABLE `oc_category_description`;
TRUNCATE TABLE `oc_category_to_store`;
TRUNCATE TABLE `oc_coupon`;
TRUNCATE TABLE `oc_customer`;
TRUNCATE TABLE `oc_download`;
TRUNCATE TABLE `oc_download_description`;
TRUNCATE TABLE `oc_manufacturer`;
TRUNCATE TABLE `oc_manufacturer_to_store`;
TRUNCATE TABLE `oc_product`;
TRUNCATE TABLE `oc_flipkart_product`;    
TRUNCATE TABLE `oc_product_description`;
TRUNCATE TABLE `oc_product_discount`;
TRUNCATE TABLE `oc_product_image`;
TRUNCATE TABLE `oc_product_option`;
TRUNCATE TABLE `oc_product_option_value`;    
TRUNCATE TABLE `oc_product_related`;
TRUNCATE TABLE `oc_product_special`;
TRUNCATE TABLE `oc_product_to_download`;
TRUNCATE TABLE `oc_product_to_store`;
TRUNCATE TABLE `oc_review`;
TRUNCATE TABLE `oc_store`;
TRUNCATE TABLE `oc_order`;
TRUNCATE TABLE `oc_order_download`;
TRUNCATE TABLE `oc_order_history`;
TRUNCATE TABLE `oc_order_option`;
TRUNCATE TABLE `oc_order_product`;
TRUNCATE TABLE `oc_order_status`;
TRUNCATE TABLE `oc_order_total`;
TRUNCATE TABLE `oc_product_to_category`;    
TRUNCATE TABLE `oc_coupon_product`
@danieleb0101

This comment has been minimized.

Show comment
Hide comment
@danieleb0101

danieleb0101 Mar 10, 2015

Hi, I am a newbie with opencart and I do not know php,
where should I copy this file and how can I run it?

danieleb0101 commented Mar 10, 2015

Hi, I am a newbie with opencart and I do not know php,
where should I copy this file and how can I run it?

@costa974

This comment has been minimized.

Show comment
Hide comment
@costa974

costa974 Aug 3, 2015

Just run on opencart 2.0.3.1
It's incredibile that there's no option to delete or not to install the sample data!
Maybe I should move back to magento.
Run this script in your MySQL Workbench or phpmyadmin

TRUNCATE TABLE oc_product;
TRUNCATE TABLE oc_address;
TRUNCATE TABLE oc_category;
TRUNCATE TABLE oc_category_description;
TRUNCATE TABLE oc_category_to_store;
TRUNCATE TABLE oc_coupon;
TRUNCATE TABLE oc_customer;
TRUNCATE TABLE oc_download;
TRUNCATE TABLE oc_download_description;
TRUNCATE TABLE oc_manufacturer;
TRUNCATE TABLE oc_manufacturer_to_store;
TRUNCATE TABLE oc_product;
TRUNCATE TABLE oc_product_description;
TRUNCATE TABLE oc_product_discount;
TRUNCATE TABLE oc_product_image;
TRUNCATE TABLE oc_product_option;
TRUNCATE TABLE oc_product_option_value;
TRUNCATE TABLE oc_product_related;
TRUNCATE TABLE oc_product_special;
TRUNCATE TABLE oc_product_to_download;
TRUNCATE TABLE oc_product_to_store;
TRUNCATE TABLE oc_review;
TRUNCATE TABLE oc_store;
TRUNCATE TABLE oc_order;
TRUNCATE TABLE oc_order_history;
TRUNCATE TABLE oc_order_option;
TRUNCATE TABLE oc_order_product;
TRUNCATE TABLE oc_order_total;
TRUNCATE TABLE oc_product_to_category;
TRUNCATE TABLE oc_coupon_product;

TRUNCATE TABLE oc_attribute;
TRUNCATE TABLE oc_attribute_description;
TRUNCATE TABLE oc_attribute_group;
TRUNCATE TABLE oc_attribute_group_description;

TRUNCATE TABLE oc_banner;
TRUNCATE TABLE oc_banner_image;
TRUNCATE TABLE oc_banner_image_description;

TRUNCATE TABLE oc_category_path;

TRUNCATE TABLE oc_product_attribute;

TRUNCATE TABLE oc_url_alias;

costa974 commented Aug 3, 2015

Just run on opencart 2.0.3.1
It's incredibile that there's no option to delete or not to install the sample data!
Maybe I should move back to magento.
Run this script in your MySQL Workbench or phpmyadmin

TRUNCATE TABLE oc_product;
TRUNCATE TABLE oc_address;
TRUNCATE TABLE oc_category;
TRUNCATE TABLE oc_category_description;
TRUNCATE TABLE oc_category_to_store;
TRUNCATE TABLE oc_coupon;
TRUNCATE TABLE oc_customer;
TRUNCATE TABLE oc_download;
TRUNCATE TABLE oc_download_description;
TRUNCATE TABLE oc_manufacturer;
TRUNCATE TABLE oc_manufacturer_to_store;
TRUNCATE TABLE oc_product;
TRUNCATE TABLE oc_product_description;
TRUNCATE TABLE oc_product_discount;
TRUNCATE TABLE oc_product_image;
TRUNCATE TABLE oc_product_option;
TRUNCATE TABLE oc_product_option_value;
TRUNCATE TABLE oc_product_related;
TRUNCATE TABLE oc_product_special;
TRUNCATE TABLE oc_product_to_download;
TRUNCATE TABLE oc_product_to_store;
TRUNCATE TABLE oc_review;
TRUNCATE TABLE oc_store;
TRUNCATE TABLE oc_order;
TRUNCATE TABLE oc_order_history;
TRUNCATE TABLE oc_order_option;
TRUNCATE TABLE oc_order_product;
TRUNCATE TABLE oc_order_total;
TRUNCATE TABLE oc_product_to_category;
TRUNCATE TABLE oc_coupon_product;

TRUNCATE TABLE oc_attribute;
TRUNCATE TABLE oc_attribute_description;
TRUNCATE TABLE oc_attribute_group;
TRUNCATE TABLE oc_attribute_group_description;

TRUNCATE TABLE oc_banner;
TRUNCATE TABLE oc_banner_image;
TRUNCATE TABLE oc_banner_image_description;

TRUNCATE TABLE oc_category_path;

TRUNCATE TABLE oc_product_attribute;

TRUNCATE TABLE oc_url_alias;

@NginUS

This comment has been minimized.

Show comment
Hide comment
@NginUS

NginUS Sep 11, 2015

I get a 500 error when I try to open it.

NginUS commented Sep 11, 2015

I get a 500 error when I try to open it.

@SilverPaladin

This comment has been minimized.

Show comment
Hide comment
@SilverPaladin

SilverPaladin Jul 7, 2017

My version Opencart V 3.0.1.1:
TRUNCATE TABLE oc_product;
TRUNCATE TABLE oc_address;
TRUNCATE TABLE oc_category;
TRUNCATE TABLE oc_category_description;
TRUNCATE TABLE oc_category_to_store;
TRUNCATE TABLE oc_coupon;
TRUNCATE TABLE oc_customer;
TRUNCATE TABLE oc_download;
TRUNCATE TABLE oc_download_description;
TRUNCATE TABLE oc_manufacturer;
TRUNCATE TABLE oc_manufacturer_to_store;
TRUNCATE TABLE oc_product;
TRUNCATE TABLE oc_product_description;
TRUNCATE TABLE oc_product_discount;
TRUNCATE TABLE oc_product_image;
TRUNCATE TABLE oc_product_option;
TRUNCATE TABLE oc_product_option_value;
TRUNCATE TABLE oc_product_related;
TRUNCATE TABLE oc_product_special;
TRUNCATE TABLE oc_product_to_download;
TRUNCATE TABLE oc_product_to_store;
TRUNCATE TABLE oc_review;
TRUNCATE TABLE oc_store;
TRUNCATE TABLE oc_order;
TRUNCATE TABLE oc_order_history;
TRUNCATE TABLE oc_order_option;
TRUNCATE TABLE oc_order_product;
TRUNCATE TABLE oc_order_status;
TRUNCATE TABLE oc_order_total;
TRUNCATE TABLE oc_product_to_category;
TRUNCATE TABLE oc_coupon_product;
truncate table oc_seo_url;
truncate table oc_product_reward;
truncate table oc_product_attribute;
truncate table oc_category_path;
truncate table oc_banner_image;
truncate table oc_banner;
truncate table oc_attribute_group_description;
truncate table oc_attribute_group;
truncate table oc_attribute_description;
truncate table oc_attribute;

SilverPaladin commented Jul 7, 2017

My version Opencart V 3.0.1.1:
TRUNCATE TABLE oc_product;
TRUNCATE TABLE oc_address;
TRUNCATE TABLE oc_category;
TRUNCATE TABLE oc_category_description;
TRUNCATE TABLE oc_category_to_store;
TRUNCATE TABLE oc_coupon;
TRUNCATE TABLE oc_customer;
TRUNCATE TABLE oc_download;
TRUNCATE TABLE oc_download_description;
TRUNCATE TABLE oc_manufacturer;
TRUNCATE TABLE oc_manufacturer_to_store;
TRUNCATE TABLE oc_product;
TRUNCATE TABLE oc_product_description;
TRUNCATE TABLE oc_product_discount;
TRUNCATE TABLE oc_product_image;
TRUNCATE TABLE oc_product_option;
TRUNCATE TABLE oc_product_option_value;
TRUNCATE TABLE oc_product_related;
TRUNCATE TABLE oc_product_special;
TRUNCATE TABLE oc_product_to_download;
TRUNCATE TABLE oc_product_to_store;
TRUNCATE TABLE oc_review;
TRUNCATE TABLE oc_store;
TRUNCATE TABLE oc_order;
TRUNCATE TABLE oc_order_history;
TRUNCATE TABLE oc_order_option;
TRUNCATE TABLE oc_order_product;
TRUNCATE TABLE oc_order_status;
TRUNCATE TABLE oc_order_total;
TRUNCATE TABLE oc_product_to_category;
TRUNCATE TABLE oc_coupon_product;
truncate table oc_seo_url;
truncate table oc_product_reward;
truncate table oc_product_attribute;
truncate table oc_category_path;
truncate table oc_banner_image;
truncate table oc_banner;
truncate table oc_attribute_group_description;
truncate table oc_attribute_group;
truncate table oc_attribute_description;
truncate table oc_attribute;

@GraniteConsultingReviews

This comment has been minimized.

Show comment
Hide comment
@GraniteConsultingReviews

GraniteConsultingReviews Aug 29, 2017

Thanks for sharing this code works well

Thanks for sharing this code works well

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment