EducationSoftwareStrategy.com
StrategyCommunity

Knowledge Base

Product

Community

Knowledge Base

TopicsBrowse ArticlesDeveloper Zone

Product

Download SoftwareProduct DocumentationSecurity Hub

Education

Tutorial VideosSolution GalleryEducation courses

Community

GuidelinesGrandmastersEvents
x_social-icon_white.svglinkedin_social-icon_white.svg
Strategy logoCommunity

© Strategy Inc. All Rights Reserved.

LegalTerms of UsePrivacy Policy
  1. Home
  2. Topics

KB486764: How to Automatically Delete Inactive Cube Caches in MicroStrategy WorkStation with a Python Script


Rainiero Leon

Support Engineer • MicroStrategy


The article outlines a method for deleting inactive cube caches in MicroStrategy WorkStation that have not been used for 30 days. It employs a Python script to streamline the process, improving efficiency and reducing the need for manual intervention.

Symptom


Over time, the accumulation of inactive cube caches could cause clutter and a possible slowness in the system. These inactive caches need to be periodically removed in order to maintain an optimal environment. Manually carrying out this operation is laborious and prone to mistakes.

Cause


The root cause might be the presence of inactive cube caches in a given environment. These caches become irrelevant and less useful over time, requiring routine cleanups to maintain optimal system performance and resources.

Solution

To seamlessly automate the deletion of inactive cube caches, follow these steps in the Strategy WorkStation:
1. Connect to Your Environment: Initialize the connection to your Strategy environment.
2. Navigate to Administration: Access the administration panel to manage scripts and workflows.
3. Access Scripts Section: Go to the scripts section where you can create or modify scripts.
4. Select Your Environment: Choose the appropriate environment where the script will be executed.
5. Open Gallery Tab: In the right section of the interface, select the Gallery tab to view available workflows and scripts.
6. Expand Administration Workflows: Find and expand the Administration Workflows section to access various administrative scripts.
7. Script Activation: Locate and double-click the 'delete inactive cube caches' script to access its details.
8. Script Execution: Commence the script by clicking the run button, thereby initiating the process of automatically deleting inactive cube caches.

ka0PW0000002C9xYAE_0EMPW000006Ycc6.jpeg

Below is the Python script in WorkStation that enables this operation:


"""Delete inactive caches which were not used for 30 days. IDs of deleted caches
will be printed."""

from datetime import datetime, timezone
from typing import List, Union

from mstrio.connection import Connection, get_connection
from mstrio.project_objects import CubeCache, list_cube_caches

def _get_datetime(date):
    """String to date time conversion"""
    return datetime.strptime(date, '%Y-%m-%dT%H:%M:%S.000%z')

def delete_inactive_caches(
    connection: "Connection", days_diff: str = 30, nodes: Union[str, List[str]] = None
) -> List["CubeCache"]:
    """Delete inactive caches which have `hit_count` equals 0 and their
    `last_time_updated` was earlier than `days_diff` before.

    Args:
        connection: Strategy connection object returned by
            `connection.Connection()`
        days_diff (int, optional): number of days to determine whether to delete
            cache when its `hit_count` equals 0. Default value is 30.
        nodes (list of strings or string, optional): names of nodes from which
            caches will be deleted. By default, it equals `None` and in that
            case all nodes' names are loaded from the cluster.

    Return:
        list with cache objects which were deleted
    """
    connection._validate_project_selected()
    caches = list_cube_caches(connection, nodes)
    # delete caches which fulfill requirements to be treated as inactive
    deleted_caches = []
    for cache in caches:
        today = datetime.now(timezone.utc)
        if (cache.hit_count == 0
                and (today - _get_datetime(cache.last_update_time)).days > days_diff):
            cache.delete(force=True)
            deleted_caches.append(cache)

    return deleted_caches

# connect to environment without providing user credentials
# variable `workstationData` is stored within Workstation
conn = get_connection(workstationData, 'Strategy Tutorial')

# execute deletion of inactive cube caches
deleted_caches = delete_inactive_caches(conn)

print('IDs of deleted caches')
for deleted_cache in deleted_caches:
    print(deleted_cache.id)

Related articles

Python within Workstation https://www2.microstrategy.com/producthelp/Current/Workstation/en-us/Content/intro_scripts.htm
Preview Python Scripts https://www2.microstrategy.com/producthelp/Current/Workstation/en-us/Content/preview_scripts.htm
mstrio-py https://github.com/MicroStrategy/mstrio-py
https://www2.microstrategy.com/producthelp/Current/Workstation/en-us/Content/intro_scripts.htm

Customization Warning


This customization is provided as a convenience to Strategy users and is only directly applicable to the version stated. While this code may apply to other releases directly, Strategy Technical Support makes no guarantees that the code provided will apply to any future or previous builds. In the event of a code change in future builds, Strategy Technical Support makes no guarantee that an updated version of this particular customization will be provided. For enhancements to this customization or to incorporate similar functionality into other versions, contact your Account Executive to inquire about Strategy Consulting assistance.


Comment

0 comments

Details

Knowledge Article

Published:

July 18, 2024

Last Updated:

July 18, 2024