Add gc_inspect command to diagnose garbage collection status#2
Merged
Conversation
SupraSummus
force-pushed
the
claude/vibrant-allen-r5lo5l
branch
2 times, most recently
from
June 16, 2026 10:27
d473b6c to
d30042c
Compare
Adds a read-only gc_inspect management command that explains why a
single object is or is not garbage collected, without deleting
anything:
python manage.py gc_inspect myapp.Attachment 42
It reports one of: object not found, excluded by filter (not currently
a candidate), referenced by N other objects (each blocking foreign key
listed), or unreferenced and collectable. The logic mirrors the gc
command's deletion decisions (filter, then foreign-key references,
honoring ignored_referencing_fields) so the reported status matches
what an actual run would do.
Shared helpers used by both commands (get_combined_config,
find_fk_fields) are factored into django_gc.core; each command keeps
its own private logic.
https://claude.ai/code/session_016DhEphjmyfNcAX38vVbjfk
SupraSummus
force-pushed
the
claude/vibrant-allen-r5lo5l
branch
from
June 16, 2026 10:28
d30042c to
695a68f
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
This PR adds a new
gc_inspectmanagement command that allows users to diagnose why a specific object is or isn't being garbage collected, without actually deleting anything. It also refactors the config loading logic into a reusable function.Key Changes
New
gc_inspectcommand (django_gc/management/commands/gc_inspect.py): A read-only diagnostic tool that reports the garbage collection status of a single object by model label and primary key. Reports one of four statuses: not found, excluded by filter, referenced by other objects, or collectable.New
InspectResultdataclass: Encapsulates the result of inspecting an object, including status, message, and list of referencing objects.New
inspect_object()function: Core logic that mirrors the decisions made byprocess_batch()(filter evaluation and foreign key reference checking) without deleting anything. This allows explaining why a specific row is or isn't collected.Refactored
get_combined_config()function: Extracted the config merging and collision detection logic from thegccommand'shandle()method into a standalone function for reuse bygc_inspect.Updated README: Added documentation for the new
gc_inspectcommand with usage examples and explanation of the four possible statuses.Comprehensive test coverage: Added tests for all
inspect_object()scenarios (not found, collectable, referenced, excluded by filter, ignored fields) and the newgc_inspectcommand.Implementation Details
inspect_object()function reuses the existingfind_fk_fields()utility to identify foreign key references, ensuring consistency with the main garbage collection logic.app_label.ModelName.field_name (pk=...)for clarity.ignored_referencing_fieldsconfiguration option.https://claude.ai/code/session_016DhEphjmyfNcAX38vVbjfk