October DealsAmazon USOctober deal check: compare before you payAmazon US: current deals, useful picks and tech finds.Check DealsClean PCRecommendedOne scan can reveal what keeps slowing WindowsLook for cleanup and repair opportunities.Run ScanOctober DealsAmazon USDeal season is back - check today's better picksAmazon US: current deals, useful picks and tech finds.See Picks×
Skip to content
World desk3 min

PostgreSQL ON DELETE: Match Each Action to the Data

Use CASCADE for dependent components, SET NULL for optional references, and RESTRICT when existing references must block deletion. PostgreSQL’s default NO ACTION can defer its check when the constraint is deferrable.
Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Choose a PostgreSQL ON DELETE action according to what the related rows mean: use CASCADE for dependent components, SET NULL for optional references, and RESTRICT when a delete must stop until the application handles existing references. PostgreSQL’s default, NO ACTION, also rejects a delete that leaves references, but a deferrable constraint can postpone that check until later in the transaction.

How the four actions differ

A foreign key links referencing rows to a referenced row. When someone tries to delete the referenced row, its ON DELETE action determines what PostgreSQL does with those references.

As an Amazon Associate I earn from qualifying purchases.

Action Referencing rows Foreign-key value Effect on delete Can the check be deferred?
CASCADE Deleted Row is removed Deletion propagates to referencing rows Not applicable as a rejection check
SET NULL Kept Set to NULL in the affected column or columns Referenced row can be deleted if the resulting row satisfies constraints Not applicable as a rejection check
RESTRICT Kept Unchanged Blocks deletion when references exist No
NO ACTION (default) Kept Unchanged Raises an error if references remain when the constraint is checked Yes, if the constraint is deferrable

PostgreSQL’s guidance is that the right action depends on “what kinds of objects the related tables represent.” PostgreSQL 18: Constraints.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

When to use each action

Use CASCADE for dependent components

Choose ON DELETE CASCADE when the referencing row is a component that has no useful independent existence. PostgreSQL’s example is order items belonging to an order: deleting the order can delete its items automatically.

Avoid cascading between independent entities if deleting one should not silently erase records belonging to the other. The convenience of automatic cleanup is also a responsibility: the delete can affect multiple referencing rows.

Use SET NULL for an optional relationship

Choose ON DELETE SET NULL when the referencing row remains meaningful after the referenced object is gone, but its association should be cleared. For example, an optional attribution can be removed while retaining the associated record.

  • The affected foreign-key column must permit NULL.
  • Other constraints must allow the resulting row state.
  • For a composite foreign key, PostgreSQL allows specifying which columns are set to null. This is useful when another foreign-key column also participates in a primary key.

Use RESTRICT when a delete must stop immediately

Choose ON DELETE RESTRICT when the referenced row must not be deleted while references exist. The application must first make an explicit decision—for example, reassign or delete the referencing rows. PostgreSQL does not allow the RESTRICT check to be deferred.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Keep NO ACTION for the default, or defer its check when needed

NO ACTION is PostgreSQL’s default. If references still exist when the constraint is checked, the delete fails. Unlike RESTRICT, a deferrable NO ACTION constraint can have its check postponed until later in the transaction. That can allow other commands in the same transaction to remove or repair the references before the check occurs.

A practical decision path

  1. Ask whether the referencing row has its own lifecycle. If it is only a component of the referenced row, choose CASCADE.
  2. If it survives independently, ask whether the reference is optional. If so, choose SET NULL only after confirming nullability and other constraints.
  3. If the reference must be handled explicitly, decide when the check should happen. Choose RESTRICT to block the delete immediately; choose deferrable NO ACTION if the transaction needs to repair or remove references before the check.
Independent reader supportYour contribution helps us test, update, and keep practical guides available for everyone.Support on Ko-Fi

Check the foreign-key index separately

PostgreSQL requires the referenced key to be backed by a primary key, unique constraint, or qualifying unique index. It does not automatically create an index on the referencing columns. An index there can make it more efficient to find referencing rows when deleting referenced rows or updating referenced keys; whether it is worthwhile depends on the workload and indexing choices. PostgreSQL 18 documentation on foreign-key constraints.

Product prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on Amazon at the time of purchase will apply.

Leave a Reply

Your email address will not be published. Required fields are marked *

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

More from the Wire

  1. World desk4 min
    How to Spot an AI Voice Scam Before Sending MoneyDon’t rely on how a caller sounds. Pause, call back through a known number, and verify the emergency with another trusted person before sending money.
  2. Mountain View desk4 min
    Google’s SynthID Detector: How to Check AI-Generated Images, Video and AudioGoogle’s SynthID Detector looks for an embedded watermark in supported images, video and audio. Here is what its results do—and do not—show.
  3. Redmond desk20 min
    How to create a link to File or Folder in Windows 11Windows 11 gives you several ways to point to a file or folder without moving or duplicating it. You can create a desktop shortcut,…
Recommended PC Tool
Recommended PC Tool
PC Slower Than It Used to Be?Free scan - under a minute
Outdated Drivers Are Slowing You DownFree scan - exact matches

Two free Windows tools

One Free Minute Could Fix That PC

Before you go - each of these free tools takes about a minute and tackles what quietly slows a Windows PC down.

Special offer. View Outbyte info, uninstall instructions, EULA, and Privacy Policy.