Confluence: How to programmatically clean versions of page with python for Confluence?

 Hi Atlassian community

It is one of the steps  from my routine work for Confluence administration for internal and customer facings installations as well. 

Also, previous articles you can find here 12.  

For continuous cleanup,  I use atlassian-python-api and Atlassian Bamboo. Also, it is very easy to automate it and extend functionality.

My use case was cleaning after a lot of programmatically created and updated pages after long time using. e.g.

Снимок экрана 2018-11-03 в 12.59.57.png

After cleanup, your page load metrics will be better, also, disk usage of Lucene indexes and DB usage.

 

Well, let's start. 

1. Remove page version based on 2 different methods for Cloud and on-Premises releases:

    def remove_content_history(self, page_id, version_number):
        """
        Remove content history. It works as experimental method
        :param page_id:
        :param version_number: version number
        :return:
        """
        url = "rest/experimental/content/{id}/version/{versionNumber}".format(id=page_id, versionNumber=version_number)
        self.delete(url)

    def remove_content_history_in_cloud(self, page_id, version_id):
        """
        Remove content history. It works in CLOUD
        :param page_id:
        :param version_id:
        :return:
        """
        url = "rest/api/content/{id}/version/{versionId}".format(id=page_id, versionId=version_id)
        self.delete(url)

2. After that we need to define the method which work with page_id and also, how many page versions we need remained.

def page_version_remover(server, content_id, remained_page_numbers):
    response = server.get_content_history(content_id)
    if not response.get('latest'):
        return
    latest_version_count = int(response.get('lastUpdated').get('number'))
    if len(response) > 0 and latest_version_count > remained_page_numbers:
        print("Number of {} latest version {}".format(
            confluence.url_joiner(confluence.url, "/pages/viewpage.action?pageId=" + content_id), latest_version_count))
        for version_page_counter in range(1, (latest_version_count - remained_page_numbers + 1), 1):
            server.remove_content_history(content_id, 1)
    else:
        print('Number of page history smaller than remained')

3. Of course, we need to loop it, after fetch all page ids from space:

def get_all_page_ids_from_space(confluence, space_key):
    """
    :param confluence:
    :param space_key:
    :return:
    """
    limit = 500
    flag = True
    step = 0
    page_ids = []

    while flag:
        values = confluence.get_all_pages_from_space(space=space_key, start=limit * step, limit=limit)
        step += 1
        if len(values) == 0:
            flag = False
            print("Did not find any pages, please, check permissions")
        else:
            for value in values:
                print("Retrieve page with title: " + value['title'])
                page_ids.append((value['id']))
    print("Found in space {} pages {}".format(space_key, len(page_ids)))
    return page_ids

def reduce_page_numbers(confluence, page_id, remained_page_history_count): page_version_remover(confluence, page_id, remained_page_history_count) return

page_ids = get_all_page_ids_from_space(confluence, space_key)
for page_id in page_ids:
reduce_page_numbers(confluence, page_id=page_id, remained_page_history_count=remained_count)

That's all and you can put into repo and trigger you plan in the Bamboo. 

Full version you can find here: full example

P.S. The next time I will provide a few advice based on community and on my experience for automatically page generating. I hope you will not meet like these problems on screenshot :)

MicrosoftTeams-image.png

 

Cheers,

Gonchik Tsymzhitov

Comments

Popular posts from this blog

How only 2 parameters of PostgreSQL reduced anomaly of Jira Data Center nodes

Atlassian Community, let's collaborate and provide stats to vendors about our SQL index usage

Stories about detecting Atlassian Confluence bottlenecks with APM tool [part 1]