Answers.Body: <p>You need to set the scenario <code>SCENARIO_REPLACE</code> or <code>SCENARIO_CREATE</code> depending what you like to do</p>
  
  <pre class="lang-PHP prettyprint-override"><code>$asset = new \craft\elements\Asset();
  $asset-&gt;tempFilePath = $tmpFilePath;
  
  $asset-&gt;setScenario(\craft\elements\Asset::SCENARIO_REPLACE);
  </code></pre>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2018-09-17T06:21:19.890Z
Answers.LastActivityDate: 2018-09-17T06:21:19.890Z
Answers.UserDisplayName: Robin Schambach
Body: <p>In the CP, when we upload an image with a same name, craft will ask if we wanna keep both or replace.
  So in plugin, is there any way to do the replace? </p>
  
Tags: <plugin-development><assets><image><upload>
Title: Is there any way to replace the asset programatically?
ThreadText: # Is there any way to replace the asset programatically?
  
  (Asked by: username_0 on 2018-09-17)
  
  In the CP, when we upload an image with a same name, craft will ask if we wanna keep both or replace.
  So in plugin, is there any way to do the replace?
  
  **Tags:** plugin-development, assets, image, upload
  
  
  ---
  
  ## 1 answers
  
  ### Answer by username_1 on 2018-09-17. Score: 2
  
  You need to set the scenario `SCENARIO_REPLACE` or `SCENARIO_CREATE` depending what you like to do
  
  ```PHP
  $asset = new \craft\elements\Asset();
  $asset->tempFilePath = $tmpFilePath;
  
  $asset->setScenario(\craft\elements\Asset::SCENARIO_REPLACE);
  
  ```
  
  
  ---
  
  