If a private messages is being deleted, it stores into Trash folder. Here, I 'd like to set a time limit and after that time, PM might be permanently deleted from Trash.

I was looking for any configuration variable for it but didn't get luck. Could anyone please help me for it.


It doesn't look like you have any nice extension hooks for the PM system. What you'll likely need to do is create a custom extension that hooks a member logging in and deletes the 'deleted' messages.

Step 1 Create an extension that hooks a member login

Here you'll need to hook member_member_login_single, see this documentation on how to wrap an extension : https://ellislab.com/expressionengine/user-guide/development/extensions.html

Step 2 Check for deleted messages for the user

From Step 1 you should have the members ID (if not it should be in the global vars) so what you can do is execute a query like this to get all the messages from the DB that belong to the user and are deleted (in the trash) :

SELECT mc .*,md.* 
FROM exp_message_copies AS mc 
JOIN exp_message_data AS md 
ON mc.message_id = md.message_id 
WHERE mc.message_deleted = 'y' 
AND mc.recipient_id = 2 
AND md.message_date < (UNIX_TIMESTAMP( NOW( ) ) - ( 3600 *12 ))

assuming here that the member ID logging in is 2 and we're looking for messages that are ovef 12 hours old (3600 * 12)

In EE, we can use ActiveRecord for this :

$query = ee()->db->select('mc.*,md.*')
             ->from('message_copies AS mc')
             ->join('message_data AS md', 'mc.message_id = md.message_id')
             ->where('mc.message_deleted', 'y')
             ->where('mc.recipient_id', $memberId)
             ->where('md.message_date <', '(UNIX_TIMESTAMP(NOW()) - ' . $period . ')')

here we assume $memberId is the id for the logging in member, $period is the cut off, so now - 12 hours == 43200

Step 3 Delete the deleted messages!

From the code above, we'll have none or more items in the $query record set. We can then iterate these and delete them from the db (message_copies table).

if ( $query->num_rows() > 0 ){
    foreach ( $query->result_array() as $row ) {
        ee()->db->where('copy_id', $row['copy_id'])

Voila! The hardest part of this may be building the actual extension! Hope this answers your questions, I tested this (I didn't create the module, but I did create real PM's and tested the delete code) on EE v2.8.1

One last note, I notice that doing this removes the message from the senders 'send' folder as well. I can't be sure if this was ever copied to the sent folder though! I believe by looking at the code another method would be to replace the delete statement with an update, and update the message_folder to 0. I did some further checking and the above method won't interfere with sent messages

After some more looking around, I notice from the PM libraries (check out the erase funtion on line 2022ish on /system/expressionengine/libraries/Messages.php) that when messages are deleted you'll need to clean up attachments too (line 2077) and update the message count (line 2097) :

$query = ee()->db->query("SELECT COUNT(*) AS count FROM exp_message_copies
                         WHERE recipient_id = '{$this->member_id}'
                         AND message_read = 'n'");

$results = ee()->db->query("UPDATE exp_members SET private_messages = '".ee()->db->escape_str($query->row('count') )."'
                            WHERE member_id = '{$this->member_id}'");
  • Did this help at all Bhashkar? I've added some extra notes regarding sent messages, attachements and message count clean up for you. – Blatant Apr 9 '15 at 11:04
  • Do I get the bounty if the OP author doesn't click this as the answer? It's only a day left and I need the Rep! I mean its a number that grants very bespoke priviledges, I NEED it!! hah :) Would be nice to have an answer o 2 in my history :) – Blatant Apr 10 '15 at 9:23

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.