Swiftybase
Function StackDatabasePostgresExamples

Delete Returns

Example 1: Delete and Return Data

In this example, we'll use the Postgres Execute function with the Type set to Delete Returns to remove data and return specific values from the deleted rows.

Add Postgres Execute Function

Start by adding the Postgres Execute function. Give it a Result Name and set the Type to Delete Returns.

Write the SQL Query

In the function configuration, write the SQL query with a RETURNING clause to get the deleted data.

Assume we have a users table and want to delete users where id > 15. We also want to return their name. The table schema looks like this:

CREATE TABLE public.users (
  id bigint GENERATED BY DEFAULT AS IDENTITY NOT NULL,
  created_at timestamp with time zone NOT NULL DEFAULT now(),
  name text NULL,
  email text NULL,
  age integer NULL,
  activated boolean NULL,
  updated timestamp without time zone NULL,
  CONSTRAINT users_pkey PRIMARY KEY (id)
);

Now write the following query:

DELETE FROM users WHERE id > $1 RETURNING name

Fill the Arguments

Provide the value for the id condition. Make sure the list literal is enabled:

[15]

Define a Type

Next, define a primitive type like String to hold the result.

Testing

When you run the query, it will delete users with id > 15 and return their names.

On this page