Gaming industry under DDoS attack. Get DDoS protection now. Start onboarding
  1. Home
  2. Developers
  3. How to Delete All Rows in MySQL

How to Delete All Rows in MySQL

  • By Gcore
  • May 28, 2023
  • 2 min read
How to Delete All Rows in MySQL

There may be times when you need to delete all records from a MySQL table. This could be for several reasons, such as wanting to clean up the data, removing outdated records, or starting over. In this guide, you’ll learn how to delete all records using two SQL statements: DELETE and TRUNCATE.

Differences Between DELETE and TRUNCATE

You can delete all records from a table using either the DELETE or the TRUNCATE statement. The DELETE statement removes rows individually, recording an entry for each deleted row in the transaction log. On the other hand, the TRUNCATE statement is a quicker method for emptying a table. It works by deallocating the data pages used by the table, effectively clearing it out completely.

The DELETE and TRUNCATE statements have several differences:

  1. Performance: TRUNCATE is faster than DELETE because it does not generate individual row delete statements or log each row deletion.
  2. Rollback: You can roll back DELETE operations if you’re using transactions, while TRUNCATE operations cannot be rolled back.
  3. Triggers: The DELETE statement will activate any ON DELETE triggers associated with the table. The TRUNCATE statement will not.
  4. Space reclaiming: DELETE does not free the space that the table occupies. In contrast, TRUNCATE reclaims the space used by the table for further use.

Note: Always ensure you have a recent backup of your data before executing any delete operations. This safeguard could be a lifesaver if anything goes wrong.

Empty the table with the DELETE Statement

1. Open your MySQL client. 

2. Select the database containing the table from which you want to delete records:

USE database_name;

Replace “database_name” with the name of the relevant database.

3. Delete all records from the table using the DELETE command:

DELETE FROM table_name;

Replace “table_name” with the name of the relevant table. If you can’t recall the table name, refer to our guide “How to List Tables in a MySQL Database” (step #2).

Empty the table with the TRUNCATE Statement

1. Open your MySQL client.

2. Select the database containing the table from which you want to delete records:

USE database_name;

Replace “database_name” with the name of the relevant database.

3. Delete all records from the table:

TRUNCATE TABLE table_name;

Replace “table_name” with the name of the relevant table. If you can’t recall the table name, refer to our guide “How to List Tables in a MySQL Database” (step #2).

Conclusion

Searching for a managed database solution? Choose Gcore Managed Database for PostgreSQL so you can focus on your core business while we manage your database.

  • 99.9% SLA for uninterrupted service with high-availability architecture
  • Adjustable database resources for changing demands
  • Currently in free public beta

Start managing your database

Related articles

What's the difference between multi-cloud and hybrid cloud?

Multi-cloud and hybrid cloud represent two distinct approaches to distributed computing architecture that build upon the foundation of cloud computing to help organizations improve their IT infrastructure.Multi-cloud environments involve us

What is multi-cloud? Strategy, benefits, and best practices

Multi-cloud is a cloud usage model where an organization utilizes public cloud services from two or more cloud service providers, often combining public, private, and hybrid clouds, as well as different service models, such as Infrastructur

What is cloud migration? Benefits, strategy, and best practices

Cloud migration is the process of transferring digital assets, such as data, applications, and IT resources, from on-premises data centers to cloud platforms, including public, private, hybrid, or multi-cloud environments. Organizations can

What is a private cloud? Benefits, use cases, and implementation

A private cloud is a cloud computing environment dedicated exclusively to a single organization, providing a single-tenant infrastructure that improves security, control, and customization compared to public clouds.Private cloud environment

What is a cloud GPU? Definition, types, and benefits

A cloud GPU is a remotely rented graphics processing unit hosted in a cloud provider's data center, accessible over the internet via APIs or virtual machines. These virtualized resources allow users to access powerful computing capabilities

What is cloud networking: benefits, components, and implementation strategies

Cloud networking is the use and management of network resources, including hardware and software, hosted on public or private cloud infrastructures rather than on-premises equipment. Over 90% of enterprises are expected to adopt cloud netwo

Subscribe to our newsletter

Get the latest industry trends, exclusive insights, and Gcore updates delivered straight to your inbox.