How to Remove all Previous Versions of a Page Manually in the Database Using SQL Commands

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

Problem

  • Due to the Bug  CONF-29166 - Getting issue details... STATUS  (Unable to delete blog version), Currently there is no way to delete the old version of a blog page through UI.
  • You would like to bulk remove previous versions of a page

If you are unsure how to deal with the database, contact your DBA. Make sure to have the database backed up completely before going further. These SQL commands were tested in some environments and they worked as intended, however it might not work in specific cases and newer versions of confluence as new constraints as changes may be done to confluence database structure. As such, a database backup is mandatory in case something goes wrong and you need to revert.

Workaround

Please note this queries will only work when:

  • The title of the page has not changed across versions.
  • There is no other page with the same title in your Confluence instance.

The process to remove a page's history is

  1. First, stop Confluence.
  2. Create a full database backup prior to running these queries against your database.
  3. You must get the page name you would like to remove. Simply copy the page name exactly the same way it is written.

  4. Obtain the number of the current version of the page/blog post. This is the version that you would like to save. This is obtainable by going to the Page/Blog post > Tools > Page History > Take note of the "current" version
  5. Run the following queries in the database, in order, since the database contains constraints that will prevent foreign keys and primary keys to be violated if you try to remove one which is mentioned in another table. Replace the <page name> with the actual page name you have copied before:

    Always back up your data before performing any modifications to the database. If possible, test any alter, insert, update, or delete SQL commands on a staging server first.


    (info) The following SQL has not been tested in Confluence 5.7 and above

    create table TEMP_CONTENT like content;
    
    insert into TEMP_CONTENT select * from content where title = '<page name>' and version != <current version number>;
    
    delete from links where contentid in (select tc.CONTENTID from TEMP_CONTENT tc);
    
    delete from imagedetails where attachmentid in(select attachmentid from attachments where pageid in (select tc.CONTENTID from TEMP_CONTENT tc));
    
    delete from attachments where pageid in (select tc.CONTENTID from TEMP_CONTENT tc);
    
    delete from notifications where contentid in (select tc.CONTENTID from TEMP_CONTENT tc);
    
    delete from confancestors where descendentid in (select tc.CONTENTID from TEMP_CONTENT tc);
    
    delete from contentproperties where contentid in (select tc.CONTENTID from TEMP_CONTENT tc);
    
    delete from usercontent_relation where targetcontentid in (select tc.CONTENTID from TEMP_CONTENT tc);
    
    delete from bodycontent where contentid in (select tc.CONTENTID from TEMP_CONTENT tc);
    
    delete from content where contentid in (select tc.CONTENTID from TEMP_CONTENT tc);
    
    drop table TEMP_CONTENT;
  6. Update this current version to become version 1

    update content set version = 1 where title = '<page name>'
  7. Lastly, check if the operation is complete from the following SQL query:

    select * from content where title = '<page name>'

    * This should return with one value, with version = 1

  8. Rebuild the indexes from scratch for server or Rebuild the indexes from scratch for DC
  9. Restart Confluence for the changes to take effect.

Example

For example, Below is a situation where a blog with the name "Test 584 Blog" with 5 versions, and you would like to delete all 4 versions before version 5:

Therefore, replace <page name> with Test 584 Blog and replace <current version number> with 5 in the SQL query given in the "Workaround" section




Last modified on Jul 7, 2023

Was this helpful?

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