Python MySQL Drop Table

Delete Table

You can use the "DROP TABLE" statement to delete an existing table:

Example

Delete "customers" Table:

import mysql.connector
mydb = mysql.connector.connect(
  host="localhost",
  user="yourusername",
  passwd="yourpassword",
  database="mydatabase"
)
mycursor = mydb.cursor()
sql = "DROP TABLE customers"
mycursor.execute(sql)

Run Instance

Delete Only If Table Exists

If the table to be deleted has been deleted or does not exist for any other reason, you can use the IF EXISTS keyword to avoid errors.

Example

Delete Table "customers" (if it exists):

import mysql.connector
mydb = mysql.connector.connect(
  host="localhost",
  user="yourusername",
  passwd="yourpassword",
  database="mydatabase"
)
mycursor = mydb.cursor()
sql = "DROP TABLE IF EXISTS customers"
mycursor.execute(sql)

Run Instance