<p><strong>Problem Statement</strong> &minus; Use boto3 library in Python to delete a database, created in your account.</p><p><strong>Example</strong> &minus; Delete a database &lsquo;Portfolio&rsquo; that is created in your account.</p><h2>Approach/Algorithm to solve this problem</h2><p><strong>Step 1</strong> &minus; Import boto3 and botocore exceptions to handle exceptions.</p><p><strong>Step 2</strong> &minus; Pass the parameter <strong>database_name</strong> that should be deleted from AWS Glue Catalog.</p><p><strong>Step 3</strong> &minus; Create an AWS session using boto3 library. Make sure region_name is mentioned in default profile. If it is not mentioned, then explicitly pass the region_name while creating the session.</p><p><strong>Step 4</strong> &minus; Create an AWS client for glue.</p><p><strong>Step 5</strong> &minus; Now use delete_database function and pass the database_name as Name parameter.</p><p><strong>Step 6</strong> &minus; It will delete the database and return the response metadata.</p><p><strong>Step 7</strong> &minus; Handle the generic exception if something went wrong while checking the job.</p><p><strong>Note</strong><strong>:</strong> After completion of this operation, user won&rsquo;t be able to access to the tables (and all table versions and partitions that belongs to the tables) and the user-defined functions (stored procedures) in the deleted database. AWS Glue deletes these &quot;orphaned&quot; resources asynchronously in a timely manner, at the discretion of the service.</p><h2>Example</h2><p>Use the following code to delete a database from AWS Glue Data Catalog &minus;</p><!--<p><a href="" target="_blank" rel="nofollow" class="demo"><i class="fa-external-link"></i> Live Demo</a></p>--><pre class="prettyprint notranslate">import boto3 from botocore.exceptions import ClientError def delete_a_database(database_name): &nbsp; &nbsp;session = boto3.session.Session() &nbsp; &nbsp;glue_client = session.client(&#39;glue&#39;) &nbsp; &nbsp;try: &nbsp; &nbsp; &nbsp; response = glue_client.delete_database(Name=database_name) &nbsp; &nbsp;return response &nbsp; &nbsp;except ClientError as e: &nbsp; &nbsp; &nbsp; raise Exception( &quot;boto3 client error in delete_a_database: &quot; + e.__str__()) &nbsp; &nbsp;except Exception as e: &nbsp; &nbsp; &nbsp; raise Exception( &quot;Unexpected error in delete_a_database: &quot; + e.__str__()) print(delete_a_database(&quot;Portfolio&quot;))</pre><h2>Output</h2><pre class="result notranslate">{&#39;ResponseMetadata&#39;: {&#39;RequestId&#39;: &#39;067b667f-0a74d4f30a5b&#39;, &#39;HTTPStatusCode&#39;: 200, &#39;HTTPHeaders&#39;: {&#39;date&#39;: &#39;Sat, 27 Feb 2021 14:54:30 GMT&#39;, &#39;content-type&#39;: &#39;application/x-amz-json-1.1&#39;, &#39;contentlength&#39;: &#39;2&#39;, &#39;connection&#39;: &#39;keep-alive&#39;, &#39;x-amzn-requestid&#39;: &#39;067b667f0a10-4f99-91be-0a74d4f30a5b&#39;}, &#39;RetryAttempts&#39;: 0}}</pre> Delete Database from AWS Data Catalog using Boto3

Delete Database from AWS Data Catalog using Boto3



Problem Statement − Use boto3 library in Python to delete a database, created in your account.

Example − Delete a database ‘Portfolio’ that is created in your account.

Approach/Algorithm to solve this problem

Step 1 − Import boto3 and botocore exceptions to handle exceptions.

Step 2 − Pass the parameter database_name that should be deleted from AWS Glue Catalog.

Step 3 − Create an AWS session using boto3 library. Make sure region_name is mentioned in default profile. If it is not mentioned, then explicitly pass the region_name while creating the session.

Step 4 − Create an AWS client for glue.

Step 5 − Now use delete_database function and pass the database_name as Name parameter.

Step 6 − It will delete the database and return the response metadata.

Step 7 − Handle the generic exception if something went wrong while checking the job.

Note: After completion of this operation, user won’t be able to access to the tables (and all table versions and partitions that belongs to the tables) and the user-defined functions (stored procedures) in the deleted database. AWS Glue deletes these "orphaned" resources asynchronously in a timely manner, at the discretion of the service.

Example

Use the following code to delete a database from AWS Glue Data Catalog −

import boto3
from botocore.exceptions import ClientError

def delete_a_database(database_name):
   session = boto3.session.Session()
   glue_client = session.client('glue')
   try:
      response = glue_client.delete_database(Name=database_name)
   return response
   except ClientError as e:
      raise Exception( "boto3 client error in delete_a_database: " + e.__str__())
   except Exception as e:
      raise Exception( "Unexpected error in delete_a_database: " + e.__str__())

print(delete_a_database("Portfolio"))

Output

{'ResponseMetadata': {'RequestId': '067b667f-0a74d4f30a5b',
'HTTPStatusCode': 200, 'HTTPHeaders': {'date': 'Sat, 27 Feb 2021
14:54:30 GMT', 'content-type': 'application/x-amz-json-1.1', 'contentlength': '2', 'connection': 'keep-alive', 'x-amzn-requestid': '067b667f0a10-4f99-91be-0a74d4f30a5b'}, 'RetryAttempts': 0}}
Updated on: 2021-03-22T08:55:53+05:30

400 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements