Skip to main content

How to Replace a File Without Changing Its Link

Learn how to update or swap an uploaded file on X02 while keeping the exact same URL and folder settings. Includes simple visual dashboard steps and developer REST API references.

By X02 Team · · Tutorial, File Management, API, Productivity, Developer Tools

We've all been there: you upload a screenshot, publish a guide, send out a PDF report, or post an image link to Discord and Twitter—only to notice a glaring typo, an outdated price tag, or an old UI mockup five minutes later.

On traditional image hosts, fixing this is painful. You have to delete the file, re-upload the new version, get a completely new URL, and then manually edit every tweet, forum post, GitHub readme, or document where the old link was shared. If you don't control the place where the link was posted, you're stuck with a broken link.

X02 solves this with In-Place File Replacement. You can swap the underlying file anytime while keeping the exact same public URL, folder placement, and privacy settings.

[!TIP] Quick Navigation:


Part 1: The Easy Guide (For Everyone)

You don't need any technical skills to replace a file. You can do it in two clicks directly from your personal dashboard.

Architecture Diagram

What Happens When You Replace a File?


Step-by-Step: Replacing a File from the Dashboard

Where to Go Direct Link Action
Dashboard Gallery [Hover File Card] Open Dashboard Gallery Click Replace (Swap Icon)
  1. Log in to your account at x02.me and go to your Dashboard.
  2. Find the file you want to update in your Gallery.
  3. Hover over the file card and click the Replace button (or click the file to open the full preview modal and select Replace from the menu).
  4. Select your new file from your computer.
  5. Confirm the swap.

That's it! Once the upload finishes, anyone opening your original link will see the new version (a browser that saved the old one may need a hard refresh, see Still Seeing the Old Image?).

[!TIP] Power-User Shortcut: If you replace files frequently, you can turn on "Bypass Confirmation" in your gallery settings to replace files immediately with a single click.


Common Everyday Use Cases


Still Seeing the Old Image?

Replacing a file changes it on X02 straight away. The old picture can still show up for a while in two places that X02 does not control:

https://x02.me/i/yourfile.png?v=2

X02 ignores everything after the ?, so the link still opens the same file. Change the number each time you replace the file (?v=3, ?v=4, and so on).


Part 2: The Developer & Power User Guide

For developers building automated pipelines, release scripts, or dynamic assets, X02 exposes an in-place replacement endpoint over REST API.


1. How Replacement Operates Under the Hood

When you perform an in-place replacement:

  1. Object Key Persistence: Rather than generating a new random UUID or slug, the backend retrieves the file's existing storage key (r2Key or disk path) and overwrites the binary buffer directly.
  2. Staged Upload: The new file is streamed to a temporary key in storage first, then copied over the live object inside storage. A failed upload never leaves you with a half-replaced file. Replacements are held to the same size and storage limits as uploads, and the same rules about what you may host apply to them.
  3. Database Metadata Delta: The database recalculates the file size, hash (SHA-256), and MIME type, updating your account's storage usage delta while preserving the original upload timestamp.
  4. Cache Control & Edge Purging: Right after the swap, X02 asks Cloudflare to drop its saved copy of the file at every address it is served from (x02.me, your subdomain, your custom domain). The replaced file is stored with public, max-age=0, s-maxage=31536000, must-revalidate, so from then on browsers check back with X02 each time instead of reusing a saved copy. A file that has never been replaced carries max-age=3600, so a browser that opened the old version can keep it for up to an hour, and caches outside X02 (Discord, forums, chat apps) keep their own copies. See Still Seeing the Old Image?.

2. Replacing a File via cURL (Terminal)

Send a PATCH request to /api/user/images/:filename/replace with your API key:

curl -X PATCH "https://x02.me/api/user/images/ABCD123.png/replace" \
  -H "x-api-key: YOUR_X02_API_KEY" \
  -F "file=@/path/to/new-graphic.png"

Successful JSON Response:

{
  "success": true,
  "message": "File replaced successfully",
  "data": {
    "filename": "ABCD123.png",
    "url": "https://x02.me/i/ABCD123.png",
    "mimeType": "image/png",
    "sizeBytes": 184520,
    "oldSizeBytes": 201334,
    "updatedAt": "2026-09-01T16:00:00.000Z"
  }
}

3. Replacing a File in JavaScript / TypeScript (Node.js)

Automate asset replacements in build scripts or continuous integration workflows:

import fs from "fs";

async function replaceHostedFile(filename: string, newFilePath: string, apiKey: string) {
  const formData = new FormData();
  const fileBuffer = fs.readFileSync(newFilePath);
  formData.append("file", new Blob([fileBuffer]), filename);

  const response = await fetch(`https://x02.me/api/user/images/${filename}/replace`, {
    method: "PATCH",
    headers: {
      "x-api-key": apiKey,
      "Accept": "application/json",
    },
    body: formData,
  });

  if (!response.ok) {
    const error = await response.json();
    throw new Error(`Failed to replace file: ${error.error || response.statusText}`);
  }

  const result = await response.json();
  console.log("File successfully replaced at:", result.data.url);
  return result;
}

// Example usage
replaceHostedFile("banner_q3.png", "./assets/banner_q3_final.png", "YOUR_API_KEY")
  .catch(console.error);

4. Replacing a File in Python

A clean Python automation script using requests:

import requests

def replace_file(filename: str, new_file_path: str, api_key: str):
    url = f"https://x02.me/api/user/images/{filename}/replace"
    headers = {
        "x-api-key": api_key,
        "Accept": "application/json"
    }

    with open(new_file_path, "rb") as f:
        files = {"file": f}
        response = requests.patch(url, headers=headers, files=files)

    if response.status_code == 200:
        data = response.json()
        print(f"Success! File replaced at: {data['data']['url']}")
        return data
    else:
        print(f"Error ({response.status_code}):", response.text)
        response.raise_for_status()

# Example usage
replace_file("test-build.zip", "./dist/test-build-v2.zip", "YOUR_API_KEY")

5. Replacing Files from a Shell Script

The same endpoint works from a one-liner, which is handy in a deploy step or a cron job that keeps a hosted asset current:

curl -X PATCH "https://up.x02.me/api/user/images/ABCD123.png/replace"   -H "x-api-key: YOUR_API_KEY"   -F "file=@./updated_screenshot.png"

Best Practices & Tips

Tip Why It Matters
Match Format When Possible While you can swap a .png for another .png, keeping the file extension consistent prevents client-side rendering issues with static image tags.
Check Storage Limits If the replacement file is significantly larger than the old one, ensure your account has sufficient quota to cover the size difference.
Give Caches a Moment X02 clears its own saved copy right after the swap, and that takes a moment to reach every edge location. Browsers (up to an hour) and other sites keep their own copies longer: use a hard refresh (Ctrl + F5 or Cmd + Shift + R) or add ?v=2 to the link.

Next Steps & Related Tutorials

What to learn next Guide Link
Rename files to clean URLs How to Rename Your Uploaded Files
Organize uploads into custom folders How to Organize Files with Folders
Connect ShareX for 1-second captures How to Integrate ShareX with X02
Explore all upload channels How to Upload Files to X02

Read this article on X02