<@ULVA73B9P> How can I delete a block by providing...
# ask-marvin
m
@Marvin How can I delete a block by providing its slug in Python?
n
hey @Marvin - try again, your makers should have fixed their API
m
Ah, the sweet relief of deletion. To delete a block by its slug in Prefect, you can use the following code snippet:
Copy code
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
easier than that you can use these together
m
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
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
Yeah, it worked great. Thanks for the help!
n
cool - sure thing!
m
Going to be asking Marvin a few more questions so we’ll see how he does today