> For the complete documentation index, see [llms.txt](https://node.spflow.com/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://node.spflow.com/packages/sppurge.md).

# sppurge

![NPM](https://camo.githubusercontent.com/b3d9ba3f275a8b86c818df0de3a8cf26502cb282/68747470733a2f2f6e6f6465692e636f2f6e706d2f737070757267652e706e673f6d696e693d7472756526646f776e6c6f6164733d7472756526646f776e6c6f616452616e6b3d747275652673746172733d74727565)

[![npm version](https://camo.githubusercontent.com/fe5c3c5e999dd19d59fc3fd9dbfde3ffd7e8f610/68747470733a2f2f62616467652e667572792e696f2f6a732f737070757267652e737667)](https://badge.fury.io/js/sppurge) [![Downloads](https://camo.githubusercontent.com/9620d783f89feb7ca2a5a521373c5849e20270cc/68747470733a2f2f696d672e736869656c64732e696f2f6e706d2f646d2f737070757267652e737667)](https://www.npmjs.com/package/sppurge) [![Build Status](https://camo.githubusercontent.com/51294eba5efd7724e652bdbe966a0a2a45b56db0/68747470733a2f2f6b6f6c7479616b6f762e76697375616c73747564696f2e636f6d2f53504e6f64652f5f617069732f6275696c642f7374617475732f737070757267653f6272616e63684e616d653d6d6173746572)](https://camo.githubusercontent.com/51294eba5efd7724e652bdbe966a0a2a45b56db0/68747470733a2f2f6b6f6c7479616b6f762e76697375616c73747564696f2e636f6d2f53504e6f64652f5f617069732f6275696c642f7374617475732f737070757267653f6272616e63684e616d653d6d6173746572) [![Gitter chat](https://camo.githubusercontent.com/20d7543bc8280bf8134b686c46c7b7e2c0a467fd/68747470733a2f2f6261646765732e6769747465722e696d2f67697474657248512f6769747465722e706e67)](https://gitter.im/sharepoint-node/Lobby)

Node.js module for files deletion from SharePoint document libraries.

### Supported SharePoint versions

* SharePoint Online
* SharePoint On-Prem (2019, 2016, 2013)

### How to use

#### Install

```
npm install sppurge
```

#### Usage

```typescript
const sppurge = require('sppurge').default;

const context = {/*...*/};
const options = {/*...*/};

sppurge(context, options)
  .then(successHandler)
  .catch(errorHandler);
```

**Arguments**

**Context**

* `siteUrl` - SharePoint site (SPWeb) url \[string, **required**]
* `creds`
  * `username` - user name for SP authentication \[string, optional in case of some auth methods]
  * `password` - password \[string, optional in case of some auth methods]

**Additional authentication options:**

Since communication module (sp-request), which is used in sppurge, had received additional SharePoint authentication methods, they are also supported in sppurge.

For more information please check node-sp-auth [credential options](https://github.com/s-KaiNet/node-sp-auth#params) and [wiki pages](https://github.com/s-KaiNet/node-sp-auth/wiki).

**Options**

* `folder` - relative folder in SharePoint to concat with filePath \[string, optional, default: \`\` (empty string)]
* `filePath` - relative file path, with extention \[string, required in general, optional if `localFilePath` and `localBasePath` are both provided]
* `localFilePath` - local full path to file \[string, optional]
* `localBasePath` - relative folder base path within project directory \[string, optional]

Result file path is formed based on the following rule:

* `siteUrl` + `folder` + `filePath`
* If `filePath` is empty, then:
  * `filePath` = path.resolve(localFilePath).replace(path.resolve(localBasePath), '')

**successHandler**

Callback gets called upon successful file deletion.

**errorHandler**

Callback gets executed in case of exception inside `sppurge`. Accepts error object as first argument for callback.

#### Basic usage example (delete a single file)

```typescript
const sppurge = require('sppurge').default;

const context = { /* auth context */ };

const options = {
  folder: '/_catalogs/masterpage/spf/module_name',
  filePath: '/scripts/dummy-file.js'
};

sppurge(context, options)
  .then(deletionResults => {
    console.log('A file has been deleted');
  })
  .catch(err => {
    console.log('Core error has happened', err);
  });
```

#### Basic usage example (delete a folder)

```typescript
const { Delete } = require('sppurge');

const context = { /* auth context */ };
const sppurge = new Delete();

sppurge.deleteFolder(context, '/sites/site/folder/repative/path')
  .then(deletionResults => {
    console.log('A folder has been deleted');
  })
  .catch(err => {
    console.log('Core error has happened', err);
  });
```

#### Within Gulp task

```typescript
const gulp = require('gulp');
const watch = require('gulp-watch');      // Allows more than gulp.watch, is recommended
const spsave = require('gulp-spsave');    // Optional SPSave, but what is the reason to use SPPurge without SPSave?
const sppurge = require('sppurge').default;
const path = require('path');

const config = require('./gulp.config'); // Getting settings for SPPurge and SPSave

gulp.task('watch-assets', () => {
  return watch(config.watch.assets, function (event) {
    // Base local folder path, e.g. 'src' from which
    // project's files are mapped to SharePoint folder
    var watchBase = config.watch.base;

    // When file is deleted event value is "unlink"
    if (event.event === 'unlink') {
      var sppurgeOptions = {
        folder: config.sppurge.options.spRootFolder,
        filePath: path.resolve(event.path).replace(path.resolve(watchBase), '')
      };
      // OR:
      // const sppurgeOptions = {
      //   folder: config.sppurge.options.spRootFolder,
      //   localFilePath: event.path,
      //   localBasePath: watchBase
      // };
      sppurge(config.sppurge.context, sppurgeOptions)
        .then(res => console.log(`File has been deleted: ${res}`))
        .catch(err => console.log('Error', err.message));
    } else {
      // Saving files to SharePoint
      gulp.src(event.path, {
        base: watchBase
      }).pipe(
        spsave(
          // SPSave's core options, see more in spsave documentation
          config.spsave.coreOptions,
          // node-sp-auth / spsave credential object
          config.spsave.creds
        )
      );
    }
  });
});
```

#### Create React App usage scenario

> Delete JS's build folder then upload all files from/build folder

One of the architectural decision in CRA is using hashes as a part of assets filenames. This allows avoiding issues related to browser cache. However, it can be challenging in term of deployment to SharePoint assets folders, as all filenames are different on each build. The further sample shows a simple use case approach of deleting files based on folder and name pattern.

```typescript
const { AuthConfig } = require('node-sp-auth-config');
const sppurge = require('sppurge').default;
const spsave = require('spsave').spsave;

// client-side project's assets destination folder
const targetFolder = '_catalogs/masterpage/assets/cra-project';

const authConfig = new AuthConfig({
  configPath: './config/private.json',
  encryptPassword: true,
  saveConfigOnDisk: true
});

authConfig.getContext().then(({ siteUrl, authOptions: creds }) => {

  const deleteOptions = {
    folder: `${targetFolder}/static/js`,
    fileRegExp: new RegExp('(.*)/(.*)\.(js|map)', 'i'), // include .js, .map to delete
    // filePath: 'SiteAssets/trash.txt' // for single file deletion
  };

  const spsaveCoreOptions = {
    siteUrl,
    notification: true,
    checkin: true,
    checkinType: 2 // 0=minor, 1=major, 2=overwrite
  };

  const spsaveFileOptions = {
    glob: [ 'build/**/*.*' ],
    base: 'build',
    folder: targetFolder
  };

  return sppurge({ siteUrl, creds }, deleteOptions)
    .then(_ => console.log('=== Files Deleted ==='));
    .then(_ => spsave(spsaveCoreOptions, creds, spsaveFileOptions))
    .then(_ => console.log('=== Files Uploaded ==='));

}).catch(err => console.log(err.message));
```

#### Passwords storage

To eliminate any local password storing if preferable to use any two-way hashing technique, like [cpass](https://github.com/koltyakov/cpass).
