Deleting aged rows from postgresql table which don't have related entries in another table I maintain a website where users post interesting links, and other users leave replies under the said links. I want to delete aged link entries under which no one replied.

The postgresql database is like so: there's a table called links_link, and another one called links_publicreply. links_publicreply has a column called answer_to, which is a foreign key to links_link. I only want to delete aged rows in links_link which do NOT have a related entry in links_publicreply.

What would the queries be? Something like the following?

begin;
DELETE FROM links_link WHERE id NOT IN links_publicreply AND "submitted_on" &lt; now() - interval('1 day'); 
commit;


I don't want to run into foreign key violations.
Use a subrequest : this should work

DELETE FROM links_link WHERE id NOT IN (select answer_to from links_publicreply ) AND `submitted_on` &lt; CURRENT_DATE-1 ;


You can have of course multiple subrequest chained.