https://prefect.io logo
Title
a

Ananya Mishra

05/10/2023, 8:03 PM
Hi All, We are using a script to programmatically delete unused blocks from our dev environment via the API using
prefect block delete...
. However, when trying to delete these blocks, we keep getting 409 Conflict error codes on those blocks. Strange, because the same blocks can be deleted just fine through the UI. Any ideas on how we could resolve this? Feeling like some kind of issue with how the API is handling block deletion...
1
n

Nate

05/10/2023, 8:09 PM
hi @Ananya Mishra - can you share the script that you're using? and the output of
prefect version
if possible
a

Ananya Mishra

05/10/2023, 8:15 PM
it's a bit layered, but the gist is we collect all the block slugs to delete into an python dict like this
{'kubernetes-job/deployment-infra-16899', 's3/deployment-storage-16451', 'kubernetes-job/deployment-infra-16541', ...}
and then run
subprocess.run(f"prefect block delete {slug}", shell=True)
in python for each block
root@421ed1fe04e5:~/.prefect# prefect version
Version:             2.7.12
API version:         0.8.4
Python version:      3.11.1
Git commit:          524c25cd
Built:               Mon, Feb 6, 2023 4:31 PM
OS/Arch:             linux/aarch64
Profile:             default
Server type:         cloud
n

Nate

05/10/2023, 8:17 PM
is there a reason you're using the CLI? I would use the client for this
a

Ananya Mishra

05/10/2023, 8:18 PM
cool let me try that
n

Nate

05/10/2023, 8:18 PM
cool! lmk if you need any more direction on that, you should just have to collect the block ids you need and pass them to that client method
a

Ananya Mishra

05/10/2023, 8:19 PM
is id different from the slug i posted above?
n

Nate

05/10/2023, 8:22 PM
so I would use this to read the block document by
slug
(as you have above), grab the ID (a UUID) off each one with
block_document.id
and then pass that value into
delete_block_document
👀 1
from prefect import get_client
from uuid import UUID

slugs = ["block-type/block-doc-slug", ...]

async with get_client() as client:
   for slug in slugs:
      type_slug, name_slug = slug.split('/')
      block_document = await client.read_block_document_by_slug(name_slug, type_slug)
      await client.delete_block_document(UUID(block_document.id))
should be something like this
a

Ananya Mishra

05/10/2023, 8:37 PM
hmm yeah that didn't really change anything. still getting
httpx.HTTPStatusError: Client error '409 Conflict' for url '<https://api.prefect.cloud/api/accounts/b8194971-9f89-43fe-a099-69552528a88a/workspaces/e0b524be-50ed-4eca-b77b-d2a1b2ade39f/block_documents/5fd2e02b-160e-4cf4-a990-5754e5e6230a>'
i think we need to take a closer look at what is happening with those objects on the server side that could be leading to these 409s
is there some other process we don't see using them?
perhaps you can take a look at that exact object i sent and see what's up there...or if you can delete it programmatically from your end.
n

Nate

05/10/2023, 8:40 PM
ah, are any of these blocks used by any of your deployments that exist?
i noticed your slugs were referring to storage and infra blocks, so if these were associated with an active deployment, this would be expected behavior - although we could probably improve the error message
a

Ananya Mishra

05/10/2023, 8:41 PM
i see. so then we need to make sure all associated deployments are deleted first?
n

Nate

05/10/2023, 8:41 PM
yes
👀 1
a

Ananya Mishra

05/10/2023, 8:43 PM
also i guess it's good for yall to note this is permitted by your UI while the API seems to catch it?
:thank-you: 1
n

Nate

05/10/2023, 8:46 PM
hmm i will check on that