I must have done something really stupid, but the following is correct:
CREATE EVENT delete_old
ON SCHEDULE
EVERY 1 DAY
COMMENT 'Clears old cache data from the DB.'
DO
DELETE FROM summoners
WHERE `date` < (NOW() - INTERVAL 7 DAY);
Where the next bit seems to throw a syntax error on the last 2 lines:
CREATE EVENT delete_old
ON SCHEDULE
EVERY 1 DAY
COMMENT 'Clears old cache data from the DB.'
DO BEGIN
DELETE FROM summoners
WHERE `date` < (NOW() - INTERVAL 7 DAY);
END;
The syntax to my knowledge is correct, however MySQL Workbench does not agree. I intend to do multiple tables inside the BEGIN - END section, that is why i need it.
I hope someone can figure out what goes wrong here, i am at a loss.
Thanks in advance,
Smiley
You have to change the DELIMITER to something that doesn't appear in your event body.
I just tried it (MySQL Workbench 6.0.6, MySQL 5.6) and it works fine. Here's a screenshot:
It's because ; in body breaks your command in the middle. Use different delimiter.
DELIMITER |
CREATE EVENT delete_old
ON SCHEDULE
EVERY 1 DAY
COMMENT 'Clears old cache data from the DB.'
DO BEGIN
DELETE FROM summoners
WHERE `date` < (NOW() - INTERVAL 7 DAY);
END;
| DELIMITER ;
Related
I want to delete rows from the database that are older than 7 days. I went through some questions here on Stack but can't find one that helped at all.
I already found this way to do it:
DELETE FROM users WHERE updateDt < (NOW() - INTERVAL 7 DAY)
And this, to create a event in the database:
DELIMITER $$
CREATE EVENT `delete7DayOldMessages`
ON SCHEDULE EVERY 1 DAY STARTS '2015-09-01 00:00:00'
ON COMPLETION PRESERVE
DO BEGIN
delete from users
where datediff(now(),updateDt)>6;
END;$$
DELIMITER ;
Is it right to transform these two examples above into this:
DELIMITER $$
CREATE EVENT `delete7DayOldMessages`
ON SCHEDULE EVERY 1 DAY STARTS '2015-09-01 00:00:00'
ON COMPLETION PRESERVE
DELETE FROM users WHERE updateDt < (NOW() - INTERVAL 7 DAY)
DELIMITER ;
?
I've made slight modifications to the SQL code provided in an answer to this question: How to delete a MySQL record after a certain time
However, I get a "You have an error in your SQL syntax" error each time I run the query.
create event delete_session
on schedule at current_timestamp + interval 1 day
on completion preserve
do begin
delete from session where date < DATE_SUB(NOW(), INTERVAL 7 DAYS);
end;
The code should create an event to delete entries from the session table after 7 days, but instead gives me this error. Is there actually a problem with the syntax here?
Your CREATE EVENT command and DELETE command is using the ; as delimiter. So your CREATE EVENT command is ending after the DELETE command (before END). You need to set the DELIMITER at the beginning to use another one on the CREATE EVENT command.
-- set the DELIMITER to "|"
DELIMITER |
CREATE EVENT delete_session
ON schedule AT current_timestamp + INTERVAL 1 DAY
ON COMPLETION PRESERVE
DO BEGIN
DELETE FROM session WHERE `date` < DATE_SUB(NOW(), INTERVAL 7 DAY);
END |
-- set the DELIMITER back to the default.
DELIMITER ;
... and you need to change one more thing:
remove the trailing S on DAYS.
I have an event that deletes record on a table that are a month old. But it does not seem to execute.
DELIMITER $
CREATE EVENT delete_wykofile_back
ON SCHEDULE EVERY 1 MINUTE
STARTS CURRENT_TIMESTAMP + INTERVAL 1 DAY
ENDS CURRENT_TIMESTAMP + INTERVAL 5 YEAR
ON COMPLETION PRESERVE
DO
BEGIN
DELETE FROM factory.wyko_file_backup WHERE date_inserted < DATE_SUB(NOW(), INTERVAL 1 MONTH);
END$
DELIMITER ;
What I've done so far:
I have followed and set global event_scheduler to ON based from this question
I've also check this question, but to no avail.
There's also no answer from this question.
The account im using is a superuser based from this.
SHOW EVENTS;
SHOW PROCESSLIST;
I am using MySQL Workbench 6.2. I am running the event with 1 minute interval for testing.
STARTS CURRENT_TIMESTAMP + INTERVAL 1 DAY
needs to be the current date or a past date. Right now its set up to start on 12/4/2015.
I'm fairly new in programming. Everything went smooth so far, but I can't solve this. I'm using MySQL Workbench 5.2.42
I know I need to create a stored procedure
This procedure needs to delete all data in my database, but only if the data I over 15 minutes old. I can access the time from my timestamp at the ExpectedEnd column. But I don't know how to do this right.
CREATE PROCEDURE p_DeleteOldData()
BEGIN
IF ExpectedEnd IS TIMESTAMP + 15
DELETE * tables FROM (DelayedLoading, Loading, LoadingDock, LoadingError, Orders, PartOrders, Semi, Trailer, Truck, Weighing)
End ;
I made this though its surely completely wrong.
edit*
I tried this, but i still can't figure how to get it to the 15 minute mark and delete.
CREATE PROCEDURE p_DeleteOldData(IN theTime DATETIME)
BEGIN
DELETE FROM Loading WHERE ExpectedEnd < theTime;
END;
Thank you in advance.
CREATE PROCEDURE p_DeleteOldData(IN theTime DATETIME)
BEGIN
DELETE FROM Loading WHERE ExpectedEnd < DATE_ADD(theTime, INTERVAL -15 MINUTE);
END;
You can see other interval values in the docs (e.g. HOUR, DAY, WEEK, MONTH) and you can even just use NOW() instead of having to pass a time value into the stored procedure if you are only looking at the current point in time
I want to delete some messages from my MySQL database after 7 days.
My message table rows have this format:
id | message | date
The date is a timestamp in the normal format; 2012-12-29 17:14:53
I was thinking that an MySQL event would be the way to go instead of a cron job.
I have what I guess is a simple question to an experienced SQL person, how do I code the delete messages portion in brackets below?
An example would be appreciated, Thanks.
DELIMITER $$
CREATE EVENT delete_event
ON SCHEDULE AT CURRENT_TIMESTAMP + INTERVAL 1 DAY
ON COMPLETION PRESERVE
DO
BEGIN
DELETE messages WHERE date >= (the current date - 7 days);
END;
$$;
You can try using this condition:
WHERE date < DATE_SUB(NOW(), INTERVAL 7 DAY)
So that the whole SQL script looks like this:
CREATE EVENT delete_event
ON SCHEDULE AT CURRENT_TIMESTAMP + INTERVAL 1 DAY
ON COMPLETION PRESERVE
DO BEGIN
DELETE messages WHERE date < DATE_SUB(NOW(), INTERVAL 7 DAY);
END;
However, on your place I would solve the given problem with a simple cron script. The reasons to do this is simple: it's easier to maintain the code, no ugly SQL workarounds, integrates smoothly with your system.
This should do the trick.
DELETE FROM messages WHERE date < (CURDATE() - INTERVAL 7 DAY);
For those out there who are on a shared hosting, like 1and1's, and can't create events, an alternative is to use webcron
You just need to tell webcron the url of the php script you'd like to be run, and they'll trigger it for you at the intervals you want