How to empty Table in MySQL database Daily with cronjobs ?

Discussion in 'MySQL' started by duperhost, Apr 16, 2009.

  1. #1
    Lets say the database name DatabaseName1 and the Table Name TableName01 how we could empty this table with cronjob daily?
     
    duperhost, Apr 16, 2009 IP
  2. jakomo

    jakomo Well-Known Member

    Messages:
    4,262
    Likes Received:
    82
    Best Answers:
    0
    Trophy Points:
    138
    #2
    Hi!

    you can create a php script.

    truncatetable.php
    Example:
    <?
    $db_host = "localhost";
    $db_user = "username";
    $db_pass = "password";
    $db_name = "databasename";
    $connect = @mysql_connect($db_host,$db_user,$db_pass);
    @mysql_select_db($db_name);

    // Empty table
    $query = "TRUNCATE TABLE TableName01";
    mysql_query($query);
    ?>

    Then setup the cronjob
    Example:
    http://www.monetizers.com/cronjob.php

    0 0 * * * wget http://www.yourdomain/truncatetable.php


    More information about wget http://www.gnu.org/software/wget/manual/wget.html

    Best,
    Jakomo
     
    jakomo, Apr 17, 2009 IP
  3. mwasif

    mwasif Active Member

    Messages:
    816
    Likes Received:
    23
    Best Answers:
    1
    Trophy Points:
    70
    #3
    TRUNCATE TABLE command reset the auto_increment to 0. If you don't want to reset this, then simply use DELETE command.
     
    mwasif, Apr 17, 2009 IP
  4. duperhost

    duperhost Well-Known Member

    Messages:
    817
    Likes Received:
    7
    Best Answers:
    0
    Trophy Points:
    130
    #4
    jakomo, thanks
    mwasif, I want to empty not delete table structure. only records...
     
    duperhost, Apr 17, 2009 IP
  5. jakomo

    jakomo Well-Known Member

    Messages:
    4,262
    Likes Received:
    82
    Best Answers:
    0
    Trophy Points:
    138
    #5
    Hi dupehost,

    Your welcome, myasif say delete and truncate are different, for example if you have a column with auto increment with truncate it will setup to 0 and with delete the auto increment will continue with the last id number.

    With Delete you will not "delete" the table, to delete the table the sentence or command is DROP TABLE :)

    More information about the diference
    http://www.sunilb.com/mysql/difference-between-mysql-delete-and-truncate-table

    Best,
    Jakomo
     
    jakomo, Apr 17, 2009 IP
  6. mwasif

    mwasif Active Member

    Messages:
    816
    Likes Received:
    23
    Best Answers:
    1
    Trophy Points:
    70
    #6
    Thanks for explaining jakomo.
     
    mwasif, Apr 17, 2009 IP
  7. duperhost

    duperhost Well-Known Member

    Messages:
    817
    Likes Received:
    7
    Best Answers:
    0
    Trophy Points:
    130
    #7
    jakomo, Thanks, How about optimizing all tables how i can add it ?
     
    duperhost, Apr 17, 2009 IP
  8. jxcheng

    jxcheng Peon

    Messages:
    13
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #8
    This can be easily done via a Shell Scripts.
    You schedule a cron job to run the shell scripts daily.
    Inside the shell script, you login into mysql and clean up the tables.
     
    jxcheng, Apr 17, 2009 IP
  9. thr3146

    thr3146 Active Member

    Messages:
    182
    Likes Received:
    7
    Best Answers:
    0
    Trophy Points:
    58
    #9
    good idea to optimize all tables for sites with alot of traffic also- ie..new users and so on
     
    thr3146, Apr 17, 2009 IP
  10. fubaba

    fubaba Member

    Messages:
    13
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    36
    #10
    just make a sibmple executable file
    /usr/local/bin/mysql dbname < 'delete from table tablename;'

    note, path is for freebsd
     
    fubaba, Apr 23, 2009 IP
  11. duperhost

    duperhost Well-Known Member

    Messages:
    817
    Likes Received:
    7
    Best Answers:
    0
    Trophy Points:
    130
    #11
    It also can be run like this in cron :
    php -q /home/username/public_html/cronjobs.php
    Code (markup):
     
    duperhost, Apr 23, 2009 IP