Common SQL Errors

MySQL Error 1813 ER_TABLESPACE_EXISTS - How to Fix and Prevent

Galaxy Team
August 7, 2025

<p>MySQL raises ER_TABLESPACE_EXISTS (error 1813) when you attempt to create a tablespace that already exists in the data directory.</p>

Sign up for the latest in common SQL errors from the Galaxy Team!
Welcome to the Galaxy, Guardian!
You'll be receiving a confirmation email

Follow us on twitter :)
Oops! Something went wrong while submitting the form.

What is MySQL error 1813 ER_TABLESPACE_EXISTS?

<p>MySQL Error 1813: ER_TABLESPACE_EXISTS appears when a CREATE TABLESPACE or ALTER TABLE ... TABLESPACE statement names a tablespace that is already present. Confirm its presence in information_schema.FILES, then either DROP TABLESPACE, use IF NOT EXISTS, or choose a new name to resolve the conflict.</p>

Error Highlights

Typical Error Message

Tablespace '%s' exists.

Error Type

Object Already Exists Error

Language

MySQL

Symbol

ER_TABLESPACE_EXISTS

Error Code

1813

SQL State

HY000

Explanation

Table of Contents

What does MySQL Error 1813 ER_TABLESPACE_EXISTS mean?

Error 1813 fires when MySQL detects that a tablespace file with the specified name already exists in the server's data directory or file-per-table path. The server blocks the CREATE TABLESPACE or ALTER TABLE operation to prevent data corruption.

Because InnoDB treats a tablespace as a physical .ibd or .ibdata file, naming collisions can damage storage metadata. Fixing the issue quickly keeps DDL operations predictable and protects data integrity.

When does the error typically occur?

The error surfaces during CREATE TABLESPACE, CREATE UNDO TABLESPACE, ALTER TABLE ... TABLESPACE, or CREATE TABLE statements that implicitly create a file-per-table tablespace. It also appears after incomplete DROP TABLESPACE attempts that leave orphaned files on disk.

Developers often see the error after schema migrations, restore scripts, or automation reruns where the same tablespace name is reused without cleanup.

What causes MySQL Error 1813?

A pre-existing .ibd or .ibdata file with the same tablespace name is the primary trigger. This can result from aborted DDL, failed backups, or manual file copies.

Configuration mismatches like duplicate innodb_data_file_path entries and permission issues that prevent cleanup can also leave ghost tablespaces behind.

How to fix MySQL Error 1813 ER_TABLESPACE_EXISTS

First, verify that the tablespace truly exists by querying information_schema.FILES or inspecting the data directory. If it is not needed, run DROP TABLESPACE. If it is required, adjust your DDL to reference or rename it.

Using IF NOT EXISTS in your CREATE TABLESPACE statement or creating an alternative name eliminates the collision. Always back up critical data before dropping or renaming any tablespace.

Common scenarios and solutions

CI/CD pipelines rerunning the same migration should wrap CREATE TABLESPACE in IF NOT EXISTS to stay idempotent. Restore scripts that copy .ibd files must run DISCARD TABLESPACE before an import to avoid duplicates.

When cloning databases, ensure the data directory is empty or use different innodb_file_per_table settings to keep names unique.

Best practices to avoid this error

Adopt naming conventions that include environment or timestamp suffixes. Automate cleanup with DROP TABLESPACE during rollbacks. Monitor information_schema.FILES for orphaned entries and alert on duplicates.

Galaxy users can run guarded DDL through the editor, share audited migrations, and rely on version history to spot accidental duplicates before deployment.

Related errors and solutions

Errors 1814 (ER_TABLESPACE_MISSING) and 1007 (ER_DB_CREATE_EXISTS) also signal object collisions. Understanding their causes helps build resilient deployment scripts.

Common Causes

Duplicate CREATE TABLESPACE command

A migration script ran twice without IF NOT EXISTS, leaving the first tablespace intact.

Aborted DROP TABLESPACE

An earlier DROP failed due to foreign key constraints, leaving the physical file behind.

Manual file copy

Copying .ibd files between servers without DISCARD TABLESPACE created conflicting names.

Restore from backup

A restore process recreated the tablespace while the original still existed.

Related Errors

MySQL Error 1814 ER_TABLESPACE_MISSING

Raised when a referenced tablespace cannot be found on disk. Opposite of ER_TABLESPACE_EXISTS.

MySQL Error 1007 ER_DB_CREATE_EXISTS

Occurs when trying to create a database that already exists. Similar name collision but at the schema level.

MySQL Error 1022 ER_DUP_KEYNAME

Thrown when creating a duplicate index name within a table.

FAQs

Can I ignore ER_TABLESPACE_EXISTS if the file is empty?

No. Even empty tablespace files register in InnoDB metadata. Remove or rename them before proceeding.

Will IF NOT EXISTS always prevent the error?

Yes, IF NOT EXISTS instructs MySQL to skip creation when a tablespace with the same name is already present.

Does dropping a table remove its tablespace?

For file-per-table InnoDB tables, DROP TABLE deletes the .ibd file, but a failed drop can leave remnants that trigger ER_TABLESPACE_EXISTS.

How does Galaxy help avoid this error?

Galaxy's versioned queries and endorsements let teams reuse safe DDL blocks and surface duplicate tablespace names during code review.

Start Querying with the Modern SQL Editor Today!
Welcome to the Galaxy, Guardian!
You'll be receiving a confirmation email

Follow us on twitter :)
Oops! Something went wrong while submitting the form.

Check out some other errors

Trusted by top engineers on high-velocity teams
Aryeo Logo
Assort Health
Curri
Rubie Logo
Bauhealth Logo
Truvideo Logo