How to use Boto3 to delete a specific version of table from AWS Glue Data catalog?

Problem Statement − Use boto3 library in Python to delete a table of specific version, created in your account.

Example − Delete a table 'security' version 1 from database 'test' 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, table_name and version_id 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_table_version function and pass the database_name as DatabaseName parameter, table_name as TableName parameter and version_id as VersionId parameter. Please keep remember, VersionId is string so it should be passed in quotes and user can't delete latest version of a table. Only the old version of a table can be deleted if user wants to delete latest version then drop the table using function delete_table.

Step 6 − It will delete the old specific version of table and return the response metadata.

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

Example

Use the following code to delete a specific version of table from AWS Glue database −

import boto3
from botocore.exceptions import ClientError

def delete_a_table_version_from_database(database_name, table_name, version_id: str):
    session = boto3.session.Session()
    glue_client = session.client('glue')
    try:
        response = glue_client.delete_table_version(
            DatabaseName=database_name, 
            Name=table_name, 
            VersionId=version_id
        )
        return response
    except ClientError as e:
        raise Exception("boto3 client error in delete_a_table_version_from_database: " + e.__str__())
    except Exception as e:
        raise Exception("Unexpected error in delete_a_table_version_from_database: " + e.__str__())

# Example usage
print(delete_a_table_version_from_database("test", "security", "1"))

Output

{'ResponseMetadata': {'RequestId': '7aa7c3cb-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-
494e-b923-8610318a238c'}, 'RetryAttempts': 0}}

Key Points

  • The VersionId parameter must be passed as a string
  • You cannot delete the latest version of a table using this method
  • To delete the latest version, use the delete_table function instead
  • The function returns response metadata upon successful deletion

Conclusion

Using boto3's delete_table_version method allows you to remove specific old versions of tables from AWS Glue Data Catalog. Remember that only previous versions can be deleted, not the current active version.

Updated on: 2026-03-25T18:15:42+05:30

738 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements