Delete an object from within an array using JSONB in PostgreSQL I am attempting to remove an element from my database. The element I want to remove is within a JSON object called playerContainer which contains an array named players. This all sits within a table called site_content. I am trying to remove the object based on it's ID.

Here is the sample JSON

"playersContainer": {
        "players": [
            {
                "id": "1",
                "name": "Nick",
                "teamName": "Shire Soldiers",
                "ratings": [
                    1,
                    5,
                    6,
                    9
                ],
                "assists": 17,
                "manOfTheMatches": 20,
                "cleanSheets": 1,
                "data": [
                    3,
                    2,
                    3,
                    5,
                    6
                ],
                "totalGoals": 19

            },
}


Below is the query I am attempting to use to delete the object within the array

UPDATE site_content
SET content = jsonb_set(content, '{playersContainer,players}'::text[], content-&gt;'playersContainer'-&gt;'id'-&gt;'players' #- '{1}'::jsonb


However this seems to delete all the data from my content row in my table and not just the array with an ID of 1 in my players array.
To remove key (warning - json/jsonb arrays index starts from 0 unlike pure SQL arrays):  

UPDATE site_content
   SET content #- '{playersContainer,players,0}'::text[];


To remove array element with specific id:

UPDATE site_content
   SET content = content #- coalesce(('{playersContainer,players,' || (
            SELECT i
              FROM generate_series(0, jsonb_array_length(content-&gt;'playersContainer'-&gt;'players') - 1) AS i
             WHERE (content-&gt;'playersContainer'-&gt;'players'-&gt;i-&gt;'id' = '"2"')
         ) || '}')::text[], '{}');