✍ 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 aPurge All
. (Empty Recycle Bin)
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 a Page 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 ofattachments
is huge.
💡 If you are reading this post, check the attachment with get_confluence_info.py