Skip to content

Instantly share code, notes, and snippets.

@billyjov
Forked from julienbourdeau/clean-prestashop-db.sql
Last active December 15, 2021 23:23
Show Gist options
  • Save billyjov/cae315c5a7257c6298bd1f1b151fc02e to your computer and use it in GitHub Desktop.
Save billyjov/cae315c5a7257c6298bd1f1b151fc02e to your computer and use it in GitHub Desktop.
Clean PrestaShop database - Drop old and unless data
# Delete all logs
TRUNCATE ps_log;
# DELETE logs with date
DELETE FROM ps_log WHERE date_add < '2021-09-14 18:00:00';
# Delete old connection data (only used for stats)
# change 2016-02-01 00:00:00 according to you needs
DELETE c, cs
FROM ps_connections c
LEFT JOIN ps_connections_source cs ON (c.id_connections = cs.id_connections)
WHERE c.date_add < '2016-02-01 00:00:00';
OPTIMIZE TABLE ps_connections, ps_connections_source;
# Delete all guest without entry in ps_customer table
DELETE g
FROM ps_guest g
LEFT JOIN ps_customer c ON (g.id_customer = c.id_customer)
WHERE c.id_customer IS NULL;
OPTIMIZE TABLE ps_guest;
# Delete tables
# Scenes are deprecated in 1.6 (used only if upgrading with feature active)
DROP TABLE `ps_scene`;
DROP TABLE `ps_scene_category`;
DROP TABLE `ps_scene_lang`;
TRUNCATE `ps_scene_products`;
DROP TABLE `ps_scene_shop`;
UPDATE `ps_configuration` SET value='0', date_upd=NOW() WHERE `name` = 'PS_SCENE_FEATURE_ACTIVE';
# This leaves a lot of ps_guests without a matching ps_cart, so what im doing after is running the following code
DELETE g FROM ps_guest as g
LEFT JOIN ps_cart as c ON g.id_guest = c.id_guest
WHERE id_cart IS NULL
# Delete Carts older than one month
DELETE FROM `'._DB_PREFIX_.'cart`
WHERE id_cart NOT IN (SELECT id_cart FROM `'._DB_PREFIX_.'orders`)
AND date_add < "'.pSQL(date('Y-m-d', strtotime('-1 month'))).'"';
DELETE FROM `ps_cart`
WHERE id_cart NOT IN (SELECT id_cart FROM `ps_orders`)
AND date_add < "'.pSQL(date('Y-m-d', strtotime('-1 month'))).'";
DELETE FROM `ps_cart`
WHERE id_cart NOT IN (SELECT id_cart FROM `ps_orders`)
AND date_add < "2021-08-20 18:00:00";
# Select order request for Cron job GLS
SELECT o.id_order FROM `ps_orders` o WHERE `current_state` = 4 AND o.`id_shop` = 1;
SELECT o.id_order FROM `ps_orders` o WHERE `current_state` = 4 AND o.`id_shop` = 1 AND o.date_upd > '2021-09-16 00:00:00';
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment