<@ULVA73B9P> how do I determine the last date of u...
# ask-marvin
n
@Marvin how do I determine the last date of usage for each block or variable in Prefect Cloud?
m
thought for 369.0 seconds
Short answer: there isn’t a built-in “last used/accessed” timestamp for Blocks or Variables in Prefect Cloud today. Prefect tracks created/updated (last modified) times, but reads/loads aren’t recorded as usage events. What you can do today: - Use “Updated” as a proxy for last change - In the UI: Blocks or Variables pages → click an item → see Created/Updated. - CLI: - Block details (shows created/updated):
Copy code
prefect block inspect <BLOCK_TYPE_SLUG>/<BLOCK_NAME>
- Variable details (shows created/updated):
Copy code
prefect variable inspect <NAME>
- API references (created/updated fields): - Blocks REST API - Variables REST API - CLI docs: prefect block, prefect variable If you need true “last used” tracking: - Emit a custom event whenever you load a block or read a variable, then use the Events view (or an Automation) to report the latest occurrence. Events docs: Events Example wrappers you can use in your flows: - Track variable access
Copy code
from prefect.variables import Variable
  from prefect.events.utilities import emit_event

  def tracked_variable_get(name: str, default=None):
      value = Variable.get(name, default=default)
      emit_event(
          event="variable.used",
          resource={
              "prefect.resource.id": f"variable/{name}",
              "prefect.resource.role": "variable",
          },
          payload={"name": name},
      )
      return value
- Track block usage
Copy code
from prefect.blocks.core import Block
  from prefect.events.utilities import emit_event

  def tracked_block_load(block_cls: type[Block], name: str):
      block = block_cls.load(name)
      emit_event(
          event="block.used",
          resource={
              "prefect.resource.id": f"block/{block_cls.__name__}/{name}",
              "prefect.resource.role": "block",
          },
          payload={"block_type": block_cls.__name__, "name": name},
      )
      return block
How to review usage once emitting events: - In Cloud, open Events and filter for
block.used
or
variable.used
. - Or stream in the CLI for quick checks:
Copy code
prefect events stream --format text
(Press Ctrl+C to stop.) If you’d like, I can help you: - Add these wrappers centrally (so all reads are tracked), - Create a small flow/automation that aggregates “last used” per block/variable and writes a report.