Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
How to use Boto3 to delete a database from AWS Data Catalog?
Boto3 is the AWS SDK for Python that allows you to interact with AWS services. The AWS Glue Data Catalog stores metadata about databases and tables. You can use boto3 to delete a database from the Data Catalog programmatically.
Prerequisites
Before deleting a database, ensure you have:
- AWS credentials configured (AWS CLI, environment variables, or IAM role)
- Appropriate IAM permissions for AWS Glue operations
- The boto3 library installed:
pip install boto3
Approach
Step 1 − Import boto3 and botocore exceptions to handle errors.
Step 2 − Create an AWS Glue client using boto3.
Step 3 − Use the delete_database() method with the database name.
Step 4 − Handle exceptions that may occur during deletion.
Example
The following code demonstrates how to delete a database named 'Portfolio' 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', 'content-
length': '2', 'connection': 'keep-alive', 'x-amzn-requestid': '067b667f-
0a10-4f99-91be-0a74d4f30a5b'}, 'RetryAttempts': 0}}
Key Points
- The
delete_database()method requires only the database name as theNameparameter - Successful deletion returns a response with HTTP status code 200
- All tables, partitions, and user-defined functions in the database are also deleted
- AWS Glue deletes orphaned resources asynchronously after the operation completes
Error Handling
Common exceptions you might encounter:
- EntityNotFoundException − The specified database doesn't exist
- InvalidInputException − Invalid database name format
- InternalServiceException − AWS service error
Conclusion
Use boto3's delete_database() method to remove databases from AWS Glue Data Catalog. Always implement proper error handling since this operation permanently deletes the database and all associated resources.
