Is it possible to purge attachment history?
Asked by Brett Ryan:

We have a situation where our attachment growth is expanding quite rapidly as users are utilising Confluence as a collaboration tool around office documents. Due to this the prior versions quickly build up which turns a file of a few MiB in size to a few hundred MiB.

I'm wondering if there is a third-party plugin or some way you could let me know to periodically purge history? We would ultimately like to be able to purge history within a designated period of time.

Looking at our attachments table shows there is a moderate 3,561 attachment documents, of this there are a total 4,953 when counting prior versions.

I could quite simply use a mechanism in SQL to print and purge content with a query such as:

```
select attachmentid,       title,       pageid,       creator,       lastmoddate  from attachments where prevver is not null   and datediff(curdate(), lastmoddate) &gt; 30;
```

And then delete the subsequent attachment file, however I would gather there are two problems to this:

1. Confluence would need to be off-line
2. Lucene cache would need to be rebuilt (maybe?).

I'd really prefer not using this method unless it's proven to be safe.

If there are no other ways, no plugins etc, I'd be happy to investigate writing such a plugin provided the api's are available from within confluence to delete prior versions. If they are, could you kindly point me in the right direction to get me started on this? My plugin authoring experience has currently been only for themes and macros.


Answers:
author: Brett Ryan
body: I have decided to solve this by creating a plugin with a schedule to purge attachments as needed.

Available on [github.com](http://github.com) you can find the [Attachment Tools Plugin](https://github.com/brettryan/confluence-attachment-tools-plugin "Attachment Tools Plugin"). I should be releasing a version later this week.

comments: author: Sorin Sbarnea (Citrix)
body: Hi Brett, Does you plugin still works with Confluence 5.4? Last change was made more than two years ago so I think it makes sense to ask :)


author: Infrastructure_Administrator
body: Hi Sorin the plugin should work fine on 5.x and I have tested on an earlier 5.x version previously but have not updated the plugin.

Internally our company has had many issues with many plugins moving to 5.x and are still on 4.x ourselvs. Atlassian really created a nightmare with 5.x that we still aren't happy about.

I hope to start looking at this soon actually and we should have something verified for 5.x releases within a month.


author: Brett Ryan
body: Hi Sorin the plugin should work fine on 5.x and I have tested on an earlier 5.x version previously but have not updated the plugin.

Internally our company has had many issues with many plugins moving to 5.x and are still on 4.x ourselvs. Atlassian really created a nightmare with 5.x that we still aren't happy about.

I hope to start looking at this soon actually and we should have something verified for 5.x releases within a month.


author: Brett Ryan
body: I've just finished the plugin, was working fine in report mode, so decided to turn on the deletions.

Little did I know that calls to AttachmentManager.removeAttachmentFromServer(Attachment) remove ALL VERSIONS.

This is a huge setback for me as I now spent three days writing a plugin only to find that the one method I need doesn't behave as I first thought.

Looking at com.atlassian.confluence.pages.persistence.dao.hibernate.NonTransactionalHibernateAttachmentDao reveals the following:

```
if (!attachment.isLatestVersion()){    attachment = (Attachment) attachment.getLatestVersion();}
```

Sad face. Really sad face :(


author: Brett Ryan
body: Thanks Matthew, have decided to write a plugin for it. Just need to figure out how to email results and it will be pretty much complete. If you know how to [send email from confluence](/questions/68340/what-is-the-correct-way-to-send-emails-from-within-a-plugin) it would be a big help :)


author: Matthew J. Horn
body: I don't know of a better way, but Confluence would not have to be off line to run your purging query. The cache would need to be rebuilt, though.


Tags: attachment

purge