To delete everything in a table:
TRUNCATE TABLE table_you_want_to_nuke
To delete certain rows, you have two options:
Follow these steps:
- Create a temporary table using
CREATE TABLE the_temp_table LIKE current_table
- Drop all of the indexes on the temp table.
- Copy the records you want to keep with
INSERT INTO the_temp_table SELECT * FROM current_table WHERE ...
TRUNCATE TABLE current_table
INSERT INTO current_table SELECT * FROM the_temp_table
- To speed this option up, you may want to drop all indexes from
current_table
before the finalINSERT INTO
, then recreate them after theINSERT
. MySQL is much faster at indexing existing data than it is at indexing on the fly.
- Create a temporary table using
The option you're currently trying:
DELETE FROM your_table WHERE whatever_condition
. You probably need to break this into chunks using theWHERE
condition orLIMIT
, so you can batch it and not bog down the server forever.
Which is better/faster depends on lots of things, mostly the ratio of deleted records to retained records and the number of indexes involved. As always, test this carefully before doing it on a live database, as both DELETE
and TRUNCATE
will permanently destroy data.