Answers.Body: <p>You can move them like this</p>
  
  <pre class="lang-PHP prettyprint-override"><code>$asset = Asset::find()-&gt;volume('resourceImages')-&gt;filename($filename)-&gt;one()
  $folder = Craft::$app-&gt;getAssets()-&gt;findFolder(['volumeId' =&gt; X]);
  Craft::$app-&gt;getAssets()-&gt;moveAsset($asset, $folder, $filename);
  </code></pre>
  
  <p>It won't change your relations or anything else in fact all the function does is:</p>
  
  <pre class="lang-PHP prettyprint-override"><code>/**
   * Move or rename an Asset.
   *
   * @param Asset $asset The asset whose file should be renamed
   * @param VolumeFolder $folder The Volume Folder to move the Asset to.
   * @param string $filename The new filename
   * @return bool Whether the asset was renamed successfully
   * @throws AssetLogicException if the asset’s volume is missing
   */
  public function moveAsset(Asset $asset, VolumeFolder $folder, string $filename = ''): bool
  {
      // Set the new combined target location, and save it
      $asset-&gt;newFilename = $filename;
      $asset-&gt;newFolderId = $folder-&gt;id;
      $asset-&gt;setScenario(Asset::SCENARIO_FILEOPS);
  
      return Craft::$app-&gt;getElements()-&gt;saveElement($asset);
  }
  </code></pre>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2018-10-08T20:11:31.937Z
Answers.LastActivityDate: 2018-10-08T20:11:31.937Z
Answers.UserDisplayName: Robin Schambach
Body: <p>Im trying to figure out how to move asset(s) to a different volume. Also if I move it will that affect the entries that are using the asset?</p>
  
  <p>Here's what im using to target the asset:
  <code>$resourceImage = Asset::find()-&gt;volume('resourceImages')-&gt;filename($filename);</code></p>
  
  <p>I need to move it to my <code>productImages</code> volume.</p>
  
  <p>Thanks!</p>
  
Tags: <assets><craft3><element>
Title: API - Update Asset Volume
ThreadText: # API - Update Asset Volume
  
  (Asked by: username_0 on 2018-10-08)
  
  Im trying to figure out how to move asset(s) to a different volume. Also if I move it will that affect the entries that are using the asset?
  
  Here's what im using to target the asset:
  `$resourceImage = Asset::find()->volume('resourceImages')->filename($filename);`
  
  I need to move it to my `productImages` volume.
  
  Thanks!
  
  **Tags:** assets, craft3, element
  
  
  ---
  
  ## 1 answers
  
  ### Answer by username_1 on 2018-10-08. Score: 4 | Accepted answer
  
  You can move them like this
  
  ```PHP
  $asset = Asset::find()->volume('resourceImages')->filename($filename)->one()
  $folder = Craft::$app->getAssets()->findFolder(['volumeId' => X]);
  Craft::$app->getAssets()->moveAsset($asset, $folder, $filename);
  
  ```
  
  It won't change your relations or anything else in fact all the function does is:
  
  ```PHP
  /**
   * Move or rename an Asset.
   *
   * @param Asset $asset The asset whose file should be renamed
   * @param VolumeFolder $folder The Volume Folder to move the Asset to.
   * @param string $filename The new filename
   * @return bool Whether the asset was renamed successfully
   * @throws AssetLogicException if the asset’s volume is missing
   */
  public function moveAsset(Asset $asset, VolumeFolder $folder, string $filename = ''): bool
  {
      // Set the new combined target location, and save it
      $asset->newFilename = $filename;
      $asset->newFolderId = $folder->id;
      $asset->setScenario(Asset::SCENARIO_FILEOPS);
  
      return Craft::$app->getElements()->saveElement($asset);
  }
  
  ```
  
  
  ---
  
  