Failed Bulk Job Cleanup Workflow
Salesforce action: DeleteJob. Daily flow scans for completed or failed bulk jobs older than 7 days using GetAllJobs, deletes them to free storage, and logs each deletion to a Dataverse audit table for compliance tracking.
Provided as-is, without warranty of any kind. Review and test each pattern in a non-production environment before deploying it to live automations. See our Terms.
Overview
Automated daily cleanup flow that scans all Salesforce bulk jobs, identifies completed or failed jobs older than a configurable retention period, deletes them to free storage, and logs each deletion to a custom Dataverse audit table for compliance tracking. Ships in the Off state — turn on by authorizing the Salesforce and Dataverse connections and configuring the environment variable.
Use Case
Salesforce bulk API jobs (imports, exports, queries) accumulate over time and consume org storage. This flow automates the housekeeping by removing stale completed and failed jobs while maintaining a full audit trail in Dataverse. IT admins can adjust the retention window via a single environment variable without modifying the flow logic.
Flow Architecture
Daily Recurrence
RecurrenceRuns daily at 6:00 AM Eastern Standard Time.
Init varRetentionDays
Initialize variableReads the flowlibs_SalesforceJobRetentionDays environment variable (default: 7 days). Runs in parallel with the deleted-count initializer.
Init varDeletedCount
Initialize variableInteger counter initialized to 0. Runs in parallel with the retention-days initializer.
Compose Cutoff Timestamp
ComposeCalculates the cutoff date by subtracting retention days from current UTC time using addDays(utcNow(), mul(int(varRetentionDays), -1)).
Get All Bulk Jobs
Salesforce — GetAllJobsRetrieves all visible Salesforce bulk jobs.
Filter Stale Completed Or Failed Jobs
Filter arrayCompound condition: job state is 'JobComplete' OR 'Failed' AND createdDate is before the cutoff timestamp.
For Each Stale Job
Apply to eachFor each filtered job: deletes it via Salesforce DeleteJob, creates a record in the flowlibs_bulkjobdeletionlogs Dataverse table capturing job ID, state, operation, object, and deletion timestamp, and increments varDeletedCount.
Compose Cleanup Summary
ComposeGenerates a summary string with total deleted count, retention threshold, and cutoff date.
Environment Variables
| Schema name | Type | Default | Description |
|---|---|---|---|
| flowlibs_SalesforceJobRetentionDays | String | 7 | Number of days to retain completed/failed bulk jobs before cleanup. |
Connectors & Connections
| Connector | API name | Actions used |
|---|---|---|
| Salesforce | shared_salesforce | GetAllJobs DeleteJob |
| Microsoft Dataverse | shared_commondataserviceforapps | CreateRecord |
Note — All connections are referenced as solution connection references; the flow is portable between environments as long as a connection is mapped at import time.
Customization Guide
Almost every realistic variant of this flow can be implemented by changing environment variable values. A few cases require small edits inside the flow definition — those are called out explicitly below.
- Change retention period
- Update the flowlibs_SalesforceJobRetentionDays environment variable value (e.g., set to 30 for 30-day retention).
- Change schedule
- Edit the Recurrence trigger to run weekly, hourly, or on a custom schedule.
- Add notifications
- Insert a Teams or Outlook action after the ForEach loop to send the cleanup summary to admins.
- Filter by operation type
- Modify the Filter Array condition to include item()?['operation'] checks (e.g., only clean up bulk query jobs).
- Connection setup
- Authorize both Salesforce and Microsoft Dataverse connections on first use. Ensure the Salesforce user has permissions to view and delete bulk jobs.
Key Expressions
The flow is intentionally light on Power Fx / WDL gymnastics — the heaviest expressions are the branch-name concatenation and the approval outcome check. They are listed below in the order they appear in the flow.
EXPR.01Cutoff calculation
Subtracts the configured retention-day count from the current UTC time to produce the deletion cutoff.
EXPR.02Filter condition
Keeps only jobs that are completed or failed AND were created before the cutoff timestamp.
EXPR.03Audit log name
Builds the Name column value for each Dataverse audit row.
EXPR.04Null-safe field access
Falls back to 'Unknown' when a job's state field is missing.
Comments
Sign in to join the conversation.
Sign inNo comments yet. Be the first to share your experience with this flow.