How to delete attachments filtered by date from a Confluence Cloud page using the REST API

Platform Notice: Cloud Only - This article only applies to Atlassian products on the cloud platform.

Summary

The steps outlined in this article are provided AS-IS. Consequently, Atlassian Support cannot guarantee the provision of any support for the steps described on this page as customizations are not covered under Atlassian Support Offerings. Please be aware that this material is provided for your information only and that you use it at your own risk. and they are not officially supported, nor can we guarantee they'll work for your specific scenario.

You may follow through and validate them on your own non-prod environments prior to production or fall back to supported alternatives if they don't work out.

We also invite you to reach out to our Community for matters that fall beyond Atlassian's scope of support!

Using the REST API might be helpful to automate some operations on Confluence.

A Confluence user or the administrator may need to delete a set of page attachments, but going through the User Interface (UI) could be rather impractical depending on the number of target attachment files.

This document provides a step-by-step procedure on how to use the Confluence REST API to bulk delete attachments, selected by a date condition.

One use case sample is when the page contains a large number of attachments thus the attachments page will no longer display so it can not be cleaned up by a user.

The suggested solution is provided using Python to run the REST API call. You may use this as an example to create an automation tool in your preferred coding language.

Environment

Confluence Cloud

This suggestion is for Confluence Cloud. Using Confluence Server or Data Center? See the similar article How to delete an attachment from a Confluence page using the REST API

Solution

The suggested solution will permanently delete all versions of the target attachment.

It is recommended you create a space backup before deleting the attachments. 

Python

For this sample code, we used Python v3 and the sample on the Confluence Cloud documentation for REST/API v2.

This code sample uses the 'requests' library available on http://docs.python-requests.org, which can be installed running the following Python command:

1 python3 -m pip install requests

1. Define the required variables used by the script:

    1. user_name= Anyone with the Delete Attachment space permission can delete a file that is attached to a page. However, you must also have the Space Administrator permission to delete a specific version of a file.

    2. user_password= Create an API token using a Space Administrator user

    3. confluence_baseurl= Confluence Cloud url without the trailing slash at the end:https://xxxxxxxx.atlassian.net/wiki

    4. pageId= Get the Page ID where the attachments will be deleted. For this sample, the Page ID used is 13008897

    5. createdAt= Date that will be used to select which pages will be deleted. For this sample, we will remove all attachments older that this date.

2. Getting information about all the page attachments:

This optional step can be used to gather information about each page attachment.

Replace USER_NAME, USER_PASSWORD, CONFLUENCE_BASEURL and PAGEID.

We will use this result to identify the creation date that will be used on the next script to delete old attachments.

getPageAttachments.py

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 # This code sample uses the 'requests' library: # http://docs.python-requests.org import requests from requests.auth import HTTPBasicAuth import json # Define the following variables user_name = "joedoe@atlassian.com" user_password = "ATATT3QWUUOIUQWO.....14FED" confluence_baseurl = "https://myconnie.atlassian.net/wiki" pageId = "13008897" # List all attachments for the given pageId (GET https://myconnie.atlassian.net/wiki/api/v2/pages/13008897/attachments) auth = HTTPBasicAuth(user_name, user_password) url = confluence_baseurl+"/api/v2/pages/"+pageId+"/attachments" headers = { "Accept": "application/json" } response = requests.request( "GET", url, headers=headers, auth=auth ) attach = json.loads(response.text) for x in attach["results"]: print (x['id'],",",x['title'],",",x['status'],",",x['version']['number'],",",x['version']['createdAt'])

These are all the attachments from the given page. We will use the date value as a condition to delete attachments.

1 2 3 4 5 6 python3 getPageAttachments.py warnings.warn( att13008914 , Screenshot 2023-10-27 at 20.19.09.png , current , 1 , 2024-01-15T12:57:54.721Z att13041683 , Screenshot 2023-10-27 at 18.08.18.png , current , 1 , 2024-01-15T12:57:55.347Z att13074433 , Screenshot 2023-10-27 at 20.11.44.png , current , 1 , 2024-01-15T12:57:54.743Z

3. Delete attachments older than a provided date

For this sample, we decided to delete all attachments older than "2024-01-15T12:57:55.347Z" (not inclusive), thus we will delete 2 attachments. The timestamp is expressed in UTC (Coordinated Universal Time), with a special UTC designator ("Z").

The first part of the code gets all the attachments for a given page with ID.

The second part of the code will do a for loop for each attachment. The condition will identify the attachments with the status "current" and created before a given date "2024-01-15T13:00:00.001Z" and delete them:

The expected HTTP status for this request is 204.

delPageAttachmentsOlderThanDate.py

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 # This code sample uses the 'requests' library: # http://docs.python-requests.org import requests from requests.auth import HTTPBasicAuth import json # Define the following variables user_name = "joedoe@atlassian.com" user_password = "ATATT3QWUUOIUQWO.....14FED" confluence_baseurl = "https://myconnie.atlassian.net/wiki" pageId = "13008897" createdAt = "2024-01-15T12:57:55.347Z" # List all attachments for the given pageId (GET https://myconnie.atlassian.net/wiki/api/v2/pages/13008897/attachments) auth = HTTPBasicAuth(user_name, user_password) url = confluence_baseurl+"/api/v2/pages/"+pageId+"/attachments" headers = { "Accept": "application/json" } response = requests.request( "GET", url, headers=headers, auth=auth ) attach = json.loads(response.text) # Moving attachments created before createdAt date provided to the trash can # The administrator can recover or purge then on the space settings > Manage Contents - trash for x in attach["results"]: if x['status'] == 'current' and x['version']['createdAt'] < createdAt: print (x['id'],",",x['title'],",",x['status'],",",x['version']['number'],",",x['version']['createdAt'])   url = confluence_baseurl+"/api/v2/attachments/" + x['id'] print (url) # (DELETE https://myconnie.atlassian.net/wiki/api/v2/attachments/<the attachment id>)         response = requests.request( "DELETE", url, headers=headers, auth=auth ) if response.status_code == 204: print (x['id'] + " deleted") else:         print("Failed to delete attachment ", x['id'], "Status code: ", response.status_code)        print(response.text)

Sample output:

1 2 3 4 5 6 7 8 python3 delPageAttachmentsOlderThanDate.py warnings.warn( att13008914 , Screenshot 2023-10-27 at 20.19.09.png , current , 1 , 2024-01-15T12:57:54.721Z https://myconnie.atlassian.net/wiki/api/v2/attachments/att13008914 att13008914 deleted att13074433 , Screenshot 2023-10-27 at 20.11.44.png , current , 1 , 2024-01-15T12:57:54.743Z https://myconnie.atlassian.net/wiki/api/v2/attachments/att13074433 att13074433 deleted

4. The DELETE operation will change the attachment status from CURRENT to TRASHED.

We can confirm this action checking the Space Settings > Trash or using a REST/API.

The sample below retrieves the deleted attachment att13074433:

getAttachmentById.py

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 # This code sample uses the 'requests' library: # http://docs.python-requests.org import requests from requests.auth import HTTPBasicAuth import json # Define the following variables user_name = "joedoe@atlassian.com" user_password = "ATATT3QWUUOIUQWO.....14FED" confluence_baseurl = "https://myconnie.atlassian.net/wiki" attachId = "att13074433" # List all attachments for the given attachment Id (GET https://myconnie.atlassian.net/wiki/api/v2/attachments/att13074433) auth = HTTPBasicAuth(user_name, user_password) url = confluence_baseurl+"/api/v2/attachments/"+ attachId headers = { "Accept": "application/json" } response = requests.request( "GET", url, headers=headers, auth=auth ) print(json.dumps(json.loads(response.text), sort_keys=True, indent=4, separators=(",", ": ")))

Sample output:

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 python3 getAttachmentByID.py warnings.warn( { "_links": { "download": "/download/attachments/13008897/Screenshot%202023-10-27%20at%2020.11.44.png?version=1&modificationDate=1705323474743&cacheVersion=1&api=v2", "webui": "/pages/viewpageattachments.action?pageId=13008897&preview=%2F13008897%2F13074433%2FScreenshot+2023-10-27+at+20.11.44.png" }, "comment": "", "createdAt": "2024-01-15T12:57:54.743Z", "downloadLink": "/download/attachments/13008897/Screenshot%202023-10-27%20at%2020.11.44.png?version=1&modificationDate=1705323474743&cacheVersion=1&api=v2", "fileId": "7baba0fe-ad89-4de9-bcec-58d8e5640dbf", "fileSize": 1534004, "id": "att13074433", "mediaType": "image/png", "mediaTypeDescription": "PNG Image", "pageId": "13008897", "status": "trashed", "title": "Screenshot 2023-10-27 at 20.11.44.png", "version": { "authorId": "5d00b8b192e6c70c5349a042", "createdAt": "2024-01-15T12:57:54.743Z", "message": "", "minorEdit": true, "number": 1 }, "webuiLink": "/pages/viewpageattachments.action?pageId=13008897&preview=%2F13008897%2F13074433%2FScreenshot+2023-10-27+at+20.11.44.png" }

6. Purge attachment

This operation can be reverted from the UI by accessing the Space Trash. To permanently delete an attachment (or "purge" it), the endpoint must be called on a trashed attachment with the following parampurge=true. For the sample code bellow, we used one of the previous deleted attachments identified with the Attachment id att13074433:

delAttachmentFromTrash.py

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 # This code sample uses the 'requests' library: # http://docs.python-requests.org import requests from requests.auth import HTTPBasicAuth import json # Define the following variables   user_name = "joedoe@atlassian.com" user_password = "ATATT3QWUUOIUQWO.....14FED" confluence_baseurl = "https://myconnie.atlassian.net/wiki" attachId = "att13074433" # Purge the attachment from the trash can (DELETE https://myconnie.atlassian.net/wiki/api/v2/attachments/att13074433?purge=true) auth = HTTPBasicAuth(user_name, user_password) url = confluence_baseurl+"/api/v2/attachments/"+attachId+"?purge=true" headers = { "Accept": "application/json" } response = requests.request( "DELETE", url, headers=headers, auth=auth ) if response.status_code == 204: print (attachId + " deleted") else: print("Failed to purge attachment", attachId, "Status code: ", response.status_code) print(response.text)

Sample output

1 2 3 4 python3 delAttachmentFromTrash.py warnings.warn( att13074433 deleted
Updated on April 15, 2025

Still need help?

The Atlassian Community is here for you.