Answers.Body: <blockquote>
    <p>Or is this not a feature because an asset can be shared between entries? </p>
  </blockquote>
  
  <p>Bingo.</p>
  
  <p>A plugin is way to go if that's the behavior you're looking for.</p>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2015-02-17T20:31:04.740Z
Answers.LastActivityDate: 2015-02-17T20:31:04.740Z
Answers.UserDisplayName: Brad Bell
Answers.Body: <p>This is how I did it:</p>
  
  <ol>
  <li><p>In my custom plugin, in its <code>init</code> function, I use the following statement to hook into the before delete event: </p>
  
  <pre><code>craft()-&gt;on('elements.onBeforeDeleteElements', array($this, 'beforeDeleteElements'));
  </code></pre></li>
  <li><p>In the same custom plugin, in the <code>beforeDeleteElements</code> function mentioned in step 1, I examine the passed in event, and determine if the entry type being deleted is the entry type I am interested in, if so, I use the following function to find the related assets and then delete them:</p>
  
  <pre><code>protected function deleteRelatedImages(EntryModel $entry)
  {
      $criteria            = craft()-&gt;elements-&gt;getCriteria(ElementType::Asset);
      $criteria-&gt;relatedTo = $entry;
      $assets              = $criteria-&gt;find();
  
      if (!$assets) {
          return;
      }
  
      foreach ($assets as $asset) {
          $sourceType = $asset-&gt;getSource()-&gt;getSourceType();
          $sourceType-&gt;deleteFile($asset);
      }
  }
  </code></pre></li>
  </ol>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2016-06-30T03:16:37.757Z
Answers.LastActivityDate: 2016-06-30T03:16:37.757Z
Answers.UserDisplayName: Jay Haase
Body: <p>Is there a way to delete an entry's assets when the entry is deleted? We're going to end up with a lot of orphaned assets.</p>
  
  <p>Or is this not a feature because an asset can be shared between entries? Thus requiring me to write a plugin to tie into the onDeleteEntry event (<a href="https://craftcms.stackexchange.com/questions/797/where-and-how-should-you-listen-for-entry-delete-in-plugin">Where and how should you listen for entry delete in plugin?</a>)?</p>
  
Tags: <entries><assets>
Title: Delete entry assets?
ThreadText: # Delete entry assets?
  
  (Asked by: username_0 on 2015-02-17)
  
  Is there a way to delete an entry's assets when the entry is deleted? We're going to end up with a lot of orphaned assets.
  
  Or is this not a feature because an asset can be shared between entries? Thus requiring me to write a plugin to tie into the onDeleteEntry event ([Where and how should you listen for entry delete in plugin?](https://craftcms.stackexchange.com/questions/797/where-and-how-should-you-listen-for-entry-delete-in-plugin))?
  
  **Tags:** entries, assets
  
  
  ---
  
  ## 2 answers
  
  ### Answer by username_1 on 2015-02-17. Score: 2 | Accepted answer
  
  > Or is this not a feature because an asset can be shared between entries?
  
  Bingo.
  
  A plugin is way to go if that's the behavior you're looking for.
  
  
  ---
  
  ### Answer by username_2 on 2016-06-30. Score: 5
  
  This is how I did it:
  
  1. In my custom plugin, in its `init` function, I use the following statement to hook into the before delete event:
  
     ```
     craft()->on('elements.onBeforeDeleteElements', array($this, 'beforeDeleteElements'));
  
     ```
  2. In the same custom plugin, in the `beforeDeleteElements` function mentioned in step 1, I examine the passed in event, and determine if the entry type being deleted is the entry type I am interested in, if so, I use the following function to find the related assets and then delete them:
  
     ```
     protected function deleteRelatedImages(EntryModel $entry)
     {
         $criteria            = craft()->elements->getCriteria(ElementType::Asset);
         $criteria->relatedTo = $entry;
         $assets              = $criteria->find();
  
         if (!$assets) {
             return;
         }
  
         foreach ($assets as $asset) {
             $sourceType = $asset->getSource()->getSourceType();
             $sourceType->deleteFile($asset);
         }
     }
  
     ```
  
  
  ---
  
  