<jupyter_start><jupyter_text>CleanupAfter building completing the notebooks you may want to delete the following to prevent any unwanted charges:* Forecast Exports* Forecasts* Predictors* Datasets* Dataset GroupsThe code snippets below will cover the base use case of creating items in notebooks 1 - 3. You can expand upon this to delete content created in other notebooks. Imports and Connecting to AWSThe following lines import all the necessary libraries and then connect you to Amazon Forecast.<jupyter_code>import sys
import os
import json
import time
import boto3
import pandas as pd
import botocore.exceptions<jupyter_output><empty_output><jupyter_text>The line below will retrieve your shared variables from the earlier notebooks.<jupyter_code>%store -r<jupyter_output><empty_output><jupyter_text>Once again connect to the Forecast APIs via the SDK.<jupyter_code>session = boto3.Session(region_name=region) 
forecast = session.client(service_name='forecast') 
forecastquery = session.client(service_name='forecastquery')<jupyter_output><empty_output><jupyter_text>Defining the Things to CleanupIn the previous notebooks you stored several variables at the end of each, now that they have been retrieved above, the cells below will delete the items that were created one at a time until all items that were created have been removed.<jupyter_code>def wait_till_delete(callback, check_time = 5, timeout = None):

    elapsed_time = 0
    while timeout is None or elapsed_time < timeout:
        try:
            out = callback()
        except botocore.exceptions.ClientError as e:
            # When given the resource not found exception, deletion has occured
            if e.response['Error']['Code'] == 'ResourceNotFoundException':
                print('Successful delete')
                return
            else:
                raise
        time.sleep(check_time)  # units of seconds
        elapsed_time += check_time

    raise TimeoutError( "Forecast resource deletion timed-out." )
# Delete the Exports
exports = forecast.list_forecast_export_jobs()
exports = [armia_export_arn, prophet_export_arn, deep_ar_export_arn]
for job in exports:
    forecast.delete_forecast_export_job(ForecastExportJobArn=job)
# Delete the Foreacst:
wait_till_delete(lambda: forecast.delete_forecast(ForecastArn=prophet_forecast_arn))
wait_till_delete(lambda: forecast.delete_forecast(ForecastArn=arima_forecast_arn))
wait_till_delete(lambda: forecast.delete_forecast(ForecastArn=deeparp_forecast_arn))
# Delete the Predictor:
wait_till_delete(lambda: forecast.delete_predictor(PredictorArn=arima_arn))
wait_till_delete(lambda: forecast.delete_predictor(PredictorArn=prophet_arn))
wait_till_delete(lambda: forecast.delete_predictor(PredictorArn=deeparp_arn))
# Delete Import
wait_till_delete(lambda: forecast.delete_dataset_import_job(DatasetImportJobArn=ds_import_job_arn))
# Delete the Dataset:
wait_till_delete(lambda: forecast.delete_dataset(DatasetArn=target_datasetArn))
# Delete the DatasetGroup:
wait_till_delete(lambda: forecast.delete_dataset_group(DatasetGroupArn=datasetGroupArn))
# Delete your file in S3
s3 = boto3.resource('s3')
bucket = s3.Bucket(bucket_name)
bucket.objects.all().delete()
# Delete the bucket
bucket.delete()<jupyter_output><empty_output><jupyter_text>IAM Policy CleanupThe very last step in the notebooks is to remove the policies that were attached to a role and then to delete it. No changes should need to be made here, just execute the cell.<jupyter_code># IAM policies should also be removed
iam = boto3.client("iam")

iam.delete_role(RoleName=role_name)<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.0695940347970174, 'fraction_numerical': 0.0033140016570008283, 'mean_word_length': 5.833962264150943, 'pattern_counts': {'":': 0, '<': 22, '<?xml version=': 0, '>': 21, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '5', 'dir': 'jupyter-structured-clean-dedup', 'id': '133471', 'license': 'MIT-0', 'n_tokens_mistral': 1080, 'n_tokens_neox': 1041, 'n_words': 314, 'path': 'workshops/immersion_day/notebooks/Cleanup.ipynb', 'repo_name': 'sengstacken/amazon-forecast-samples', 'size': '3621'}