puter.fs.unshare()

Websites Puter Apps Node.js Workers

This method withdraws a user's access to a file or directory.

What an app can share. An app never gets more reach than it was given. It can share its own AppData, and files the user specifically granted it, at up to the level of access it holds itself — so an app with read access can grant read, and nothing more. Files its user owns but never handed to the app stay out of reach, and listShared() shows an app only the shares it can reach. Shares an app creates are attributed to the user and carry issuedByApp, so the owner can tell them apart in getShares() — and those are the only ones an app can list or withdraw on an item. Opening an item to anyone with the link is the owner's own call, never an app's.

Syntax

puter.fs.unshare(path, recipient)
puter.fs.unshare(options)

Parameters

path (String) (required)

The path to the file or directory. If path is not absolute, it will be resolved relative to the app's root directory.

recipient (String | Object) (required)

Whose access to withdraw. A string containing @ is treated as an email address, and any other string as a username.

Pass yourself to leave a share someone else gave you. Pass { team: uid } to withdraw a team's access, or { anyone: true } to stop sharing with anyone with the link — the latter is the owner's call, in person, as opening it was.

options (Object) (optional)

An object with the following properties:

Option Type Description
path String The item. Required when passing options as the only argument, unless paths or uid names it.
paths Array Several items to withdraw in one call.
uid String The item, by UID. Can be used instead of path.
recipient String | Object | Array Whose access to withdraw. An array withdraws several recipients in one call.
recipients String | Object | Array Alias for recipient.

Return value

A Promise that resolves to an object with the following properties:

Property Type Description
revoked Number How many grants were actually removed. It is 0 when there was nothing to withdraw, which is not an error.
failed Array The recipient/item pairs that were refused. Empty when none were.

Each entry of failed has the following properties:

Property Type Description
recipient String The recipient as the call named it. This is a username, an email address, a team's uid or handle, or anyone.
path String The item's absolute path, when the call named it by path. A relative path comes back resolved against the app's root directory, and a path that starts with ~ comes back under the user's home directory, such as /username/report.txt.
uid String The item's UID as the call passed it, when the call named it by UID.
message String Why the pair was refused.
code String One of the codes below.

Each recipient/item pair is withdrawn on its own. With several items or recipients, every combination is one pair. A refused pair does not stop the others. The promise resolves, revoked counts what the others removed, and failed lists the pairs that were refused.

Errors

The promise rejects only when every pair was refused, and at least one of them for a reason other than the recipient or the item being gone. A pair refused with user_does_not_exist or subject_does_not_exist has nothing left to withdraw, so a call made only of those resolves with revoked: 0 and the pairs listed in failed.

That rejection carries { message, code, failed }. The message and code come from the first refused pair that names a code other than those two. The failed array lists every refused pair, with the same properties as in the return value.

A request that fails as a whole also rejects, with the server's error and no failed. This happens when a call names no recipient or no item, or names a team by both team and teamHandle (bad_request), when it names more recipients or items than one call allows (too_many_recipients, too_many_items), and when it's over the rate limit.

The codes a refused pair carries, in failed or on a rejection:

code Meaning
team_share You tried to leave an item you hold only through a team. That access is the team's grant, not yours to drop; ask whoever shared it with the team, or leave the team. If you also hold a share of your own, leaving drops that one and succeeds.
forbidden You may not withdraw shares of this item, or you tried to close its "anyone with the link" share, which only the owner may do, in person rather than through an app.
subject_does_not_exist No such item, or you cannot see it. It is listed in failed and never causes a rejection on its own.
user_does_not_exist The username has no account. It is listed in failed and never causes a rejection on its own.
team_not_found No team has that uid.
cannot_revoke_owner The recipient owns the item.
unshare_failed No refused pair named a code, as when the server could not complete the request.

Who can withdraw what

  • The item's owner can withdraw any share of it, whoever granted it.
  • Anyone else can withdraw the shares they granted.
  • An app acting for you withdraws only the shares that app issued, whatever authority you have over the rest: your own shares, another app's and a delegate's all report revoked: 0 to it. Leaving a share yourself still works through an app, since that is your own access to drop.
  • Anyone can withdraw their own access, whoever granted it — except access held only through a team, which is the team's (see team_share above).

An item's owner cannot be removed from their own item.

Withdrawing someone's access also withdraws whatever they re-shared of that item — unless their authority to grant does not rest on what you took back. Someone who still holds manage here from another person, from a team, or from a folder above keeps what they granted; it was never yours to withdraw.

Passing an email address that was invited but has not yet joined cancels the invitation. Nothing was granted, so nothing is revoked from anyone — the pending share simply stops waiting.

Examples

Stop sharing a file

<html>
<body>
    <script src="https://js.puter.com/v2/"></script>
    <script>
        (async () => {
            await puter.fs.write('report.txt', 'Quarterly numbers');
            await puter.fs.share('report.txt', 'friend@example.com');

            const result = await puter.fs.unshare('report.txt', 'friend@example.com');
            puter.print(`Removed ${result.revoked} share(s)<br>`);
        })()
    </script>
</body>
</html>

Withdraw several items and see which were refused

const { revoked, failed } = await puter.fs.unshare({
    paths: ['report.txt', 'budget.txt', 'notes.txt'],
    recipient: 'friend',
});
console.log(`Removed ${revoked} share(s)`);
for (const pair of failed) {
    console.log(`${pair.path} was not withdrawn from ${pair.recipient}: ${pair.message} (${pair.code})`);
}

Leave a share someone gave you

const me = await puter.auth.getUser();
await puter.fs.unshare('/alice/report.txt', me.username);