Silverstripe auto-delete $has_one/$has_many relationships

Installs: 13

Dependents: 0

Suggesters: 0

Security: 0

Stars: 0

Watchers: 3

Forks: 0

Open Issues: 0


0.0.1-beta2 2021-07-13 22:56 UTC

This package is auto-updated.

Last update: 2024-04-14 05:20:00 UTC



An extension to add functionality to DataObjects to:

  • Delete unused $has_one relationships when the $has_one changes (eg: a new file is uploaded). This functionality will not run directly from any Versioned DataObjects.
  • Delete unused $has_one & $has_many relationships when a DataObject is deleted.

The extension will only delete objects if they are not in use elsewhere.

All DataObjects get two additional new functions which you can integrate into other things:

  • inUse() - used to detect whether an object is in use somewhere. Note: It only detects if it is used from non-versioned and published relationships. Draft & modified states are not detected!
  • deleteIfUnused() - used to delete a DataObject if not in use somewhere. If the object being deleted has a deleteFile() method, then that will be run too (ie: file deletion).


In your defined DataObject, add a private static $auto_delete = [....]; variable, defining which related $has_one and $has_many DataObjects to automatically delete, for example:

class Slide extends DataObject {
    private static $db = [
        'Title' => 'Varchar(50)',

    private static $has_one = [
        'Image' => Image::class,

     * $has_one & $has_many auto-delete definitions
     * @var array
    private static $auto_delete = [

This will automatically delete the Image (if unused elsewhere) if the Slide is deleted, or delete the original Image if a new Image is uploaded.


Auto-delete cannot recursively delete a $has_many objects, as it will only delete if the object is no longer linked to any other DataObject. If you wish to recursively delete objects then you should do that manually via onBeforeDelete() or 'onAfterDelete().


  • silverstripe/assets ^1.7.0 (Silverstripe 4)

Installation via Composer

You can install it via composer with

composer require axllent/silverstripe-auto-delete