<commit_before>---
owner_slack: "#govuk-2ndline"
title: Replace an asset's file
section: Assets
layout: manual_layout
parent: "/manual.html"
last_reviewed_on: 2020-01-23
review_in: 1 month
---

If you need to replace the file of an existing attachment without
changing the URL, follow these steps:


0. Copy the new file from your computer to the server:

    ```
    scp filename.ext backend-1.production:/tmp/filename.ext
    ```

0. `ssh backend-1.production`

0. `govuk_app_console asset-manager`

0. Find the asset:

    ```
    asset = Asset.find("asset-id-from-url")` # e.g. `57a9c52b40f0b608a700000a`
    # or for a Whitehall asset:
    asset = WhitehallAsset.find_by(legacy_url_path: '/government/uploads/system/uploads/attachment_data/file/id/path.ext')`
    ````

0. Check the asset is what you think it is.

0. Replace the file:

    ```
    asset.file = Pathname.new("/tmp/filename.ext").open
    asset.save!
    ```<commit_msg>Review "Replace an asset's file"<commit_after>---
owner_slack: "#govuk-2ndline"
title: Replace an asset's file
section: Assets
layout: manual_layout
parent: "/manual.html"
last_reviewed_on: 2020-03-17
review_in: 3 months
---

If you need to replace the file of an existing attachment without
changing the URL, follow these steps:


0. Copy the new file from your computer to a `backend` server:

    ```
    scp filename.ext <hostname>:/tmp/filename.ext
    ```

0. `ssh <hostname>`

0. `govuk_app_console asset-manager`

0. Find the asset:

    ```
    asset = Asset.find("asset-id-from-url")` # e.g. `57a9c52b40f0b608a700000a`
    # or for a Whitehall asset:
    asset = WhitehallAsset.find_by(legacy_url_path: '/government/uploads/system/uploads/attachment_data/file/id/path.ext')`
    ````

0. Check the asset is what you think it is.

0. Replace the file:

    ```
    asset.file = Pathname.new("/tmp/filename.ext").open
    asset.save!
    ```
{'fraction_non_alphanumeric': 0.12857142857142856, 'fraction_numerical': 0.03597883597883598, 'mean_word_length': 3.481042654028436, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '523958', 'n_tokens_mistral': 703, 'n_tokens_neox': 639, 'n_words': 185}