Clearing a table lets you rerun imports, integration tests, and staging jobs without rebuilding the table definition, indexes, or grants. When the rows need to disappear but the object itself must stay in place, an empty-table workflow is safer than dropping and recreating the table.
In MySQL and MariaDB, full-table clearing is usually done with either TRUNCATE TABLE or DELETE FROM. TRUNCATE is a fast DDL-style reset that empties the table and restarts AUTO_INCREMENT, while DELETE removes rows through the normal DML path and reports how many rows were removed.
The difference matters before you press Enter. Current MySQL and MariaDB releases still require the DROP privilege for TRUNCATE, still reject it with ERROR 1701 when another table references the target through a foreign key, and still skip ON DELETE triggers. DELETE needs only the DELETE privilege and can run inside an explicit transaction on InnoDB, but a large full-table delete generates more row-by-row work, undo, and binary-log traffic than a truncate.
$ mysqldump --user root --password --host db.example.net --port 3306 table_demo clear_demo > clear_demo-backup.sql Enter password:
Current MariaDB installs commonly use mariadb-dump instead of mysqldump for the same task.
$ mysql --user root --password --host db.example.net --port 3306 Enter password: Welcome to the MySQL monitor. Commands end with ; or \g. mysql>
Current MariaDB installs often provide mariadb as the client binary name. The SQL statements below are the same once you are connected.
mysql> USE table_demo; DATABASE changed mysql> SHOW TABLES LIKE 'clear_demo'; +-----------------------------------+ | Tables_in_table_demo (clear_demo) | +-----------------------------------+ | clear_demo | +-----------------------------------+ 1 ROW IN SET (0.00 sec) mysql> SELECT COUNT(*) AS rows_in_table FROM clear_demo; +---------------+ | rows_in_table | +---------------+ | 3 | +---------------+ 1 ROW IN SET (0.00 sec)
Use the fully qualified form table_demo.clear_demo when you do not want to change the current database first.
mysql> TRUNCATE TABLE clear_demo; Query OK, 0 ROWS affected (0.01 sec)
TRUNCATE requires the DROP privilege, performs an implicit commit, resets AUTO_INCREMENT to its start value, and commonly reports 0 rows affected because it does not return a meaningful deleted-row count.
Current MySQL and MariaDB releases return ERROR 1701 (42000): Cannot truncate a table referenced in a foreign key constraint when another table points at the target, and TRUNCATE does not fire DELETE triggers.
mysql> DELETE FROM clear_demo; Query OK, 3 ROWS affected (0.00 sec)
DELETE needs the DELETE privilege, fires DELETE triggers, and can be rolled back on InnoDB before COMMIT when it runs inside an explicit transaction.
mysql> ALTER TABLE clear_demo AUTO_INCREMENT = 1; Query OK, 0 ROWS affected (0.01 sec) Records: 0 Duplicates: 0 Warnings: 0
Run this only after confirming the table is empty. If rows remain, the next generated value still advances past the highest existing identifier.
mysql> SELECT COUNT(*) AS rows_in_table FROM clear_demo; +---------------+ | rows_in_table | +---------------+ | 0 | +---------------+ 1 ROW IN SET (0.00 sec) mysql> SHOW TABLE STATUS LIKE 'clear_demo'\G *************************** 1. ROW *************************** Name: clear_demo Engine: InnoDB ROWS: 0 AUTO_INCREMENT: 1 ##### snipped ##### 1 ROW IN SET (0.01 sec)
Auto_increment is the next value that will be assigned. After DELETE FROM without the optional reset step, InnoDB usually keeps the next value above the previous high-water mark.
mysql> EXIT;
Bye