Cleanup Service

The example demonstrates a Task which executes in Trains Agent services model and deletes archived Tasks, along with the model snapshots, other artifacts, and debug samples associated with those Tasks. When the script runs, it creates an experiment named Cleanup Service which is associated with the DevOps project.

The example creates a Trains API client session to delete the Tasks. It creates an APIClient object which establishes a session with the Trains backend (Trains Server), and accomplishes the cleanup by calling:

  • Tasks.get_all to get a list of Tasks to delete, providing the following parameters:

    • system_tags - Get only Tasks tagged as archived.
    • only_fields - Get only the Task id. Only the Task id is needed to delete Tasks and its output.
    • order_by - Order the list of Tasks returned by last activity timestamp, in descending order (most recent first).
    • page_size - Set the number of Tasks returned in each page (the last page may contain fewer results).
    • page - Set the number of the page in the resulting list of Tasks to return.
    • status_changed - Get Tasks whose last status change is older than then delete threshold (in seconds).
  • Tasks.delete - Delete a Task, optionally forcing the deletion of a Task, even if its status is not Draft.

Running the cleanup service

  1. Trains Agent must be running in services mode and listening to the services queue, so that when the script calls the Task.execute_remotely method to enqueue the cleanup Task, Trains Agent can begin the service.

    For example:

    trains-agent daemon --services-mode --detached --queue services --create-queue --docker  --cpu-only

  2. Run the example script. If you use the default values of the arguments in the script, it will run as a service, run daily, delete Tasks in the Archive which have been inactive for thirty days or more, and delete only status Draft Tasks.


    The arguments in the script that you can change are:

    • delete_threshold_days - Tasks older than this number of days will be deleted. The default value is 30 days.
    • cleanup_period_in_days - Repeat the cleanup service at this interval, in days. The default value is to run once a day.
    • run_as_service - Run the cleanup as a service (it repeats regularly), or run it once locally. The default value is to run as a service.
    • force_delete - A boolean indicating whether to delete only status created (Draft) Tasks, or force the deletion of all Tasks. The default value is False, delete only status created (Draft) Tasks.