{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 参考|今回作成したモニタリングスケジュール、エンドポイント、エンドポイント構成を削除するコード\n",
    "与えられた文字列に名前の一部が一致するリソースを削除する  \n",
    "<span style=\"color: red; font-size: 140%;\">名前に一致するエンドポイント、エンドポイント構成を自動で削除するので注意して実行してください!!</span>"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [],
   "source": [
    "import boto3\n",
    "import time\n",
    "\n",
    "def delete_monitoring_schedule(target_substring):\n",
    "    if not target_substring:\n",
    "        print('Please specify deletion target sub-string')\n",
    "        return\n",
    "    \n",
    "    sm = boto3.client('sagemaker')\n",
    "    resp = sm.list_monitoring_schedules()\n",
    "    schedule_names = [x['MonitoringScheduleName'] for x in resp['MonitoringScheduleSummaries']]\n",
    "    target_schedule_names = [x for x in schedule_names if target_substring in x]\n",
    "    print('Deletion target schedules:', target_schedule_names)\n",
    "\n",
    "    for schedule_name in target_schedule_names:\n",
    "        print('  Deleting...', schedule_name )\n",
    "        sm.delete_monitoring_schedule(MonitoringScheduleName=schedule_name)\n",
    "        \n",
    "def delete_endpoint(target_substring):\n",
    "    if not target_substring:\n",
    "        print('Please specify deletion target sub-string')\n",
    "        return\n",
    "    \n",
    "    sm = boto3.client('sagemaker')\n",
    "    resp = sm.list_endpoints()\n",
    "    endpoint_names = [x['EndpointName'] for x in resp['Endpoints']]\n",
    "    target_endpoint_names = [x for x in endpoint_names if target_substring in x]\n",
    "    print('Deletion target endpoints:', target_endpoint_names)\n",
    "\n",
    "    for endpoint_name in target_endpoint_names:\n",
    "        print('  Deleting...', endpoint_name)\n",
    "        sm.delete_endpoint(EndpointName=endpoint_name)\n",
    "\n",
    "def delete_endpoint_config(target_substring):\n",
    "    if not target_substring:\n",
    "        print('Please specify deletion target sub-string')\n",
    "        return\n",
    "    \n",
    "    sm = boto3.client('sagemaker')\n",
    "    resp = sm.list_endpoint_configs()\n",
    "    endpoint_config_names = [x['EndpointConfigName'] for x in resp['EndpointConfigs']]\n",
    "    target_config_names = [x for x in endpoint_config_names if target_substring in x]\n",
    "    print('Deletion target endpoint configs', target_config_names)\n",
    "\n",
    "    for endpoint_config_name in target_config_names:\n",
    "        print('  Deleting...', endpoint_config_name)\n",
    "        sm.delete_endpoint_config(EndpointConfigName=endpoint_config_name)\n",
    "        \n",
    "def delete_endpoint_resources(target_substring):\n",
    "    delete_monitoring_schedule(target_substring)\n",
    "    time.sleep(3)\n",
    "    delete_endpoint(target_substring)\n",
    "    time.sleep(3)\n",
    "    delete_endpoint_config(target_substring)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Deletion target schedules: []\n",
      "Deletion target endpoints: []\n",
      "Deletion target endpoint configs []\n"
     ]
    }
   ],
   "source": [
    "# nyctaxi- を名前に含むモニタリングスケジュール、推論エンドポイント、エンドポイントConfigを順に削除する\n",
    "# スケジュールやエンドポイントの削除中に依存するオブジェクトを削除しようとするとエラーになるので、しばらく待って再実行する\n",
    "\n",
    "# 下のコメントアウトを外し、削除してもよいエンドポイント名等を指定して実行してください\n",
    "# delete_endpoint_resources('nyctaxi-')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "instance_type": "ml.t3.medium",
  "kernelspec": {
   "display_name": "Python 3 (Data Science)",
   "language": "python",
   "name": "python3__SAGEMAKER_INTERNAL__arn:aws:sagemaker:ap-northeast-1:102112518831:image/datascience-1.0"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.10"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}