Search for unused attachments in page
Asked by ABoerio:

Hi All,

after having read [this](https://community.atlassian.com/t5/Confluence-articles/Bulk-Updating-Page-Content-in-Confluence-Server-using/ba-p/1385821?utm_campaign=SR4C%20newsletter%20every%203-4%20months&utm_medium=email&_hsmi=93031857&_hsenc=p2ANqtz--YZjjWihyhaKsI19gcQS_T7MawbEX-CZUx4x5f8WUJNY5XYje6TdXHIWW1Od5ZtWnHg2CrzWrD3xKYycRM9TFYdmMa1nSF_io-AHzWQRGXNSS_9oQ&utm_content=93031857&utm_source=hs_email) article, I wrote a short script to compare the filenames of the attachments actually used in a page with the filenames of the files actually attached to the page.

I'll make it then cycle through the various spaces to look for the many attcahments that users have been draggin in and then simply forgot there.

I still have to check for those attachments that are no more present in their storage page but maybe linked from some other page, even though I guess this should be a rare case.

As usual for me, it's not perfect/elegant \& not finished, but it seems to do what I need.

If it can be of some start/help for anybody, then I'm happy.

> ```
> import com.atlassian.confluence.pages.PageManager
> import com.atlassian.sal.api.component.ComponentLocator
> import com.atlassian.confluence.pages.Attachment
> import com.atlassian.confluence.pages.AttachmentManager
> import org.jsoup.Jsoup
>
> def pageManager = ComponentLocator.getComponent(PageManager)
> def attachmentManager = ComponentLocator.getComponent(AttachmentManager)
>
> //Aqcuisisco pagina
> def page = pageManager.getPage('~MySpace', 'Check Attachments')
> //Acquisisco contenuto pagina
> def body = page.bodyContent.body
>
> //Definisco lista allegati citati nella pagina
> def usedAttachments=[]
> //Definisco lista di tutti gli allegati della pagina
> def existingAttachments=[]
> //Definisco lista allegati non utilizzati
> def unusedAttachments=[]
>
> //Faccio un parse del contenuto pagina
> def parsedBody = Jsoup.parse(body)
> //Cerco blocco attachment
> def attachments = parsedBody.select("ri|attachment")
> //Scorro tutti i blocchi attachment trovati
> attachments.each(){
> attachment ->
> //log.warn(attachment.attr('ri:filename'))
> usedAttachments.add(attachment.attr('ri:filename'))
> }
>
> //Cerco allegati alla pagina
> def loadedAttachments=attachmentManager.getLatestVersionsOfAttachments(page)
> loadedAttachments.each(){
> lattachment->
> //log.warn(lattachment.fileName)
> existingAttachments.add(lattachment.fileName) 
> }
>
> //Riordino le liste
> existingAttachments.sort()
> usedAttachments.sort()
>
> //Controllo utilizzo
> existingAttachments.each(){
> eAttachment->
> if(!usedAttachments.contains(eAttachment)){
> unusedAttachments.add(eAttachment)
> }
> }
>
> log.warn('Allegati alla pagina:')
> log.warn(existingAttachments)
>
> log.warn('Presenti nella pagina:')
> log.warn(usedAttachments)
>
> log.warn('Allegati non utilizzati nella pagina:')
> log.warn(unusedAttachments)
> ```
>
Ciao, A


Answers:
author: Joshua Yamdogo @ Adaptavist
body: Hi [@ABoerio](/t5/user/viewprofilepage/user-id/2035799)

Thanks for sharing this script! I will pass this along to the rest of the ScriptRunner for Confluence team. We enjoy seeing how our users are utilizing SR - it is insightful for us. ?

Regards,

Josh


author: Dave Liao
body: [@ABoerio](/t5/user/viewprofilepage/user-id/2035799) - grazie mille! ?

This is a useful script for admins to houseclean and potentially free up storage capacity.


Tags: attachment

confluence-server

page

scriptrunner

server