PrestaShop database cleanup means removing data the store no longer needs: visitor statistics, guests, logs, the e-mail log, abandoned carts, expired discounts and old sessions. These tables can reach millions of rows while orders and products stay small. The short answer: find the largest tables, make a backup, delete old rows from statistics and logs (child tables first), never touch carts that have an order, then run OPTIMIZE TABLE to give the space back to the disk.
- Which PrestaShop tables grow and why
- Symptoms of a bloated database
- How to check table sizes
- What PrestaShop offers, and its limits
- PrestaShop database cleanup with SQL
- OPTIMIZE and ANALYZE: why the file does not shrink
- Missing indexes and orphan rows
- Automating the cleanup with cron
- A module for database cleanup
- Sources
- Frequently asked questions
Which PrestaShop tables grow and why
Most of the weight is in tables that record activity, not sales. We use the default ps_ prefix; yours may differ (since PrestaShop 9 the installer randomizes it, see database_prefix in app/config/parameters.php).
| Table | What fills it | Safe to clean? |
|---|---|---|
ps_connections | one row per visit, written by the "Data mining for statistics" module | yes, beyond the period your statistics need |
ps_connections_source | referrer and address of visits | yes, with their visits |
ps_connections_page | time per page, only with "Save page views for each customer" | yes |
ps_guest | one row per new visitor | yes, if no customer, cart or visit uses it |
ps_page_viewed | page view counters, only with "Save global page views" | yes, old date ranges |
ps_pagenotfound | 404 errors logged by the "Pages not found" module | yes |
ps_statssearch | search phrases logged by the search statistics module | yes |
ps_log | errors, warnings, Back Office changes | yes, once you have read the errors |
ps_mail | every e-mail sent, when "Log Emails" is on | yes, keep recent months |
ps_cart, ps_cart_product | every cart, ordered or not | only carts without an order |
ps_cart_rule | discount codes, also single-use ones created by modules | expired ones, with care |
ps_specific_price | specific prices with an end date | expired ones |
ps_customer_session, ps_employee_session | one row per login, removed only on logout | old ones; a deleted live session logs its user out |
ps_smarty_cache, ps_smarty_lazy_cache | Smarty cache in MySQL (1.7 only) and the index of cache files | yes, with "Clear cache" |
The statistics module records visits whether or not you read the reports, so steady traffic adds rows to ps_connections, ps_connections_source and ps_guest every day. Both page view options are off on a fresh installation.
Symptoms of a bloated database
- A slow Back Office: the customer list computes "Last visit" from
ps_guestandps_connectionsfor every row, and the dashboard reads visits too. - Statistics pages that time out on longer periods.
- Slow, large backups and dumps that fail.
- Disk usage that fills the hosting quota.
- Slow upgrades and migrations: every backup and table change takes longer.
How to check table sizes
Run this query in phpMyAdmin (select the database, then the "SQL" tab) or in a MySQL console:
SELECT TABLE_NAME AS table_name, ENGINE AS engine,
TABLE_ROWS AS rows_estimate,
ROUND((DATA_LENGTH + INDEX_LENGTH) / 1024 / 1024, 1) AS size_mb,
ROUND(DATA_FREE / 1024 / 1024, 1) AS free_mb
FROM information_schema.TABLES
WHERE TABLE_SCHEMA = DATABASE()
ORDER BY DATA_LENGTH + INDEX_LENGTH DESC
LIMIT 20;
For InnoDB, TABLE_ROWS is an estimate (MySQL says it can be off by 40–50%); use SELECT COUNT(*) for an exact number. MySQL 8 caches these statistics for 24 hours, so run SET SESSION information_schema_stats_expiry = 0; first (skip it on MariaDB and MySQL 5.7). In phpMyAdmin, sorting the table list by "Size" shows the same. A table that is not InnoDB is worth converting.
What PrestaShop offers, and its limits
- Logs: Advanced Parameters > Logs, "Erase all" in the list menu. All or nothing.
- E-mail log: Advanced Parameters > E-mail, "Erase all" in the e-mail list, or switch off "Log Emails".
- Pages not found: Stats, "Pages not found" report, "Empty database" for a period or everything.
- Statistics: the "Data mining for statistics" module settings (Modules > Module Manager) only decide what is recorded. The code has an auto-clean setting, but its form is commented out in 1.7.8, 8 and 9, and it only pruned
ps_connections_page. Nothing cleansps_connectionsorps_guest. - Abandoned carts: Orders > Shopping Carts, filter and bulk delete, page by page; carts with an order cannot be deleted.
- Smarty cache: Advanced Parameters > Performance, "Clear cache". The MySQL caching type exists in 1.7 only.
- Backup: Advanced Parameters > Database > DB Backup; on a large database it often hits the PHP time limit.
Expired discounts, specific prices and sessions have no cleanup tool.
PrestaShop database cleanup with SQL
Backup first. Make a full dump and check that it restores, ideally on a copy of the store. A
DELETEcannot be undone.
Delete child rows before their parents, otherwise you leave rows nothing can reach. Statistics, guests and logs older than a fixed date:
DELETE cs FROM ps_connections_source cs
JOIN ps_connections c ON c.id_connections = cs.id_connections
WHERE c.date_add < '2025-11-01';
DELETE cp FROM ps_connections_page cp
JOIN ps_connections c ON c.id_connections = cp.id_connections
WHERE c.date_add < '2025-11-01';
DELETE FROM ps_connections WHERE date_add < '2025-11-01';
DELETE FROM ps_guest WHERE id_customer = 0
AND NOT EXISTS (SELECT 1 FROM ps_cart ca WHERE ca.id_guest = ps_guest.id_guest)
AND NOT EXISTS (SELECT 1 FROM ps_connections co WHERE co.id_guest = ps_guest.id_guest);
DELETE FROM ps_log WHERE date_add < '2025-11-01';
DELETE FROM ps_mail WHERE date_add < '2025-11-01';
On millions of rows, delete a month at a time, outside peak hours. Never delete carts with an order, orders, customers, addresses, products or images. Leave abandoned carts to PrestaShop too: Cart::delete() also removes customizations, uploaded files and cart discounts, and payment or reminder modules keep data by cart. To count the carts concerned:
SELECT COUNT(*) FROM ps_cart c
WHERE c.date_upd < '2025-05-01'
AND NOT EXISTS (SELECT 1 FROM ps_orders o WHERE o.id_cart = c.id_cart);
OPTIMIZE and ANALYZE: why the file does not shrink
After a large DELETE, the database is not smaller on disk: InnoDB reuses the freed space for new rows, but the file keeps its size. To give the space back, rebuild the tables:
OPTIMIZE TABLE ps_connections, ps_connections_source, ps_connections_page, ps_guest;
ANALYZE TABLE ps_connections, ps_guest;
For InnoDB, MySQL turns OPTIMIZE TABLE into a rebuild, so "doing recreate + analyze instead" is a normal message. Space returns to the system only when the table has its own .ibd file (innodb_file_per_table, the default); a shared tablespace never shrinks. The rebuild is online but needs free disk space about the size of the table. ANALYZE TABLE is light: it refreshes the index statistics the optimizer uses.
Missing indexes and orphan rows
A fresh installation creates its indexes from install/data/db_structure.sql. After years of upgrades, a move to another host or a restore from a partial dump, some can be missing, and MySQL then reads whole tables: milliseconds become seconds. Compare SHOW INDEX FROM ps_orders; with the KEY lines of your exact version. Many gaps can be a sign that an upgrade did not finish cleanly; see also our PrestaShop upgrade service.
Orphan rows are child rows whose parent is gone: cart lines without a cart, translations of deleted records, stock of deleted products. Modules and scripts that delete with SQL leave them behind:
SELECT COUNT(*) FROM ps_cart_product cp
LEFT JOIN ps_cart c ON c.id_cart = cp.id_cart
WHERE c.id_cart IS NULL;
Automating the cleanup with cron
After a one-off cleanup the tables grow again. Save the deletes with a relative date (NOW() - INTERVAL 12 MONTH) and run them weekly:
0 4 * * 0 mysql store_db < /home/store/db-cleanup.sql
Keep the password in ~/.my.cnf and let the nightly backup run first. If you would rather not manage the server, our PrestaShop server and administration includes PrestaShop crons, database dumps and the MySQL slow query log.
A module for database cleanup
If you prefer not to write SQL, we built Database Cleaner & Optimizer. Each data group (statistics, page views, guests, logs, e-mails, 404s, searches, abandoned carts, expired codes and prices, sessions, Smarty cache) has a card with its rows, size and a colour: green OK, yellow large, red very large. Clean everything, keep the last N days or the newest N rows, with a preview of the exact number of rows and a backup confirmation checkbox before every delete.
Where PrestaShop has its own function (carts, discount codes, specific prices, logs, Smarty cache), the module uses it. It never deletes carts with an order, orders, customers, addresses or images. It runs OPTIMIZE and ANALYZE, finds and deletes orphan rows on request, checks indexes against a fresh installation of your PrestaShop line with an "Add index" button, and lists the largest tables and those not on InnoDB. A cron URL with a token cleans by your rules; in multistore only an employee managing all shops can act. It works with PrestaShop 1.7.8, 8 and 9 and does not make backups itself. For a large or unusual database you can also book PrestaShop development work.
A clean database is one of the steps in our guide on how to speed up PrestaShop.
Sources
- MySQL 8.0: OPTIMIZE TABLE — InnoDB rebuild, online DDL, disk space.
- MySQL 8.4: ANALYZE TABLE — index statistics.
- MySQL 8.0: File-Per-Table Tablespaces — when space returns to the system.
- MySQL 8.0: The INFORMATION_SCHEMA TABLES Table — row estimates, cached statistics.
- MariaDB: OPTIMIZE TABLE.
- PrestaShop devdocs: Database and Database structure — prefix,
db_structure.sql.
Frequently asked questions
Is it safe to empty ps_connections?
Yes, if you accept losing the visit history in the Stats reports for that period. Orders, customers and products do not depend on it. Delete the matching ps_connections_source and ps_connections_page rows first, after a backup.
Why is my database still the same size after deleting?
InnoDB keeps freed space inside the table file and reuses it. Run OPTIMIZE TABLE on the cleaned tables; the file shrinks when innodb_file_per_table is on, which is the default.
Can I delete abandoned carts?
Carts without an order, yes, preferably older than a few months and through PrestaShop's own functions. Carts with an order must stay. If a reminder module e-mails abandoned carts, keep the recent ones.
Should I turn off PrestaShop statistics?
If you use another analytics tool and never open the Stats reports, disabling "Data mining for statistics" stops new visit rows. Some reports and dashboard blocks then show no visitor data.
Want to clean your database without writing SQL? See the Database Cleaner & Optimizer module or contact us if you have a question about your store.