remove all data from table mysql code example

Example 1: delete all rows from table mysql

-- Quick, no possible rollback
TRUNCATE TABLE my_table;
-- With rollback
DELETE FROM my_table;
COMMIT;

Example 2: mysql empty a table

-- If you do not need a condition or limit the rows:
TRUNCATE TABLE tblYourTable; -- Not Foreign key constrained

-- Or

SET FOREIGN_KEY_CHECKS = 0;

TRUNCATE YourTable1;
TRUNCATE YourTable2;

SET FOREIGN_KEY_CHECKS = 1;

-- --------------------------------------------------------
-- Otherwise:
DELETE FROM tblYourTable WHERE condition;

-- Or

DELETE FROM tblYourTable LIMIT row_count;

Example 3: remove all records from table mysql

/* Will delete all rows from your table. Next insert will take next auto increment id. */
DELETE from tableName;
/* Will delete all rows from your table but it will start from new row with 1. */
TRUNCATE tableName;

Example 4: delete all content in table mysql

TRUNCATE tablename

Example 5: delete all tables from database mysql

DROP PROCEDURE IF EXISTS `drop_all_tables`;

DELIMITER $$
CREATE PROCEDURE `drop_all_tables`()
BEGIN
    DECLARE _done INT DEFAULT FALSE;
    DECLARE _tableName VARCHAR(255);
    DECLARE _cursor CURSOR FOR
        SELECT table_name 
        FROM information_schema.TABLES
        WHERE table_schema = SCHEMA();
    DECLARE CONTINUE HANDLER FOR NOT FOUND SET _done = TRUE;

    SET FOREIGN_KEY_CHECKS = 0;

    OPEN _cursor;

    REPEAT FETCH _cursor INTO _tableName;

    IF NOT _done THEN
        SET @stmt_sql = CONCAT('DROP TABLE ', _tableName);
        PREPARE stmt1 FROM @stmt_sql;
        EXECUTE stmt1;
        DEALLOCATE PREPARE stmt1;
    END IF;

    UNTIL _done END REPEAT;

    CLOSE _cursor;
    SET FOREIGN_KEY_CHECKS = 1;
END$$

DELIMITER ;

call drop_all_tables(); 

DROP PROCEDURE IF EXISTS `drop_all_tables`;