Truncating tables via Doctrine 2.x in MySQL
Feb 26, 2013
I recently stumbled across a question on stackoverflow.com where somebody was having problems with foreign key constraints and truncating data in MySQL. An accepted answer stated that while it was possible to truncate the data, it isn’t possible if there are foreign keys present (as is the case with Doctrine 2 entities).
Actually, it is possible to truncate your data even with foreign key constraints. It can be dangerous if you’re not careful about what you delete, but the following code can be useful in Doctrine 2.x if you have a need for emptying tables programmatically:
The raw query before and after the truncate statement tells MySQL to forget about foreign key constraints, so it just blindly empties your table(s).
Hopefully this will save people some time.
Share