Skip to content

Instantly share code, notes, and snippets.

@jheth
Created February 11, 2015 03:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jheth/442ee65d9c58e0beaf71 to your computer and use it in GitHub Desktop.
Save jheth/442ee65d9c58e0beaf71 to your computer and use it in GitHub Desktop.
Clear Old Session Records
DROP procedure IF EXISTS `ClearSessions`;
DELIMITER $$
CREATE PROCEDURE ClearSessions(IN days_ago INT)
BEGIN
DECLARE bDone INT;
DECLARE count INT;
DECLARE sessionId INT;
DECLARE curs CURSOR FOR SELECT id FROM sessions WHERE updated_at < DATE_SUB(NOW(), INTERVAL days_ago DAY);
DECLARE CONTINUE HANDLER FOR NOT FOUND SET bDone = 1;
OPEN curs;
SET bDone = 0;
SET count = 0;
REPEAT
FETCH curs INTO sessionId;
DELETE FROM sessions WHERE id = sessionId;
SET count = count + 1;
UNTIL bDone END REPEAT;
CLOSE curs;
SELECT count AS 'Deleted';
END$$
DELIMITER ;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment