Hi @Magicr,
If you are using AEM 6.5 you can consider to use below tool from ACS Commons, so you may not need to implement anything custom.
https://adobe-consulting-services.github.io/acs-aem-commons/features/package-garbage-collector/index.html
However if the implementation is still the key, you ca...