16

How can I empty the contents of all tables in my database in phpMyAdmin without dropping any of the tables in the database?

Since I do this several times an hour while in development, I'd rather not individually click "Empty" on all 60+ tables every time.

Ivan Beldad
  • 2,285
  • 2
  • 21
  • 33
Joshua
  • 6,643
  • 15
  • 55
  • 76

10 Answers10

37

IMHO the easiest solution is:

  • In phpmyadmin select the database you want to truncate in the databases-list (left-hand-side)
  • In the main view scroll down, you'll see a checkbox "Check All" and next to it a drop-down box where you can select "Empty". Done.
andreas
  • 955
  • 7
  • 15
  • 5
    This is THE best answer, since it's easy, to the point and it's the ONLY answer that exactly does what the original question asked (delete all tables using PhpMyAdmin, not SQL or console calls). The next best answer is the one by John Yin, but even though he used PhpMyAdmin, his method is only useful when importing a very similar with the same tables. – OMA Dec 24 '15 at 13:20
  • 1
    This the best answer by far and also the easiest to implement – Jean Eric Feb 12 '20 at 14:26
  • Also, for future readers: you can uncheck "foreign key check" thing. And indeed, it was the easiest answer. – Hossam El-Deen Apr 30 '20 at 10:52
7

Create a SQL script with multiple DELETE statements (one for each table) and execute it.

Get into phpMyAdmin and select the database that you want. Select the SQL tab and paste the SQL script into the window. Hit Go.

Look here too:

Drop all tables from a MySQL Database without deletion

Leniel Maccaferri
  • 100,159
  • 46
  • 371
  • 480
4

We can truncate all tables data by phpMyAdmin actually!

In phpMyAdmin, you can do it as following steps:

1) select u DB and do Export operation as this way:

select Custom Export method

  • select 'Dump Table -> data' in Format-specific options
  • select 'Add DROP TABLE ... statement' in Object Creation Options.

By this step, phpMyAdmin helps us create one sql script of full list of all tables

3) do Import operation to delete and create each blank table one by one by this script

enter image description here

John Yin
  • 8,057
  • 4
  • 25
  • 25
  • This is one of the best answers because the original question asked about PhpMyAdmin, not regular SQL, and console calls might not be an option if the user has no privileges to do that. – OMA Dec 24 '15 at 13:17
3

you could start with this query

SELECT T.*
FROM INFORMATION_SCHEMA.tables T
WHERE T.table_type = 'BASE TABLE'

And iterate through those results to build a dynamic SQL string to the tune of 'DELETE FROM ' + T.Table_Name

LesterDove
  • 3,014
  • 1
  • 23
  • 24
2

Unfortunately, there is no TRUNCATE DATABASE or equivalent. With that said, you could probably use some kind of stored procedure that go through all tables in your database and truncate them. I found something of that kind here, but I don't know whether it works. (You should probably read the comment discussion too)

PatrikAkerstrand
  • 45,315
  • 11
  • 79
  • 94
2

You can delete all data from phpmyadmin function easily. Maybe this feature didn't exists during 2010 when this question was posted, but I feel all beginners can refer to this.

Delete all data from database

Byte2c
  • 21
  • 1
2

The TRUNCATE TABLE statement will empty a table completely (MySql 3.23 and above).

You can create a script that you can reuse for a particular database. It's not generic but saves you from creating a new database + login each time you want to start with a fresh db.

Here's how to create a script yourself: In your webbrowser go to phpMyAdmin and select the database. Click the SQL tab and execute 'SHOW TABLES'. Choose the printerfriendly format and copy the contents to a text editor.

Now for each table name you re-write it in the format:

TRUNCATE TABLE <tablename>;

note: You can use a regular expression if you have many tables

Ghostman
  • 6,042
  • 9
  • 34
  • 53
mathijsuitmegen
  • 2,270
  • 1
  • 34
  • 36
  • If you truncate a table like that will the primary key start back at 1? – JRowan Aug 03 '16 at 21:48
  • 1
    Nevermind this told me http://www.mainelydesign.com/blog/view/difference-between-truncate-empty-delete-from-in-mysql – JRowan Aug 03 '16 at 21:52
1

You could export the table structures only like this:

mysqldump -u root -p --no-data mydb > backup_info.sql

then drop the whole database, recreate it and restore from the backup.

Easy enough to script the whole thing.

OGHaza
  • 4,795
  • 7
  • 23
  • 29
1
drop procedure if exists truncate_tables;

delimiter #
create procedure truncate_tables()
begin
 declare tab_name varchar(64);
 declare done tinyint unsigned default 0;

 declare table_cur cursor for select t.table_name
 from 
  information_schema.schemata s
  inner join information_schema.tables t on s.schema_name = t.table_schema
 where
   s.schema_name = database() and t.table_type = 'BASE TABLE';

 declare continue handler for not found set done = 1;

 open table_cur;
 repeat
   fetch table_cur into tab_name;
   set @cmd = concat('truncate table ', tab_name);

   prepare stmt from @cmd;
   execute stmt;
 until done end repeat;

 close table_cur;
end #
Jon Black
  • 16,223
  • 5
  • 43
  • 42
0

Actually, went deeper and wrote this:

SET @command:='';
SELECT @command:=CONCAT(@command, 'TRUNCATE TABLE ',T.TABLE_NAME,';')
FROM INFORMATION_SCHEMA.tables T
WHERE T.table_type = 'BASE TABLE' AND T.table_schema='YOUR_TABLE_SCHEMA';
PREPARE bye_world FROM @command;
EXECUTE bye_world;
DEALLOCATE PREPARE bye_world;

It selects all table names from provided schema YOUR_TABLE_SCHEMA and puts them into a @command user variable, forming a query for each one like this: TRUNCATE TABLE TABLE_NAME;

Then i just prepare selected statement and execute it. Note, that you must declare user variable before query, or it will be 0 and mess up our statement.

Tutorial

Using MySQL DROP TABLE To Remove Existing Tables

UnstableFractal
  • 1,403
  • 2
  • 15
  • 29