Latest: Simple MySQL Table Archive Stored Procedure Listing (Note the column 'serverdatetime' has a datetime data type) -- archive log data older than 30 days to history table CREATE PROCEDURE log_archive () BEGIN DECLARE current_count INT; DECLARE EXIT HANDLER FOR SQLWARNING ROLLBACK; DECLARE EXIT HANDLER FOR SQLEXCEPTION ROLLBACK; -- get count of current rows to be archived SELECT count(*) into current_count from YOURLOGTABLENAME where DATE(serverdatetime) < date_sub(CURDATE(),interval 30 day); IF current_count > 0 THEN START TRANSACTION; -- archive those rows to a history table INSERT into YOURLOGTABLENAME_history (SELECT * from YOURLOGTABLENAME where DATE(serverdatetime) < date_sub(CURDATE(),interval 30 day)); -- only delete main table rows if archive successful -- note any auto_increment values will remain IF ROW_COUNT() > 0 THEN DELETE from YOURLOGTABLENAME where DATE(serverdatetime) < date_sub(CURDATE(),interval 30 day); END IF; COMMIT; END IF; OPTIMIZE TABLE YOURLOGTABLENAME;