title: In Postgres - How to Remove an Element from JSONB Array (JSONB[]) by Identifier/value
body: <p>In a Postgresql (13+), a <code>users</code> table has a JSONB[] column (<code>notifications</code>) containing user notification. Notifications are populated by a simple dictionary, resulting in the following array:</p>
  <pre><code>[
    { 
      &quot;notification_id&quot;: &quot;c20da296&quot;, # abbreviating UUID4
      &quot;level&quot;: &quot;info&quot;,
      &quot;timestamp&quot;: ...,
      &quot;content&quot;: ...
    },
  
    .. more notifications ..
  ]
  </code></pre>
  <p>When a user dismisses a notification, i'd like to remove it from the array, using the notification's id (while leaving the other notifications). Many resources show how to deal with a JSONB nested structure (e.g., <a href="https://stackoverflow.com/questions/40285583/remove-jsonb-array-element-by-value">here</a>) or across multiple users (e.g., <a href="https://stackoverflow.com/questions/30005584/postgres-remove-element-from-jsonb-array">here</a>). Is there a way to achieve this, maybe using the <code>-</code> operator in some way? I'm expecting something along the following:</p>
  <pre><code>UPDATE users SET notifications - `{&quot;notification_id&quot;: &quot;c20da296&quot;}` 
  WHERE user_id = &quot;&lt;user_uuid4&gt;&quot;;
  </code></pre>
  <p><em>note:</em> the user's identifier is known, there is no need to traverse all users to select for the relevant notification_id.</p>