Create temp file delete for MySql

Discussion in 'MySQL' started by ian_ok, Mar 15, 2006.

  1. #1
    I get temp files created in my database and i'd like a quick and easy code to delete these, also a friend who is running the same d/base has never deleted their and has over 8000!

    Could the code below be changed to a wildcard to delete any tables that are:
    default_temp_*

    I also have a table called default_temp which cannot be deleted! So it has to be anything called default_temp_

    Thanks Ian

    
    DROP TABLE `default_temp_6bb2824c646b02eca19751294f70cabe` ,
    `default_temp_33cdb9e7da8954e74aaefe0ca0669ee6` ,
    `default_temp_86e8a587156040c985c6890055feea4d` ,
    `default_temp_209b67489cfc5f9dc13ebc2c4e77edb6` ,
    `default_temp_84766867ba2a5127c18ba90dff0f9fb4`  ;
    Code (markup):
     
    ian_ok, Mar 15, 2006 IP
  2. chengfu

    chengfu Well-Known Member

    Messages:
    113
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    108
    #2
    You could use a simple script that does a "SHOW TABLES" sql-command and then issues a DROP TABLE for each table that matches the pattern.
     
    chengfu, Mar 15, 2006 IP
  3. sketch

    sketch Well-Known Member

    Messages:
    898
    Likes Received:
    26
    Best Answers:
    0
    Trophy Points:
    148
    #3
    I've never tried it with a table name but maybe doing something like DROP TABLE LIKE 'default_temp_%' ?
     
    sketch, Mar 15, 2006 IP
  4. chengfu

    chengfu Well-Known Member

    Messages:
    113
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    108
    #4
    Unfortunately (?) DROP TABLE doesn't work with wildcards. But if you have obtained a list of tables using SHOW TABLES LIKE they could all be deleted with one DROP TABLE command:
    
    DROP TABLE table1, table2, table3...
    
    Code (markup):
    The complete reference on DROP TABLE can be found here:
    http://dev.mysql.com/doc/refman/4.1/en/drop-table.html
     
    chengfu, Mar 15, 2006 IP
  5. forkqueue

    forkqueue Guest

    Messages:
    401
    Likes Received:
    21
    Best Answers:
    0
    Trophy Points:
    0
    #5
    Potentially very dangerous scriptlet you can run from the command line:
    (This is all one line, replace yourpass with your mysql root password, yourdatabase with your database name).

    PASSWORD=yourpass; DATABASE=yourdatabase; for table in `mysql -u root -p$PASSWORD -se "show tables;" $DATABASE | grep -v Tables_in_$DATABASE | grep '_temp_'`; do mysql -u root -p$PASSWORD -e "DROP table $table;" $DATABASE; done

    Make sure you backup your database first :)
     
    forkqueue, Mar 15, 2006 IP
  6. ian_ok

    ian_ok Peon

    Messages:
    551
    Likes Received:
    11
    Best Answers:
    0
    Trophy Points:
    0
    #6
    Thanks.....still working on this as the 'host' has come back and said the cpanel backup won't back up due to the size of the d/b (eg all the temp tables) - what crap, so as it's not my d/b I'm not keen on the 'dangerous script'!

    I'll let you know how I get on
     
    ian_ok, Mar 16, 2006 IP
  7. forkqueue

    forkqueue Guest

    Messages:
    401
    Likes Received:
    21
    Best Answers:
    0
    Trophy Points:
    0
    #7
    If cpanel can't backup do it yourself from the command line:

    mysqldump -u root -p -A | gzip -c9 > mysqlbackup-`date +%F`.gz
     
    forkqueue, Mar 17, 2006 IP
  8. ian_ok

    ian_ok Peon

    Messages:
    551
    Likes Received:
    11
    Best Answers:
    0
    Trophy Points:
    0
    #8
    When u say command line do you mean in shell access mode? Which she doesn't have...

    I've done show tables and then done this:
    DROP TABLE 'default_0016d78728f671e6e7a617d5e0b06a85temp','default_01635d8a30e1ecf2ff7c242c7493aba0temp',   'default_0ff4e7fa08c0f97e93a35690e669d032temp';
    Code (markup):
    etc....

    And I get this error:
    #1064 - 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 ''default_0016d78728f671e6e7a617d5e0b06a85temp','default_001a8ca4a205bdb34365d81f' at line 1
     
    ian_ok, Mar 17, 2006 IP
  9. chengfu

    chengfu Well-Known Member

    Messages:
    113
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    108
    #9
    The mysqldump-command has to be issued from the command line, not the sql shell. If you don't have ssh access this won't be easy.

    The DROP TABLE command should work if you omit the quotes.
     
    chengfu, Mar 17, 2006 IP
  10. ian_ok

    ian_ok Peon

    Messages:
    551
    Likes Received:
    11
    Best Answers:
    0
    Trophy Points:
    0
    #10
    BINGO....removing the quotes worked......now deleted the 8000 temp tables, hope she's grateful!

    Thanks a lot everyone.

    Ian
     
    ian_ok, Mar 17, 2006 IP