SQL Truncate
Today during a smoke-test I watched a SQL-savvy colleague wipe a database table with the truncate
syntax.
truncate users;
truncate
is a feature of Postgres and is included in the SQL:2008 standard.
truncate
includes a cascade
option that further truncates all tables that have foreign-key references to any of the named tables, or any tables included by cascade
itself. That sounds either useful or extremely destructive, depending on context.
This replaces my go-to for this type of cleanup, the Rails console:
User.delete_all
The benefits of truncate
over delete_all
are that you have more control and it's probably a little faster due to requiring one less layer of abstraction.