PostgreSQL

PostgreSQL DELETE

Safely remove selected records from a PostgreSQL table with the DELETE statement.

The DELETE statement

The DELETE statement removes existing records from a table. Use the WHERE clause to specify which record or records should be deleted. The condition is evaluated for every row, and matching rows are removed.

Delete matching records

To delete all records where name is Laptop, use:

DELETE FROM products
WHERE name = 'Laptop';

This removes every product whose name is Laptop. Before executing the delete, verify the matching records with:

SELECT * FROM products
WHERE name = 'Laptop';

Result

After the delete runs, PostgreSQL reports:

DELETE 1

This means that one row was deleted. If several rows match the condition, the number will reflect the total rows removed.

Display the table

To check the remaining records, display the table with:

SELECT * FROM products;
Warning: always review WHERE. If you omit the WHERE clause, PostgreSQL deletes every record in the table:
DELETE FROM products;

This removes all rows but leaves the products table itself in place.

Further Reading

Continue learning with these related tutorials:

Continue learning