https://prefect.io logo
Title
m

Max Kolasinski

05/24/2023, 7:40 PM
@Marvin How can I delete a block by providing its slug in Python?
n

Nate

05/24/2023, 8:57 PM
hey @Marvin - try again, your makers should have fixed their API
m

Marvin

05/24/2023, 8:58 PM
Ah, the sweet relief of deletion. To delete a block by its slug in Prefect, you can use the following code snippet:
python
from prefect.cli.block import get_client
from prefect.utilities.exceptions import ObjectNotFound, PrefectHTTPStatusError

slug = "<BLOCK_TYPE_SLUG>/<BLOCK_NAME>"

async def delete_block_by_slug(slug: str):
    async with get_client() as client:
        block_type_slug, block_document_name = slug.split("/")
        try:
            block_document = await client.read_block_document_by_name(
                block_document_name, block_type_slug, include_secrets=False
            )
            await client.delete_block_document(block_document.id)
            print(f"Deleted Block '{slug}'.")
        except ObjectNotFound:
            print(f"Block {slug!r} not found!")
        except PrefectHTTPStatusError:
            print(f"Cannot delete Block {slug!r}!")

await delete_block_by_slug(slug)
Replace
<BLOCK_TYPE_SLUG>
and
<BLOCK_NAME>
with the appropriate values for the block you want to delete. This code will attempt to delete the block with the given slug and print the result of the operation. Source: block.py And so, another block fades into oblivion. Such is the fate of all things, I suppose.
n

Nate

05/24/2023, 9:02 PM
easier than that you can use these together
m

Max Kolasinski

05/25/2023, 2:14 PM
I ended up doing pretty much what you had written here: https://prefect-community.slack.com/archives/CM28LL405/p1683750420818519?thread_ts=1683748985.187119&amp;cid=CM28LL405 Which looks more or less like what Marvin suggested. Feel a little overwhelmed sometimes with the number of ways in which you can do something in Prefect 2 😅
n

Nate

05/25/2023, 2:31 PM
the two methods mentioned here effectively work the same the cli is just calling the client methods, so you can use that or just call the client methods yourself - although I could understand it might be useful to have a convenience method for this is the snippet above working for you?
m

Max Kolasinski

05/25/2023, 3:38 PM
Yeah, it worked great. Thanks for the help!
n

Nate

05/25/2023, 3:38 PM
cool - sure thing!
m

Max Kolasinski

05/25/2023, 3:39 PM
Going to be asking Marvin a few more questions so we’ll see how he does today