def purge_trigger_instances(logger, timestamp):
    """
    :param timestamp: Trigger instances older than this timestamp will be deleted.
    :type timestamp: ``datetime.datetime
    """
    if not timestamp:
        raise ValueError("Specify a valid timestamp to purge.")

    logger.info(
        "Purging trigger instances older than timestamp: %s"
        % timestamp.strftime("%Y-%m-%dT%H:%M:%S.%fZ")
    )

    query_filters = {"occurrence_time__lt": isotime.parse(timestamp)}

    try:
        deleted_count = TriggerInstance.delete_by_query(**query_filters)
    except InvalidQueryError as e:
        msg = (
            "Bad query (%s) used to delete trigger instances: %s"
            "Please contact support."
            % (
                query_filters,
                six.text_type(e),
            )
        )
        raise InvalidQueryError(msg)
    except:
        logger.exception(
            "Deleting instances using query_filters %s failed.", query_filters
        )
    else:
        logger.info("Deleted %s trigger instance objects" % (deleted_count))

    # Print stats
    logger.info(
        "All trigger instance models older than timestamp %s were deleted.", timestamp
    )

    :param timestamp: Trigger instances older than this timestamp will be deleted.
    :type timestamp: ``datetime.datetime
    
st2common/st2common/garbage_collection/trigger_instances.py