It's not the same without you

Join the community to find out what other Atlassian users are discussing, debating and creating.

Atlassian Community Hero Image Collage

Script to delete attachment's version

I'm trying to write a script job to scan and delete attachment versions on a page.

For example on a page I have attach file A and reattach a few times to get 5 versions for file A. I want to write a script to delete the first 4 version.

Here's my attempt:

import com.atlassian.confluence.pages.Attachment
import com.atlassian.confluence.pages.AttachmentManager
import com.atlassian.sal.api.component.ComponentLocator

def attachmentManager = ComponentLocator.getComponent(AttachmentManager)

hits.each { attachment ->
def attachmentOnPage = attachmentManager.getAttachments(attachment)
if (attachmentOnPage.size() >1)
log.warn "attachmentOnPage: ${attachmentOnPage}"
log.warn "size: ${attachmentOnPage.size()}"
int size = attachmentOnPage.size()
int sizeMax = size - 1
List<String> attachmentDeleteList = new ArrayList<String>()
for(int i=0;i<size;i++)
if (i<sizeMax)
def attachmentSingle = attachmentOnPage.get(i)
log.warn "attachmentDeleteList: ${attachmentDeleteList}"
//log.warn "attachmentSingle: ${attachmentSingle}"

However, when I try to run the removeAttachmentFromServer method, it delete every versions and the file itself in the database. I also tried the removeAttachment(attachmentDeleteList) and it does the same thing. Is there any method that just delete the version not the attachment itself.

1 answer

0 votes
Bill_Bailey Community Leader Jun 06, 2019

Well maybe it is easier to do via a REST call? The URL for deleting an attachment version takes the form of:<your file name>&version=n

Where n is the version number. Now I don't know if you delete version 1, if the rest of the attachments will renumber so you will have to experiment (meaning you may have to delete version 1 four times in your case).

We would like to automate this using the scheduled script job in Scriptrunner though. I've managed to make my script works. Will post the answer here shortly. 

Hey Nyugen, 

Any chance you got this script working - looking to do something similar so would be a great help.


Like Alex_Gregory likes this

Suggest an answer

Log in or Sign up to answer
Community showcase
Posted in Confluence

What project did you transition or start on Confluence with the shift to remote work?

It’s been great to hear from fellow users over the last few weeks about the best tips and fun moments you’ve had working on Confluence since the transition to working remote. I’d love to keep the c...

109 views 3 7
Join discussion

Community Events

Connect with like-minded Atlassian users at free events near you!

Find an event

Connect with like-minded Atlassian users at free events near you!

Unfortunately there are no Community Events near you at the moment.

Host an event

You're one step closer to meeting fellow Atlassian users at your local event. Learn more about Community Events

Events near you