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 table from AWS Glue Data catalog?
AWS Glue Data Catalog stores metadata about your data sources, transformations, and targets. You can use boto3 to programmatically delete tables from the Glue catalog when they are no longer needed.
Problem Statement
Use boto3 library in Python to delete a table created in your AWS Glue Data Catalog account.
Example
Delete a table 'security' from database 'test' that exists in your AWS Glue catalog.
Prerequisites
Before running this code, ensure you have:
- AWS credentials configured (via AWS CLI, IAM role, or environment variables)
- Appropriate IAM permissions for Glue operations
- The target database and table exist in your Glue catalog
Implementation Steps
Step 1 − Import boto3 and botocore exceptions to handle errors gracefully.
Step 2 − Define the database name and table name parameters for the table to be deleted.
Step 3 − Create a boto3 session. Ensure your AWS region is configured in the default profile.
Step 4 − Create an AWS Glue client using the session.
Step 5 − Use the delete_table() function with DatabaseName and Name parameters.
Step 6 − Handle exceptions that may occur during the deletion process.
Complete Example
Here's the complete code to delete a table from AWS Glue Data Catalog ?
import boto3
from botocore.exceptions import ClientError
def delete_a_table_from_database(database_name, table_name):
"""
Delete a table from AWS Glue Data Catalog
Args:
database_name (str): Name of the database containing the table
table_name (str): Name of the table to delete
Returns:
dict: Response metadata from AWS
"""
session = boto3.session.Session()
glue_client = session.client('glue')
try:
response = glue_client.delete_table(
DatabaseName=database_name,
Name=table_name
)
return response
except ClientError as e:
raise Exception("boto3 client error in delete_a_table_from_database: " + e.__str__())
except Exception as e:
raise Exception("Unexpected error in delete_a_table_from_database: " + e.__str__())
# Example usage
print(delete_a_table_from_database("test", "security"))
Expected Output
When the table is successfully deleted, you'll receive a response with metadata ?
{
'ResponseMetadata': {
'RequestId': '7aa7c3cb-f611-494e-b923-8610318a238c',
'HTTPStatusCode': 200,
'HTTPHeaders': {
'date': 'Sun, 21 Feb 2021 04:58:04 GMT',
'content-type': 'application/x-amz-json-1.1',
'content-length': '2',
'connection': 'keep-alive',
'x-amzn-requestid': '7aa7c3cb-f611-494e-b923-8610318a238c'
},
'RetryAttempts': 0
}
}
Key Points
- The
delete_table()function permanently removes the table metadata from Glue catalog - This operation does not delete the actual data files stored in S3 or other locations
- A successful deletion returns HTTP status code 200
- Handle
ClientErrorexceptions for AWS-specific errors like table not found
Common Error Scenarios
The function may raise exceptions in these cases:
- EntityNotFoundException: Table or database doesn't exist
- AccessDeniedException: Insufficient IAM permissions
- InvalidInputException: Invalid database or table name format
Conclusion
Using boto3's delete_table() function provides a programmatic way to remove table metadata from AWS Glue Data Catalog. Remember that this only removes catalog entries, not the underlying data files.
