Published on

PostgreSQL Tips: Delete All Rows Except First 10 in a Table

Authors

Sometime, we might want to delete almost all the rows in a huge table. It could be for the purpose of performance testing, debugging, etc.

In PostgreSQL, we can easily do it with the following SQL statement.

DELETE FROM table_name -- Replace "table_name" with your table name
WHERE id NOT IN(
		SELECT
			id FROM table_name -- Replace "table_name" with your table name
		ORDER BY
			id
		LIMIT 10); -- Replace "10" with number of rows that you want to retain.

Happy dropping records!