How to delete an attachment from a Confluence page using the REST API

Still need help?

The Atlassian Community is here for you.

Ask the community


Platform notice: Server and Data Center only. This article only applies to Atlassian products on the Server and Data Center platforms.

Support for Server* products ended on February 15th 2024. If you are running a Server product, you can visit the Atlassian Server end of support announcement to review your migration options.

*Except Fisheye and Crucible

Summary

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 delete an attachment, provided you have the attachment ID.

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

This suggestion is for Confluence Server and Data Center. Using Confluence Cloud? See the similar article How to delete attachments filtered by date 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 test this in a lower environment to understand the consequences of it before applying your solution in a production environment.


  1. Set up environment variables as suggested below.

    USER_NAME=<username> ### You must be either a Space Administrator or a Confluence Administrator since we will purge the trash
    USER_PASSWORD=<user password>
    CONFLUENCE_BASEURL=<Confluence Base URL> ### FQDN and context path without the trailing slash
    ATTACHMENT_ID=<attachment ID>



  2. Send the attachment to the Space Trash.
    The DELETE operation will change the attachment status from CURRENT to TRASHED.
    The expected HTTP status for this request is 204.
    This operation can be reverted from the UI by accessing the Space Trash.

    curl  -u ${USER_NAME}:${USER_PASSWORD} -H 'Content-Type: application/json' -H 'Accept: application/json' -X DELETE ${CONFLUENCE_BASEURL}'/rest/api/content/'${ATTACHMENT_ID}'?status=current'



  3. Purge the attachment from the trash.
    This is the operation that will delete the attachment metadata from the Confluence database and the actual file(s) from the file system.
    The expected HTTP status for this request is 204.
    This operation cannot be reverted.

    curl -v -u ${USER_NAME}:${USER_PASSWORD} -H 'Content-Type: application/json' -H 'Accept: application/json' -X DELETE ${CONFLUENCE_BASEURL}'/rest/api/content/'${ATTACHMENT_ID}'?status=trashed' >/dev/null

With the steps above we provided an example of how to completely delete all versions of an attachment.

With this, you may be able to create a script in your preferred coding language to delete a set of attachments.


See Also

Confluence Server REST API

Confluence REST API Documentation



Last modified on Jan 18, 2024

Was this helpful?

Yes
No
Provide feedback about this article
Powered by Confluence and Scroll Viewport.