116

How to delete my tables who all have the prefix myprefix_?

Note: need to execute it in phpMyAdmin

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
Deniz Zoeteman
  • 9,691
  • 26
  • 70
  • 97

10 Answers10

202

You cannot do it with just a single MySQL command, however you can use MySQL to construct the statement for you:

In the MySQL shell or through PHPMyAdmin, use the following query

SELECT CONCAT( 'DROP TABLE ', GROUP_CONCAT(table_name) , ';' ) 
    AS statement FROM information_schema.tables 
    WHERE table_name LIKE 'myprefix_%';

This will generate a DROP statement which you can than copy and execute to drop the tables.

EDIT: A disclaimer here - the statement generated above will drop all tables in all databases with that prefix. If you want to limit it to a specific database, modify the query to look like this and replace database_name with your own database_name:

SELECT CONCAT( 'DROP TABLE ', GROUP_CONCAT(table_name) , ';' ) 
    AS statement FROM information_schema.tables 
    WHERE table_schema = 'database_name' AND table_name LIKE 'myprefix_%';
Andre Miller
  • 15,255
  • 6
  • 55
  • 53
  • I have only one database so i wont need the 2nd one. – Deniz Zoeteman Oct 19 '09 at 15:38
  • Thanks! So problematic that my MySQL server is so slow. Needed 10-15 minutes to execute the script. Then it took way too long to display the full script, that i stopped it. Will try again when have more time. – Deniz Zoeteman Oct 20 '09 at 19:53
  • how about ignoring the foreign key check ? – Raptor Dec 02 '13 at 04:43
  • This works but limits it to only about 20 or so tables. I have over a thousand to drop (auto generated test tables). Why does it not give me everything? – Elijah Lynn May 09 '14 at 21:17
  • 4
    @ElijahLynn the limit source is probably due to the GROUP_CONCAT max length. you can enlarge it, see for example [here](http://stackoverflow.com/questions/2567000/mysql-and-group-concat-maximum-length) – Asaf David Jul 11 '14 at 09:18
  • 8
    Note to self.Increase num of max char: **SET SESSION group_concat_max_len = 999999999;** – Mohammed Joraid Apr 12 '15 at 00:59
  • 2
    Please note that, in some cases, it is necessary to escape underscores in table prefix names `[...] LIKE 'myprefix\_%';` – Magictallguy Apr 08 '17 at 19:33
  • Summing this up, with quoting and duplicate management: 'SET SESSION group_concat_max_len = 999999999; SELECT CONCAT( 'DROP TABLE IF EXISTS ', GROUP_CONCAT('`', TABLE_SCHEMA, '`.`', table_name, '`') , ';' ) AS statement FROM information_schema.tables WHERE table_name LIKE 'my_prefix%'; ' – Łukasz Muchlado Mar 03 '18 at 23:49
  • 1
    This returns numerous duplicate table_names. The answer that Pankaj Khurana posted works better IMHO. – Jeremy May 31 '18 at 20:46
  • I needed to use `TABLES` for mysql to drop more than the first table listed. – am71722 Aug 21 '18 at 13:31
44

Some of the earlier answers were very good. I have pulled together their ideas with some notions from other answers on the web.

I needed to delete all tables starting with 'temp_' After a few iterations I came up with this block of code:

-- Set up variable to delete ALL tables starting with 'temp_'
SET GROUP_CONCAT_MAX_LEN=10000;
SET @tbls = (SELECT GROUP_CONCAT(TABLE_NAME)
               FROM information_schema.TABLES
              WHERE TABLE_SCHEMA = 'my_database'
                AND TABLE_NAME LIKE 'temp_%');
SET @delStmt = CONCAT('DROP TABLE ',  @tbls);
-- SELECT @delStmt;
PREPARE stmt FROM @delStmt;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;

I hope this is useful to other MySQL/PHP programmers.

Valentin Despa
  • 40,712
  • 18
  • 80
  • 106
Bradley Slavik
  • 875
  • 7
  • 13
  • 2
    This exactly the right answer. Other answers use multiple steps or require it to be done manually. Don't know how this answer doesn't have more votes after 4 years! – gbe Sep 29 '18 at 04:59
  • This is the right answer for people utilizing mysql command line. No copy paste needed just straight to the point. – Grogu Oct 20 '20 at 01:17
  • good stuff but doesn't work for table names with hyphens in them – HyperActive Oct 28 '21 at 09:22
  • @gbe on many Web Hosting platforms you don't have access to MySQL command line hence the relative unpopularity of this suggestion, also many people will feel happier seeing what is going to be deleted before it actually is!! – deep64blue Jun 12 '22 at 08:37
26
show tables like 'prefix_%';

copy the results and paste them into a text editor or output the query to a file, use a few search and replaces to remove unwanted formatting and replace \n with a comma put a ; on the end and add drop table to the front.

you'll get something that looks like this:

drop table myprefix_1, myprefix_2, myprefix_3;
j0k
  • 22,600
  • 28
  • 79
  • 90
Daniel
  • 261
  • 3
  • 2
10

@andre-miller solution is good but there is even better and slightly more professional that will help you execute all in one go. Still will need more than one command but this solution will allow you to use the SQL for automated builds.

SET @tbls = (SELECT GROUP_CONCAT(TABLE_NAME) 
    FROM information_schema.TABLES
    WHERE TABLE_NAME LIKE 'myprefix_%');
PREPARE stmt FROM 'DROP TABLE @tbls';
EXECUTE stmt USING @tbls;
DEALLOCATE PREPARE stmt;

Note: this code is platform dependant, it's for MySQL but for sure it could be implemented for Postgre, Oracle and MS SQL with slight changes.

Community
  • 1
  • 1
Alex Rashkov
  • 9,833
  • 3
  • 32
  • 58
  • 1
    ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '@tbls' at line 1 ERROR 1243 (HY000): Unknown prepared statement handler (stmt) given to EXECUTE ERROR 1243 (HY000): Unknown prepared statement handler (stmt) given to DEALLOCATE PREPARE – Roni Tovi Nov 14 '14 at 14:22
  • 2
    Syntax error at : PREPARE stmt FROM 'DROP TABLE @tbls'; – ledawg Mar 20 '16 at 12:20
6
SELECT CONCAT("DROP TABLE ", table_name, ";") 
FROM information_schema.tables
WHERE table_schema = "DATABASE_NAME" 
AND table_name LIKE "PREFIX_TABLE_NAME%";
mirza
  • 5,685
  • 10
  • 43
  • 73
Pankaj Khurana
  • 494
  • 6
  • 6
4

I drop table successfully by edit query to like this

SET GROUP_CONCAT_MAX_LEN=10000;
SET FOREIGN_KEY_CHECKS = 0;
SET @tbls = (SELECT GROUP_CONCAT(CONCAT('`', TABLE_NAME, '`'))
           FROM information_schema.TABLES
          WHERE TABLE_SCHEMA = 'pandora'
            AND TABLE_NAME LIKE 'temp_%');
SET @delStmt = CONCAT('DROP TABLE ',  @tbls);
-- SELECT @delStmt;
PREPARE stmt FROM @delStmt;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;
SET FOREIGN_KEY_CHECKS = 1;
Duong Vo
  • 41
  • 1
  • 1
    This was the only one I found that did the whole operation in one shebang while addressing foreign key constraints. – Evan Mattson Nov 16 '15 at 23:49
3

Just another solution using GROUP_CONCAT so it will execute one drop query like
DROP TABLE table1,table2,..

SET @Drop_Stm = CONCAT('DROP TABLE ', (
      SELECT GROUP_CONCAT(TABLE_NAME) AS All_Tables FROM information_schema.tables 
      WHERE TABLE_NAME LIKE 'prefix_%' AND TABLE_SCHEMA = 'database_name'
)); 
PREPARE Stm FROM @Drop_Stm; 
EXECUTE Stm;
DEALLOCATE PREPARE Stm;
Mohamad Hamouday
  • 2,070
  • 23
  • 20
2

You can do that in one command with MySQL:

drop table myprefix_1, myprefix_2, myprefix_3;

You'll probably have to build the table list dynamically in code though.

An alternative approach would be to use the general purpose routine library for MySQL 5.

Asaph
  • 159,146
  • 25
  • 197
  • 199
2

I just wanted to post the exact SQL I used - it's something of a mixture of the top 3 answers:

SET GROUP_CONCAT_MAX_LEN=10000;

SET @del = (
    SELECT      CONCAT('DROP TABLE ', GROUP_CONCAT(TABLE_NAME), ';')
    FROM        information_schema.TABLES

    WHERE       TABLE_SCHEMA = 'database_name'
    AND         TABLE_NAME LIKE 'prefix_%'
);

PREPARE stmt FROM @del;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;
Danny Beckett
  • 20,529
  • 24
  • 107
  • 134
1

I found that the prepared statements were a little tricky to get working for me but setting the GROUP_CONCAT_MAX_LEN was essential when you have a lot of tables. This resulted in a simple three-step process with cut-and paste from the mysql command line that worked great for me:

SET GROUP_CONCAT_MAX_LEN=10000;
SELECT CONCAT( 'DROP TABLE ', GROUP_CONCAT(table_name) , ';' ) 
    AS statement FROM information_schema.tables 
    WHERE table_name LIKE 'myprefix_%';

Then carefully cut-and-paste the resulting long DROP statement.

Raptor
  • 53,206
  • 45
  • 230
  • 366
drchuck
  • 4,415
  • 3
  • 27
  • 30