Jump to content

sql query to delete old tickets from anonymous users


evcz

Recommended Posts

Hi,

 

do you think having an SQL query to delete old (older then 1 month for example) tickets filled by anonymous users completely removed would be doable without breaking WHMCS?

Basically we are talking about sales requests or similar things.

 

It makes sense to retain tickets from account, but the ones from anonymous sources should be deleted in order to have the least possible personal data stored.

 

Is there any example to start from or any already existing module?

 

Thank you

Link to comment
Share on other sites

hooking at the cronjob make sense, but my main question is running these two queries:

DELETE FROM `tblticketreplies` WHERE `userid` = 0 AND `date` < DATE_SUB(NOW(), INTERVAL 1 MONTH)
DELETE FROM `tbltickets` WHERE `userid` = '0' AND `lastreply` < DATE_SUB(NOW(), INTERVAL 1 MONTH)

is safe to run and do not leave things broken behind

the only thing left broken behind are the attachments, but those are taken care on 7.8 beta with automatic old attachments pruning

another incosistency is removing a ticket before all tickets before their respective ticket replies are deleted, but that's not a big deal, just a metter of delayed days

Edited by evcz
Link to comment
Share on other sites

1 minute ago, evcz said:

the only thing left broken behind are the attachments, but those are taken care on 7.8 beta with automatic old attachments pruning

it would leave behind replies to those tickets too, as any ticket replies (from admins or users) would still be in tblticketreplies - a similarSQL query wouldn't work as is because all admin replies have the userid of 0, so it would need to be a more nuanced query... Kian's idea of using the API would get rid of both the original ticket and any replies.

either way, i'd suggest making a copy of the database before running any sql queries like that.

Link to comment
Share on other sites

11 minutes ago, evcz said:

another incosistency is removing a ticket before all tickets before their respective ticket replies are deleted, but that's not a big deal, just a metter of delayed days

note what I said about ALL admin replies to tickets having a userid of 0 - that replies query would delete replies to ALL (clients and non clients) tickets between those dates... you might have to join the tables to get an accurate list f valid tickets to delete.

Link to comment
Share on other sites

ouch, didn't know that admin replies had userid 0

 

then this as a select makes more sense then

 

SELECT * FROM tbltickets RIGHT OUTER JOIN tblticketreplies ON tbltickets.id = tblticketreplies.tid WHERE tbltickets.userid = '0' AND tbltickets.status = 'Closed' and tbltickets.date <  DATE_SUB(NOW(), INTERVAL 1 MONTH)

change the 

SELECT *

with

DELETE tbltickets, tbltitcketreplies

and we should be good.

 

how does it looks to you?

Link to comment
Share on other sites

  • 2 weeks later...

Pulled the trigger and did

DELETE tbltickets, tblticketreplies FROM tbltickets RIGHT OUTER JOIN tblticketreplies ON tbltickets.id = tblticketreplies.tid WHERE tbltickets.userid = '0' AND tbltickets.status = 'Closed' and tbltickets.date <  DATE_SUB(NOW(), INTERVAL 6 MONTH)

to remove tickets from anonymous users older then 6 months, now just need to wait for the next WHMCS version for the auto pruning of attachments before doing the delete here and we should be fine 🙂

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...

Important Information

By using this site, you agree to our Terms of Use & Guidelines and understand your posts will initially be pre-moderated