Back to articles list May 22, 2014 - 1 minutes read DELETE RETURNING clause in PostgreSQL Agnieszka Kozubek-Krycuń Agnieszka is a Chief Content Officer at Vertabelo. Before coming to Vertabelo, she worked as a Java programmer. She has a PhD in mathematics and over 10 years of experience in teaching mathematics and computer science at the University of Warsaw. In her free time, she enjoys reading a good book, going mountain hiking and practicing yoga. Tags: PostgreSQL SQL The standard DELETE statement in SQL returns the number of deleted rows. DELETE FROM external_data; DELETE 10 In PostgreSQL you can make DELETE statement return something else. You can return all rows that have been deleted. DELETE FROM external_data RETURNING *; id | creation_date | user_id | data ----+---------------------------+---------+---------------- 101 | 2014-05-06 13:10:45.09484 | 23 | 'Some text' 102 | 2014-06-10 22:23:12.12045 | 25 | 'Some other text' (2 rows) DELETE 2 You can return the columns of your choice. DELETE FROM external_data RETURNING id; id ---- 101 102 (2 rows) DELETE 2 In your code you can process the returned rows in the same way as you would process the results of an SQL query. For example, you may log the data that have been deleted. Tags: PostgreSQL SQL You may also like SQL INSERT, SQL UPDATE, SQL DELETE – Oh My! Need to understand SQL INSERT, UPDATE, or DELETE? We'll get you up to speed with a comprehensive How-To and useful examples! Read more How to Use Subqueries in INSERT, UPDATE, and DELETE Statements Do you find subqueries useful in SELECT? Learn how to use them in INSERT, UPDATE, and DELETE and see how powerful your SQL queries can be. Read more TRUNCATE TABLE vs. DELETE vs. DROP TABLE: Removing Tables and Data in SQL What’s the difference between DROP TABLE, TRUNCATE TABLE, and DELETE in SQL? We’ll look at the syntax of each operation and discuss their usage, details, and differences. Read more How to Use Subqueries in INSERT, UPDATE, and DELETE Statements Do you find subqueries useful in SELECT? Learn how to use them in INSERT, UPDATE, and DELETE and see how powerful your SQL queries can be. Read more SQL Basics Cheat Sheet This 2-page SQL Basics Cheat Sheet will be a great value for beginners as well as for professionals. Download it in PDF or PNG format. Read more How SQL supports data-driven organization Businesses face many decisions. Most of them are driven by intuition, but organizations that make the most business impact use data-driven decision-making. Read more How to Select the First Row in a Group? Often you want to select a single row from each GROUP BY group. PostgreSQL has a statement especially for that: SELECT DISTINCT ON. Say I want to select one weather report for each location. Read more Subscribe to our newsletter Join our weekly newsletter to be notified about the latest posts.