LoginSignup
1
0

More than 3 years have passed since last update.

Check and Delete Confluence attachments

Posted at

banner-wiki000.png

✍ How to delete Confluence attachments with api call.
I used https://github.com/pycontribs/confluence

Test environment

  • Confluence 6.1.1
  • Python 2.7.14

Way

Attachment can be deleted by calling curl (Recycle Bin)
To physically completely delete from the server, you need to do a Purge All. (Empty Recycle Bin)
wiki001.png

1) Attachment can be deleted by calling curl

✅ The id of the attachment is required.
Each attachment has its own id.

$ vi delete_attachment.py
# -*- coding: utf-8 -*-
import sys
import os

reload(sys)
sys.setdefaultencoding('utf-8')

attachmentId = '' # attachment id
delete_cmd = "curl -v -S -u \"<id>:<password>" -X DELETE \"https://<confluence_url>/rest/api/content/" + str(attachmentId) +"\""

os.system(delete_cmd)

2) The source code to find the id of the attached file.

✅ You need a Space KEY and aPage Title.

$ vi get_attachment_id.py
# -*- coding: utf-8 -*-
from confluence import Confluence
import sys

reload(sys)
sys.setdefaultencoding('utf-8')

confluence = Confluence(profile='confluence')

page_title = '' # Page Title
space_key = '' # Space KEY

attachments = confluence.getAttachments(page_title, space_key)

for attachment in attachments :
    a_title = attachment['title']
    a_url = attachment['url']
    a_creator = attachment['creator']
    a_fileName = attachment['fileName']
    a_id = attachment['id']
    # MB
    a_fileSize = int(attachment['fileSize']) / 1000000
    # attachment print id 
    print(str(a_id))

3) ✅ This code gets the Space KEY and Page Title for the entire Confluence.

$ vi get_confluence_info.py
# -*- coding: utf-8 -*-
from confluence import Confluence
import sys

reload(sys)
sys.setdefaultencoding('utf-8')

confluence = Confluence(profile='confluence')
allSpace = confluence.getSpaces()

for space in allSpace :

    if space['type'] == 'global':

        spaceKey = space['key']        

        pages = confluence.getPages(spaceKey)

        print('-' * 100)
        # Print Space Name and Key
        print(str(space['name']) + "\t" + str(spaceKey))

        total_attachment = 0
        total_fileSize = 0        
        for page in pages :
            pageTitle = page['title']
            # Print Page Title
            print(pageTitle)
            attachments = confluence.getAttachments(pageTitle, spaceKey)
            total_attachment += len(attachments)

            for attachment in attachments :
                attachment_title = attachment['title']                
                # MB size
                file_size = int(attachment['fileSize']) / 1000000
                total_fileSize += file_size

        print(str(total_attachment)) + " Count"
        print(str(total_fileSize)) + " MB"

Lesson

The Confluence Page Copy feature creates a new page including attachments from existing page.
When copying a page, there is a trap that makes it invisible for the attachment to be copied. 😱
※ After Confluence v7.1, the ability to skip attachments is added.
If the copy is producing a copy, starting with a heavy page, the amount of attachments is huge.

💡 If you are reading this post, check the attachment with get_confluence_info.py
wiki002.png

1
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
0