w3resource

Using the DELETE Command to Remove Rows in PostgreSQL


Deleting Rows in PostgreSQL

In PostgreSQL, the DELETE statement is used to remove rows from a table. This can be done conditionally (based on a WHERE clause) to delete specific rows, or without conditions to remove all rows in a table. Proper use of DELETE ensures that only the desired data is removed, while caution is advised to avoid unintentional data loss.

Syntax:

DELETE FROM table_name
WHERE condition;

Here:

  • table_name: The name of the table from which you want to delete rows.
  • condition: A WHERE clause specifying which rows to delete. If omitted, all rows in the table are deleted.

Examples of Deleting Rows in PostgreSQL

Example 1: Delete a Specific Row

Suppose you want to delete a customer with a specific ID from the customers table:

Code:


DELETE FROM customers
WHERE customer_id = 101;

Explanation:

  • This command removes the row where customer_id is 101 from the customers table.
  • The WHERE clause ensures only the specified row is deleted, preventing accidental deletion of other rows.

Example 2: Delete Multiple Rows Based on Condition

To delete all orders from the orders table where the order_status is 'cancelled':

Code:

DELETE FROM orders
WHERE order_status = 'cancelled';

Explanation:

  • The command removes all rows from orders where order_status is 'cancelled'.
  • This is useful when removing obsolete or irrelevant data from a table.

Example 3: Delete All Rows from a Table

To remove all rows from the products table, you can use DELETE without a WHERE clause:

Code:

DELETE FROM products;

Explanation:

  • This command deletes all rows in the products table.
  • It keeps the table structure intact but clears all data, useful for resetting data during testing or cleanup.

Note: Use caution when deleting all rows. It’s often safer to use TRUNCATE TABLE products;, which is faster for large datasets.

Using RETURNING with DELETE to Confirm Deleted Rows

PostgreSQL's RETURNING clause can be paired with DELETE to return details of deleted rows, useful for confirmation or logging:

Code:

DELETE FROM customers
WHERE customer_id = 101
RETURNING customer_id, customer_name;

Explanation:

  • This query deletes the customer with customer_id = 101 and returns the customer_id and customer_name of the deleted row, confirming the action.

Practical Tips for Using DELETE in PostgreSQL:

  • Always Use a WHERE Clause for Specific Rows: Avoid accidentally deleting all data by using specific conditions with WHERE.
  • Consider RETURNING for Feedback: Use RETURNING to log or verify what was deleted.
  • Alternative for Deleting All Rows: TRUNCATE is often a better choice than DELETE for removing all rows, as it’s faster and less resource-intensive.

Summary:

The DELETE statement in PostgreSQL provides controlled row deletion from tables, whether for specific entries or entire datasets. Combined with the RETURNING clause, DELETE allows you to track deleted data. Understanding and using DELETE effectively is crucial to maintaining data integrity within your database.

All PostgreSQL Questions, Answers, and Code Snippets Collection.



Become a Patron!

Follow us on Facebook and Twitter for latest update.

It will be nice if you may share this link in any developer community or anywhere else, from where other developers may find this content. Thanks.

https://w3resource.com/PostgreSQL/snippets/postgres-delete-row.php