MySQL bulk delete tables based on regular expressions

Source: Internet
Author: User

Recently in the maintenance of a WordPress mall project, the mall is multi-station, will be independent of the station to create a few tables. Project on the test server, it seems to be a malicious crawler registration, resulting in thousands of users, resulting in tens of thousands of tables, resulting in the database import and export like a snail, slow to death, so it is only on the server to execute the SQL delete, first temporarily shut down the registration function, due to registration seemingly not verified conditions caused, Then I wrote a few SQL statements to delete the tens of thousands of extra tables.


First login to the server with SSH, log in to the MySQL database:

Mysql-uroot-proot

Note:-U is followed by your database username-p followed by your database password


It then generates an SQL statement that deletes the table according to the regular expression and saves:

Select CONCAT (' drop table ', table_name, '; ') From Information_schema.tables

WHERE table_name REGEXP ' ss_[0-9]{3,}.. ' into outfile ' del.sql ';;


Note: REGEXP is followed by regular expressions that need to delete the table name

Here are some common rules for MySQL regular expressions:

Logical OR: SELECT * from S_user where name REGEXP ' A|b|c '

Specific character characters: SELECT * from S_user where name REGEXP ' [abc]123 '

Matching range: [0-9] [A-z]

Repeating meta characters: * 0 or more

+ one or more

? 0 or one

{n} specified number of times

{N,M} specified number of times range

{N,} The specified number of times range above

Anchor metacharacters: ^ Beginning of line

$ End of line

Any number of characters:..

.... More specific matching rules can be Google a bit


The SQL file here is imported by default to/var/lib/mysql/del.sql here.


Then we execute del.sql inside the command can be, if not rest assured, you can use VI open del.sql look inside the content:

Execution: Source Del.sql






This article is from the "hope to the end of the Road" blog, please be sure to keep this source http://wujingchao.blog.51cto.com/6558896/1563513

MySQL bulk delete tables based on regular expressions

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.