Skip to content

Commit 45c3a9a

Browse files
authored
Merge pull request #1553 from endelwar/cleanup-command
Add cleanup command to remove orphaned uploaded files
2 parents bd3ff3d + 5cdb4c1 commit 45c3a9a

18 files changed

Lines changed: 2222 additions & 6 deletions

config/command.xml

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,15 @@
2121
<tag name="console.command" command="vich:mapping:list-classes" />
2222
</service>
2323

24+
<service id="vich_uploader.command.cleanup" class="Vich\UploaderBundle\Command\CleanupCommand" public="false">
25+
<argument type="service" id="vich_uploader.storage" />
26+
<argument type="service" id="vich_uploader.property_mapping_factory" />
27+
<argument type="service" id="vich_uploader.metadata_reader" />
28+
<argument>[]</argument><!-- manager registries, filled by compiler pass -->
29+
<argument>%vich_uploader.mappings%</argument>
30+
<tag name="console.command" command="vich:cleanup" />
31+
</service>
32+
2433
</services>
2534

2635
</container>

docs/command/cleanup.md

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
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)

docs/commands.md

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
## Debug class
44

5-
Show entity file mapping metadata for class.
5+
Show entity file mapping metadata for a class.
66

77
```bash
88
php bin/console vich:mapping:debug-class App\\Entity\\Foo
@@ -24,4 +24,15 @@ Searches for uploadable classes.
2424
php bin/console vich:mapping:list-classes
2525
```
2626

27+
## Cleanup orphaned files
28+
29+
Removes orphaned files from storage that are no longer referenced in the database.
30+
31+
```bash
32+
php bin/console vich:cleanup --dry-run
33+
```
34+
35+
For detailed documentation, options, examples, and safety considerations, see the
36+
[cleanup command documentation](command/cleanup.md).
37+
2738
[Return to the index](index.md)

phpstan-baseline.neon

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,3 +96,9 @@ parameters:
9696
count: 4
9797
path: tests/TestCase.php
9898

99+
-
100+
message: '#^Return type of call to method PHPUnit\\Framework\\TestCase\:\:createMock\(\) contains unresolvable type\.$#'
101+
identifier: method.unresolvableReturnType
102+
count: 17
103+
path: tests/Command/CleanupCommandTest.php
104+

0 commit comments

Comments
 (0)