Deleting files in plugin folder

Hi everyone,

Is it possible to delete a file from the plugin folder?

Yes. Something like:

import fs from "fs"
import { storage } from "uxp"

const emptyDir = (dir: string): void => {
    dir = dir.replace(/[\\/]+$/, "")
    const dirData = fs.readdirSync(dir)

    for (const item of dirData) {
        const entryPath = `${dir}/${item}`

        if (fs.lstatSync(entryPath).isDirectory()) {
            emptyDir(entryPath)
            fs.rmdir(entryPath, () => null)
        } else {
            fs.unlink(entryPath, () => null)
        }
    }
}

const emptyPluginDir = async (): Promise<void> => {
    try {
        emptyDir((await storage.localFileSystem.getPluginFolder()).nativePath)
    } catch (e) {
        console.warn(e)
    }
}

emptyPluginDir()

Probably should be possible with plugin: scheme (ref.) instead of the whole storage.localFileSystem.getPluginFolder(), but didn’t try

You could try directly this

emptyDir("plugin:")

and skip emptyPluginDir()

This one didn’t work I kept getting route not found

I was able to get the files using this but still can’t delete them, I got: the file uses a storage provider that is read-only

Did you maybe forget the await and/or nativePath? I’ve no idea what could give you such an error

I think it’s because of the path scheme, could you show me how the dir path should look like

On Windows I get something like:

C:\Users\MyUser\AppData\Roaming\Adobe\UXP\Plugins\External\plugin.id_0.0.0\

Could you share the exact piece of code you have?

this is what I have now:

const dirData = fs.readdirSync("plugin:");
console.log(dirData);
for (const item of dirData) {
  const entryPath = `plugin:/${item}`;
  console.log(entryPath);
  if (fs.lstatSync(entryPath).isDirectory()) {
    fs.rmdir(entryPath, (err) => console.log(err));
  } else {
    fs.unlink(entryPath, (err) => console.log(err));
  }
}

when I used getPluginFolder() I got the dist folder same with plugin:

Which line exactly gives you route not found in this code?
Also, fs.rmdir(entryPath, (err) => console.log(err)) will always log an error if directory is not empty

Because they point to the same folder

when I pass pluginFolderPath to readdirSync

    const pluginFolder = await fs1.getPluginFolder();
    let pluginFolderPath = pluginFolder.nativePath;
    
    pluginFolderPath = pluginFolderPath.replace(/[\\/]+$/, "");

    const dirData = fs.readdirSync(pluginFolderPath);

but they’re pointing to my build folder while in your case they’re not

Although this shouldn’t be the case, but maybe it would help adding to manifest:

  "requiredPermissions": {
    "localFileSystem": "fullAccess",

They’re pointing to whereever plugins manifest is

It turns out my first implementation of this had a typo in it that’s why I was getting that route not found error I tried with this snippet I rewrote to show you and it worked :man_facepalming:t2:, THANK YOU for the solution!

1 Like