<p>Try using <a href="https://docs.craftcms.com/api/v3/craft-services-assets.html#method-replaceassetfile" rel="nofollow noreferrer">replaceAssetFile()</a>, something like this (untested):</p>
<pre><code>$rename = Craft::$app-&gt;assets-&gt;replaceAssetFile($asset-&gt;id,$asset-&gt;getFolder(),$newFileName);
</code></pre>

CC BY-SA 4.0
2021-03-31T07:36:03.943Z
Thanx for your reply, unfortunately same behavior.
Maarten Heideman
CC BY-SA 4.0
2021-03-26T17:31:43.187Z
2021-03-26T17:31:43.187Z
Tammy Shipps
<p>How to remove the date behind a asset filename or prevent add a date behind the filename? I'll have a plugin that's modifies an existing pdf with SETA SIGN. To do so I'm copy the current asset, modify it in the temp folder, unlink original, copy modified file and unlink the temp file. I tried to rename the file (with $rename) but still the date is added behind the filename. I'll found something with <code>avoidFilenameConflicts</code> but as far as I'll see this is for new assets not for moving or renaming assets.</p>
<pre><code>$assetFilePath = $asset-&gt;getVolume()-&gt;getRootPath() . DIRECTORY_SEPARATOR . $asset-&gt;getPath();
....
        unlink($assetFilePath);
        copy($tempPath . DIRECTORY_SEPARATOR . $asset-&gt;filename, $assetFilePath);
        unlink($tempPath . DIRECTORY_SEPARATOR . $asset-&gt;filename);

...
$newFileName = 'bla.pdf';
$rename = Craft::$app-&gt;assets-&gt;moveAsset($asset,$asset-&gt;getFolder(),$newFileName);
</code></pre>
<p><code>bla.pdf</code> becomes <code>bla_2021-03-26-124307.pdf</code> but I'll want just 'bla.pdf'.</p>

<assets><remove>
Craft adds unwanted date to filename
# Craft adds unwanted date to filename

(Asked by: username_0 on 2021-03-26)

How to remove the date behind a asset filename or prevent add a date behind the filename? I'll have a plugin that's modifies an existing pdf with SETA SIGN. To do so I'm copy the current asset, modify it in the temp folder, unlink original, copy modified file and unlink the temp file. I tried to rename the file (with $rename) but still the date is added behind the filename. I'll found something with avoidFilenameConflicts but as far as I'll see this is for new assets not for moving or renaming assets.
$assetFilePath = $asset->getVolume()->getRootPath() . DIRECTORY_SEPARATOR . $asset->getPath();
....
        unlink($assetFilePath);
        copy($tempPath . DIRECTORY_SEPARATOR . $asset->filename, $assetFilePath);
        unlink($tempPath . DIRECTORY_SEPARATOR . $asset->filename);

...
$newFileName = 'bla.pdf';
$rename = Craft::$app->assets->moveAsset($asset,$asset->getFolder(),$newFileName);

bla.pdf becomes bla_2021-03-26-124307.pdf but I'll want just 'bla.pdf'.

**Tags:** assets, remove


---

## 1 answers

### Answer by username_1 on 2021-03-26. Score: 1

Try using replaceAssetFile(), something like this (untested):
$rename = Craft::$app->assets->replaceAssetFile($asset->id,$asset->getFolder(),$newFileName);

> **Comments:**
> **username_0**: Thanx for your reply, unfortunately same behavior.  

---

