Check and Delete Confluence attachments

![banner-wiki000.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/144058/5953614c-7623-e8c0-1521-e8a21ea6a297.png)
> ✍ How to delete [Confluence](https://www.atlassian.com/ko/software/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](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/144058/e821c0ae-bd07-df32-3fcd-cd431deef709.png)

#### 1) Attachment can be deleted by calling `curl`
✅ The `id` of the attachment is required.
Each attachment has its own `id`.

```bash
$ vi delete_attachment.py
```
```python
# -*- 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`.

```bash
$ vi get_attachment_id.py
```
```python
# -*- 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.

```bash
$ vi get_confluence_info.py
```

```python
# -*- 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](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/144058/5bcaa4e2-865a-5ec4-fbb1-acf959b60569.png)