Re: [SPAM] Auto-empty trash items older then a certain age

Tom Collins <[email protected]> Mon, 12 Nov 2012 10:51:10 -0800
Newsgroups gmane.mail.qmail.admin
Message-ID <[email protected]>
Just want to throw out there that different email clients will use different names for their deleted items.

I'm using a cron job to clean up the "Trash", "Deleted Messages" and "Deleted Items" folders, including any folder that starts with those strings.  Survey your system and see what variations you find.

I decided not to share my Perl script, since it uses the file date (Perl's "-M" which returns days between script start time and file modification time), not the date it was moved into the Trash folder.  Hmm…  I just did some quick tests and it looks like I should use -C (days since file change time) to trigger of the timestamp of when the file was moved to the trash.

So, here's my script.  Interested in feedback.  I have it in my /etc/cron.daily so it runs every night.  The file globs take care of directory hashing for both domains and users.

I figured this was more efficient than recursing through the domain list, getting a domain's home directory, recursing through the user list and then processing the files.

-Tom

#!/usr/bin/perl

$|++;
$days = 45;

@globs = (
	'*/*/Maildir/.Trash*/{cur,new}',
	'?/*/*/Maildir/.Trash*/{cur,new}',
	'*/?/*/Maildir/.Trash*/{cur,new}',
	'?/*/?/*/Maildir/.Trash*/{cur,new}',

	'*/*/Maildir/.Deleted\ {Messages,Items}*/{cur,new}',
	'?/*/*/Maildir/.Deleted\ {Messages,Items}*/{cur,new}',
	'*/?/*/Maildir/.Deleted\ {Messages,Items}*/{cur,new}',
	'?/*/?/*/Maildir/.Deleted\ {Messages,Items}*/{cur,new}'
);

$c = 0;

print "Deleting mail in .Trash and .Deleted Messages folders, over $days days old.\n";

foreach $path (@globs)
{
	while ($name = glob ('/home/vpopmail/domains/' . $path))
	{
		opendir (D, $name) or print "failed to open $name\n";
		while ($f = readdir(D))
		{
			# skip non-files
			next if (! -f "$name/$f");
			$t = -C "$name/$f";
			if ($t > $days)
			{
				unlink "$name/$f";
				$c++;
			}
		}
	}
}

print "Deleted $c messages over $days days old\n";


On Nov 12, 2012, at 8:21 AM, Matt Rauch wrote:

>> Hello,
>> 
>> 	Sorry if this isn't the place to ask, but I've looked 
>> all over and haven't managed to find a clear solution. I'm 
>> looking for a way to have items that have been in the user's 
>> .Trash folder for a certain period of time (lets say 30 days 
>> or older) removed automatically. I thought there must be 
>> built-in functionality for this, or a script someone has come 
>> up with to manage this sort of thing. I've see a squirrelmail 
>> plugin that does it based on number of logins or every x 
>> number of days, but it empties the whole trash and doesn't 
>> check each message's age individually.
>> 
>> Any help would be greatly appreciated.
>> 
>> Thanks,
>> 
>> Matt Rauch
> 
> Just to update everyone. I did find a way to accomplish this even though we
> are using the vpopmail auth. I found this script online and modified it to
> fit my login info:
> ----------------------------------------------------------
> #!/usr/local/bin/bash
> # MySQL details
> 
> HOST="localhost";
> USER="vpopmail";
> PWD="vpopmail passwd";
> 
> # Output sql to a file that we want to run
> echo "USE vpopmail; select concat(pw_name,'@',pw_domain) as username from
> vpopmail;" > /tmp/query.sql;
> 
> # Run the query and get the results
> results=`mysql -h $HOST -u $USER -p$PWD -N < /tmp/query.sql`;
> 
> # Loop through each row
> for row in $results
> do
> /usr/local/bin/doveadm expunge -u $row mailbox Trash savedbefore 52w
> done
> 
> ---------------------------------------------------------
> 
> Maybe this can help someone else who is in the same boat.
> 
> Matt Rauch
> 
> 
> 
> 


!DSPAM:50a11bf333991914119077!