Gossamer Forum
Home : Products : Gossamer Forum : Discussion :

Deleting Attachments

Quote Reply
Deleting Attachments
Is there a way to delete attachments by age but keep the post. In the admin area you can delete posts older than x days. How would one delete attachments but leave the posts, other than manually deleting them one by one? Text takes very little storage but attachments add up to a lot of space.
Quote Reply
Re: [Eric P] Deleting Attachments In reply to
Hi,

No, there isn't an easy way to do this. You could write a perl script that would go through the attachments directory, look at the age of the file, and then unlink it and update the post table if it's over a ceartain age.

Cheers,

Alex
--
Gossamer Threads Inc.
Quote Reply
Re: [Alex] Deleting Attachments In reply to
Alex

I was thinking along those lines. I'm comfortable with everything except messsing with the post table. Can you give a code snippet or provide some other help?
Quote Reply
Re: [Eric P] Deleting Attachments In reply to
Hi Eric,

I think the easiest way to go about it would not be to look at the actual modification time of the file, but rather the date of the post. In this way, all posts older than (for example) 30 days will have their attachments removed. It makes the work a little easier, and the case where someone uploads a new attachment to a 30-day-old post is quite rare.

This code snippit should delete the attachments from any posts older than 30 days UNLESS they have been selected as "kept". The attachments are automatically deleted from disk.

Code:

my $days_old = 30;my $post = $DB->table('Post');
my $patt = $DB->table('PostAttachment');my $cond = GT::SQL::Condition->new(
post_keep => '=' => 0,
post_time => '<' => time - 24 * 60 * 60 * $days_old,
post_has_attachments => '>=' => 1
);my $sth = $post->select(post_id => $cond);
while (my $post_id = $sth->fetchrow) {
$patt->delete({ post_id_fk => $post_id });
}

Jason Rhinelander
Gossamer Threads
jason@gossamer-threads.com
Quote Reply
Re: [Jagerman] Deleting Attachments In reply to
Jagerman

That is a huge help. What I thought I might do it insert your code in the TempAttachment.pm file. That way attachment deletion is triggered by a new attachement. Sort of a pop off push on kind of process. Can you think of a better way or any problems with this?