> ## Documentation Index
> Fetch the complete documentation index at: https://cloudinary.com/documentation/llms.txt
> Use this file to discover all available pages before exploring further.

# Delete temporary UGC assets


When you allow users to upload their own content to your product environment for time-bound activities, such as to see how your product looks in their environment, as in the [Try before you buy](try_before_you_buy_sample_project) use-case, you need to manage the deletion of these temporary assets.

One way to do this is to use [MediaFlows](mediaflows) to check periodically for uploaded assets that have been present for a certain amount of time and automatically delete them.

This guide demonstrates how to build a **PowerFlow** that does just that. 

![Delete UGC media PowerFlow](https://cloudinary-res.cloudinary.com/image/upload/bo_2px_solid_gray/f_auto/q_auto/docs/mediaflows/delete_ugc_media_flow_on_scheduled_search.png "thumb: c_scale,w_800/dpr_2.0, width:800, popup:true")

> **TIP**: You can access MediaFlows from your [Cloudinary Console](https://console.cloudinary.com/mediaflows). From there, create a new PowerFlow. 

[Sign up for free](https://cloudinary.com/users/register_free) if you don't yet have a Cloudinary account.

## Overview
The PowerFlow uses the following blocks:

* [On Scheduled Search](#on_scheduled_search): Triggers the flow periodically and runs the rest of the flow for the assets returned by the search
* [Iterate Assets](#iterate_assets): Ensures that the subsequent blocks run for each asset returned by the search
* [Condition](#condition): Checks if the asset is an image or video
* [Scheduled Email Report](#scheduled_email_report): Includes the asset from its part of the flow in a scheduled daily email
* [Delete Media](#delete_media): Deletes the asset from your Cloudinary product environment
* [Add To Logs](#add_to_logs): Writes a message to the log

## On Scheduled Search

The **On Scheduled Search** block triggers the flow periodically and runs the rest of the flow for the assets returned by the search.

Steps:

1. Add the **On Scheduled Search** block to your canvas.
1. Click the block to edit its settings.
1. Set the **Recurring schedule** according to when you want the flow to trigger. See [example cron expressions](#example_cron_expressions).
1. Set the **Search expression** to return the assets you want to delete. For example, to delete assets uploaded over a day ago with the tag `ugc-content` applied, use the expression: `created_at 

## Iterate Assets

The **Iterate Assets** block ensures that the subsequent blocks run for each asset returned by the search.

Steps:

1. Add the **Iterate Assets** block to your canvas and ensure it's connected to the **On Scheduled Search** block.
1. Click the block to edit its settings.
1. Set the **Source of asset list** field to On Scheduled Search **Resources** (use the `+` icon to insert the variable).
1. Set **Run in parallel** to **Yes**. There's no reason to process each asset in order and it's faster to run in parallel.

![Iterate Assets configuration](https://cloudinary-res.cloudinary.com/image/upload/bo_1px_solid_gray/f_auto/q_auto/docs/mediaflows/ugc-delete-iterate-assets-scheduled.png "thumb: c_scale,w_600/dpr_2.0, width:600, popup:true")

## Condition

There are two **Condition** blocks in this flow, which lead to three possible branches: one for images, one for videos, and one for [raw files](cloudinary_glossary#raw_asset).

### Is it an image?

The first **Condition** block checks if the asset is an image. If it isn't, the flow moves to the second condition block, which checks if it's a video. 

Steps:

1. Add the **Condition** block to your canvas and ensure it's connected to the **Iterate Assets** block.
1. Click the block to edit its settings.
1. Set the name to something meaningful like `Is it an image`.
1. In the **First variable** field, click the `+` icon to insert a variable. Select **Resource type** from **Iterate Assets**.
1. Set the **Operator** to `equal_to`.
1. Set the **Second variable** to `image`.
1. Set the **Variable type** to `String`.

![Condition configuration](https://cloudinary-res.cloudinary.com/image/upload/bo_1px_solid_gray/f_auto/q_auto/docs/mediaflows/ugc-delete-is-an-image-scheduled.png "thumb: c_scale,w_600/dpr_2.0, width:600, popup:true")

The top output connector is the path taken if the condition is true (in this case if the resource type is "image"), otherwise the flow continues through the bottom output connector.

### Is it a video?

If the asset isn't an image, the flow moves on to check if it's a video. 

Steps:

1. Add the **Condition** block to your canvas and ensure it's connected to the bottom output of the previous **Condition** block.
1. Click the block to edit its settings.
1. Set the name to something meaningful like `Is it a video`.
1. In the **First variable** field, click the `+` icon to insert a variable. Select **Resource type** from **Iterate Assets**.
1. Set the **Operator** to `equal_to`.
1. Set the **Second variable** to `video`.
1. Set the **Variable type** to `String`.

![Condition configuration](https://cloudinary-res.cloudinary.com/image/upload/bo_1px_solid_gray/f_auto/q_auto/docs/mediaflows/ugc-delete-is-a-video-scheduled.png "thumb: c_scale,w_600/dpr_2.0, width:600, popup:true")

If the asset isn't an image or video, the flow presumes that it's a raw file.

## Scheduled Email Report

At this point in the flow there are three branches. Add the next three blocks to each of the branches and configure them according to whether the asset is an image, video, or raw file.

The **Scheduled Email Report** block includes the asset from its branch of the flow in a scheduled daily email.

Steps:

1. Add three **Scheduled Email Report** blocks to the canvas.
1. Ensure the first is connected to the top output of the **Is it an image** condition block.
1. Ensure the second is connected to the top output of the **Is it a video** condition block.
1. Ensure the third is connected to the bottom output of the **Is it a video** condition block.
1. Click each block in turn to edit its settings.
1. Set **Use the asset from** to **Iterate Assets**.
1. Set the **Recipients** to one or more email addresses (separated by commas) that will receive the email.
1. Set the **Time** to the UTC time at which the email will be sent. If the flow runs three times a day, and this block fires in one or more of those runs, the flow sends one email per day.
1. Set the **Report name** to something meaningful to that branch, e.g. `Delete UGC Image`.
1. Set the **Description** to something meaningful to that branch, e.g. `Deleted UGC images`.

![Scheduled Email Report configuration](https://cloudinary-res.cloudinary.com/image/upload/bo_1px_solid_gray/q_auto/f_auto/docs/mediaflows/ugc-delete-scheduled-email-report-scheduled.png "thumb: c_scale,w_600/dpr_2.0, width:600, popup:true")

The email that's sent looks something like this:

![Scheduled email report](https://cloudinary-res.cloudinary.com/image/upload/bo_1px_solid_gray/f_auto/q_auto/docs/delete_ugc_image_email.png "thumb: c_scale,w_600/dpr_2.0, width:600, popup:true")

## Delete Media

The **Delete Media** block deletes the specified asset from your Cloudinary product environment.

Steps: 

1. Add three **Delete Media** blocks to the canvas.
1. Ensure each is connected to the output of one of the **Scheduled Email Report** blocks.
1. Click each block in turn to edit its settings.
1. Set the name to something meaningful like `Delete Image`, `Delete Video`, or `Delete Raw`.
1. Set **Use the asset from** to **Iterate Assets**.
1. Set **Invalidate** to true to invalidate CDN cached copies of the asset.

![Delete Media configuration](https://cloudinary-res.cloudinary.com/image/upload/bo_1px_solid_gray/f_auto/q_auto/docs/mediaflows/ugc-delete-delete-media-scheduled.png "thumb: c_scale,w_600/dpr_2.0, width:600, popup:true")

> **TIP**: If you have backups enabled on your product environment, consider also [deleting backups](backups_and_version_management#deleting_backups).

## Add To Logs

The **Add To Logs** block writes whatever message you want to the log. In this case, you could record the asset's public ID.

Steps:

1. Add three **Add To Logs** blocks to the canvas.
1. Ensure each is connected to the output of one of the **Delete Media** blocks.
1. Click each block in turn to edit its settings.
1. Set the name to something meaningful like `Add To Logs Image`, `Add To Logs Video`, or `Add To Logs Raw`.
1. Set the **Message** to something meaningful. To add the public ID of the deleted asset, click the `+` icon to insert the Iterate Assets **Public id** variable.

![Add To Logs configuration](https://cloudinary-res.cloudinary.com/image/upload/bo_1px_solid_gray/f_auto/q_auto/docs/mediaflows/ugc-delete-logs-iterate.png "thumb: c_scale,w_600/dpr_2.0, width:600, popup:true")

