Create
cancel
Showing results for 
Search instead for 
Did you mean: 
Sign up Log in

Next challenges

Recent achievements

  • Global
  • Personal

Recognition

  • Give kudos
  • Received
  • Given

Leaderboard

  • Global

Trophy case

Kudos (beta program)

Kudos logo

You've been invited into the Kudos (beta program) private group. Chat with others in the program, or give feedback to Atlassian.

View group

It's not the same without you

Join the community to find out what other Atlassian users are discussing, debating and creating.

Atlassian Community Hero Image Collage

Delete all attachments

Hello,

Is there a way of deleting ALL attachments from a page?

I'm running 5.4.1, server

4 answers

Hi Mick,

If you load up the browser console (by pressing F12 or Ctrl/Cmd + Shift + i) and paste the following code in and press Enter, it will delete all attachments from the page you are currently viewing.

var deleteAttachments = function(attachments) {
    requestObject = [];
    requestObject.push(attachments[0].ownerId);
    requestObject.push(attachments[0].fileName);
    jQuery.ajax({
        contentType: 'application/json',
        type: 'POST',
        url: '/rpc/json-rpc/confluenceservice-v2/removeAttachment',
        data: JSON.stringify(requestObject),
        success: function( response ) {
            attachments.shift();
            if (attachments.length) {
                deleteAttachments(attachments);
            } else {
                console.log("Done!");
            }
        }
    });
}
var getAttachments = function(attachments, startIndex) {
    var attachments = attachments || [];
    var startIndex = startIndex || 0;
    jQuery.ajax({
        url: contextPath + "/rest/prototype/1/content/" + AJS.params.pageId + "/attachments.json?start-index=" + startIndex,
        success: function(response) {
            attachments.push.apply(attachments, response.attachment);
            if ( attachments.length < response.size ) {
                startIndex += 50;
                getAttachments(attachments, startIndex);
            } else {
                deleteAttachments(attachments);
            }
        }
    });
}
getAttachments();

This should work for most versions of Confluence, including 5.4.1.  It will probably even work for Confluence Cloud.

Note: This doesn't check if you have permission to delete the attachments or not.  If not, it won't work.

Stephen, great, thanks for this. I haven't tried it yet, but will get back to you as soon as I have done. Cheers.

For this script to work there is a need to fix escaping that was applied to '<' operator in the if statement

if ( attachments.length &lt; response.size ) {
                startIndex += 50;
                getAttachments(attachments, startIndex);
            } else {
                deleteAttachments(attachments);
            }

Expected

if ( attachments.length < response.size ) {
startIndex += 50;
getAttachments(attachments, startIndex);
} else {
deleteAttachments(attachments);

Thanks @AndrewChuk & @Stephen Deutsch - with the above script this successfully deleted ~60 attachments on Confluence Server 6.13 - saved me having to remove them manually!

2 votes
Rodrigo Rosa Atlassian Team Sep 25, 2015

Hello Mick,

There is a feature request to add this functionality to Confluence here. I believe there are some workarounds in the comments there you could try. :smile:

2 votes
Giuliano C_ Atlassian Team Sep 25, 2015

In addition to the information above: The Bob Swift's CLI add-on can help to remove a selection of attachments: https://bobswift.atlassian.net/wiki/display/CSOAP/How+to+remove+a+selection+of+attachments

 

var headers = {
'Authorization': 'Basic <base64credential_Y2hldjoyOUtsdWJuaWthJg>',
'X-Atlassian-Token': 'nocheck'
};
var apiRoot = 'https://confluence.host.net/rest/api';

// page
var containerId = '171607042'

fetch(`${apiRoot}/content/${containerId}/child/attachment`, {headers})
.then(r => r.json())
.then(o => Promise.all(o.results.map(i=>fetch(i._links.self, {method: "DELETE", headers}))))
.then(r => console.log('r', r));

Suggest an answer

Log in or Sign up to answer
TAGS
Community showcase
Posted in Confluence Cloud

Please tell us how you use Confluence space settings

👋 Hi there, a few of us at Atlassian would love to learn about how you use "space settings" functionality in Confluence. A facelift to the space settings is long overdue and we want to start with im...

126 views 5 6
Join discussion

Community Events

Connect with like-minded Atlassian users at free events near you!

Find an event

Connect with like-minded Atlassian users at free events near you!

Unfortunately there are no Community Events near you at the moment.

Host an event

You're one step closer to meeting fellow Atlassian users at your local event. Learn more about Community Events

Events near you