Skip to main content

Purge trashed pages from several spaces

Permanently delete the trashed pages of the Confluence spaces you list, in one run.

Applies to: Script Master for Confluence Β· Cloud

A version of this script is in the Snippets library as the use case Permanently purge trashed pages from multiple Confluence spaces.

Before you begin​

  • Confluence permission: Confluence admin, to open the Script Console. You must also be a space admin of each space: purging a page needs that permission.
  • App permission: an active Script Master license.

Run the script​

  1. On the Script Console tab, paste the script below into the editor.

  2. Replace the keys in spaceKeys with the keys of the spaces to clean up.

  3. Select Run.

    warning

    Purged pages can't be restored, from Script Master or from the space's trash. Check the space keys before you run the script.

Result: Result shows an indented list with one entry per page: purged, or why it wasn't. When the spaces have no trashed pages, it says No changes made (no trashed pages found in the selected spaces).

// Settings
const spaceKeys = ['DEMO', 'ALPHA'];

// https://developer.atlassian.com/cloud/confluence/rest/v2/api-group-space/#api-spaces-get
const spacesResponse = await requestConfluence(`/wiki/api/v2/spaces?keys=${spaceKeys.join(',')}`, {
headers: { Accept: 'application/json' },
});
const spacesData = await spacesResponse.json();
if (!spacesData.results?.length) return 'Spaces not found';

const output = [];
for (const space of spacesData.results) {
// https://developer.atlassian.com/cloud/confluence/rest/v2/api-group-page/#api-pages-get
const pagesResponse = await requestConfluence(`/wiki/api/v2/pages?space-id=${space.id}&status=trashed&limit=250`, {
headers: { Accept: 'application/json' },
});
const pagesData = await pagesResponse.json();

for (const page of pagesData.results) {
// https://developer.atlassian.com/cloud/confluence/rest/v2/api-group-page/#api-pages-id-delete
const deleteResponse = await requestConfluence(`/wiki/api/v2/pages/${page.id}?purge=true`, {
method: 'DELETE',
});
if (deleteResponse.status === 204) {
output.push(`'${page.title}' (${page.id}) purged from ${space.key}`);
} else {
output.push(`'${page.title}' (${page.id}) not purged from ${space.key}: ${await deleteResponse.text()}`);
}
}
}

return output.length ? output : 'No changes made (no trashed pages found in the selected spaces)';

Each run handles up to 250 trashed pages per space. Run it again for spaces with more.