A comprehensive Python tool for cleaning up old Flow versions in Salesforce orgs. This tool supports both interactive single-org processing and automated batch processing across multiple orgs.
- Interactive & Batch Modes: Process single orgs interactively or multiple orgs via configuration file
- Automatic OAuth Authentication: Browser-based authentication with local callback server
- Configurable Ports: Use default port 8080 or specify custom ports for OAuth callbacks
- Production Safety: Special confirmation prompts and detection for production instances
- Flexible Cleanup Options:
- Clean up all old Flow versions (not latest and not active)
- Clean up specific Flow versions by name
- Bulk Delete: Uses Salesforce Composite API for efficient bulk deletion
- Comprehensive Logging: Detailed audit trails with masked sensitive data
- Deletion Lists: Save what will be deleted before confirmation
- Silent Mode: Use stored credentials for automated runs
- Multi-Org Support: Process multiple Salesforce orgs in a single batch operation
flow_cleanup.py
- Main script (supports both interactive and batch modes)config_example.json
- Example configuration file for batch processingrequirements.txt
- Python dependenciesREADME.md
- This documentation
python flow_cleanup.py
- Create a configuration file based on
config_example.json
- Run the script and select configuration file mode
- Specify your configuration file path
The tool uses port 8080 by default for OAuth callbacks. You can:
- Use default: Press Enter when prompted for port (interactive mode)
- Custom port: Specify any port 1024-65535
- Batch mode: Configure
callback_port
in your JSON file
Important: If using a custom port, update your Salesforce Connected App callback URL to http://localhost:[PORT]/callback
- Python 3.7+
- Salesforce org with appropriate permissions
- Connected App configured in Salesforce
-
Install dependencies:
pip install -r requirements.txt
-
Configure Connected App in Salesforce:
- Go to Setup → App Manager → New External Client App
- Enable OAuth Settings
- Set Callback URL to:
http://localhost:8080/callback
(default) orhttp://localhost:[PORT]/callback
(if using custom port) - Add OAuth Scopes:
Access and manage your data (api)
,Perform requests on your behalf at any time (refresh_token, offline_access)
- Enable PKCE (Proof Key for Code Exchange)
- Note the Consumer Key (Client ID)
Run the script:
python flow_cleanup.py
The script will:
- Ask if you want to use a configuration file or interactive mode
- Ask what type of cleanup you want to perform
- If specific flows: Ask for Flow API names to clean up
- Ask for your Salesforce instance URL
- Ask for callback port (default 8080)
- Start a local server and open your browser for authentication
- Automatically receive the OAuth callback
- Check if it's a production instance and require confirmation
- Query for Flow versions to delete
- Show you what will be deleted and ask for confirmation
- Perform the bulk deletion and report results
Create a configuration file (see config_example.json
):
{
"orgs": [
{
"instance": "https://yourorg.sandbox.my.salesforce.com",
"client_id": "your_client_id",
"client_secret": "your_client_secret",
"cleanup_type": "1",
"flow_names": [],
"skip_production_check": false,
"auto_confirm_production": false,
"callback_port": 8080
}
]
}
Run the script and select configuration file mode:
python flow_cleanup.py
Field | Required | Valid Options |
---|---|---|
instance |
Yes | Salesforce instance URL (e.g., https://myorg.my.salesforce.com ) |
client_id |
Yes | Connected App Consumer Key (15+ character string) |
client_secret |
No | Connected App Consumer Secret (if required) |
cleanup_type |
No | "1" (all old versions) or "2" (specific flows) |
flow_names |
No | Array of Flow API names (e.g., ["MyFlow", "AccountFlow"] ) |
skip_production_check |
No | true or false (default: false ) |
auto_confirm_production |
No | true or false (default: false ) |
callback_port |
No | Integer 1024-65535 (default: 8080 ) |
When running in interactive mode, you'll be prompted for a callback port:
- Default: 8080 (press Enter to use default)
- Custom: Enter any port between 1024-65535
- Important: If using a custom port, update your Salesforce Connected App callback URL to
http://localhost:[PORT]/callback
Configure the port in your JSON configuration file:
{
"orgs": [
{
"instance": "https://yourorg.sandbox.my.salesforce.com",
"client_id": "your_client_id",
"callback_port": 8081
}
]
}
If port 8080 is already in use:
- macOS/Linux:
lsof -i :8080
to see what's using it - Windows:
netstat -ano | findstr :8080
to see what's using it - Solution: Use a different port and update your Connected App
- Production Detection: Queries Organization.IsSandbox to accurately detect production instances
- Confirmation Prompts: Multiple confirmation steps before deletion
- Detailed Preview: Shows exactly what will be deleted before proceeding
- Audit Trails: Comprehensive logging with masked sensitive data
- Error Handling: Graceful handling of authentication and API errors
- Port Conflict Detection: Clear error messages and troubleshooting for port conflicts
- Batch Safety: Individual org success/failure tracking with comprehensive summaries
- Log files:
flow_cleanup_YYYYMMDD_HHMMSS.log
- Complete audit trail with masked sensitive data - Deletion lists:
flows_to_delete_YYYYMMDD_HHMMSS.json
- JSON file with flows to be deleted - Configuration files: Custom JSON files for batch processing with per-org settings
- All sensitive data (client IDs, secrets, auth codes) are masked in logs
- No credentials are stored in plain text
- Comprehensive audit trails for compliance
- Production safety checks and confirmations
- Invalid client credentials: Check your Connected App Consumer Key and Secret
- Callback URL mismatch: Ensure your Connected App callback URL matches the port you're using
- PKCE errors: Make sure PKCE is enabled in your Connected App
- Port conflicts: Use a different port if 8080 is already in use
- If you get "Port 8080 is already in use", close any applications using that port
- Common applications that use port 8080: web servers, development servers, other OAuth tools
- You can check what's using the port with:
lsof -i :8080
(macOS/Linux) ornetstat -ano | findstr :8080
(Windows)
- MALFORMED_QUERY: The script uses correct SOQL syntax for the Tooling API
- Permission errors: Ensure your user has appropriate permissions to query Flow objects
- API version: The script uses API version 60.0, which supports all current Flow features
- Configuration validation: Check your JSON syntax and required fields
- Individual org failures: The script continues processing other orgs if one fails
- Production confirmations: Set
auto_confirm_production: true
to skip manual confirmations
For issues or questions, refer to the troubleshooting section above or check the generated log files for detailed error information.