|
| 1 | +# Cleanup Command |
| 2 | + |
| 3 | +Removes orphaned files from storage that are no longer referenced in the database. |
| 4 | + |
| 5 | +This command is useful when files have been deleted from the database (e.g., through |
| 6 | +foreign key cascades or direct database operations) but the physical files remain in |
| 7 | +storage. |
| 8 | + |
| 9 | +## ⚠️ Safety & Limitations |
| 10 | + |
| 11 | +**BEFORE RUNNING THIS COMMAND:** |
| 12 | + |
| 13 | +- **ALWAYS BACKUP YOUR STORAGE—**file deletions are permanent and cannot be rolled |
| 14 | + back. There is no undo. |
| 15 | +- **TEST WITH `--dry-run` FIRST** - always preview what will be deleted before running |
| 16 | + the actual clean-up. |
| 17 | + |
| 18 | +**Built-in safety:** |
| 19 | + |
| 20 | +- Files younger than `--min-age` (default: 60 minutes) are never deleted to prevent race |
| 21 | + conditions with concurrent uploads |
| 22 | +- Interactive mode requires explicit "yes" confirmation before deleting |
| 23 | + |
| 24 | +**Limitations:** |
| 25 | + |
| 26 | +- Requires repositories that support `createQueryBuilder()`. Custom repositories without |
| 27 | + this method will be skipped with a warning |
| 28 | +- For remote storage backends (S3, Azure, etc.), file timestamps may not be available |
| 29 | + and `--min-age` protection may not work correctly |
| 30 | +- Scripts and CI/CD must explicitly use `--force` or `--dry-run` (interactive mode is |
| 31 | + not available in non-interactive environments) |
| 32 | + |
| 33 | +## Basic usage |
| 34 | + |
| 35 | +```bash |
| 36 | +# Preview what would be deleted |
| 37 | +php bin/console vich:cleanup --dry-run |
| 38 | + |
| 39 | +# Interactive mode (asks for confirmation) |
| 40 | +php bin/console vich:cleanup |
| 41 | + |
| 42 | +# Non-interactive mode for scripts/CI-CD |
| 43 | +php bin/console vich:cleanup --force |
| 44 | +``` |
| 45 | + |
| 46 | +## Options |
| 47 | + |
| 48 | +- `--dry-run`: Preview which files would be deleted without actually deleting them |
| 49 | +- `--force`: Skip confirmation prompt (required for non-interactive execution) |
| 50 | +- `--mapping=MAPPING` (`-m`): Process only a specific mapping instead of all mappings |
| 51 | +- `--batch-size=SIZE` (`-b`): Number of entities to process per batch (default: 1000, |
| 52 | + max: 10000) |
| 53 | +- `--min-age=MINUTES`: Minimum age in minutes for files to be considered orphaned |
| 54 | + (default: 60 minutes) |
| 55 | +- `--verbose` (`-v`): Show detailed progress information including list of all orphaned |
| 56 | + files |
| 57 | + |
| 58 | +## Common examples |
| 59 | + |
| 60 | +```bash |
| 61 | +# Preview cleanup for a specific mapping |
| 62 | +php bin/console vich:cleanup --mapping=product_image --dry-run |
| 63 | + |
| 64 | +# Run cleanup for a specific mapping |
| 65 | +php bin/console vich:cleanup --mapping=product_image --force |
| 66 | + |
| 67 | +# Extra safety: only delete files older than 2 hours |
| 68 | +php bin/console vich:cleanup --min-age=120 --force |
| 69 | + |
| 70 | +# Verbose output with file details |
| 71 | +php bin/console vich:cleanup --dry-run -v |
| 72 | + |
| 73 | +# Memory-constrained environments |
| 74 | +php bin/console vich:cleanup --batch-size=500 --force |
| 75 | + |
| 76 | +# Immediate cleanup (USE WITH CAUTION - may delete files being uploaded) |
| 77 | +php bin/console vich:cleanup --min-age=0 --force |
| 78 | +``` |
| 79 | + |
| 80 | +[Return to commands overview](../commands.md) |
0 commit comments