Protect certain entries from being deleted.
A plugin for Strapi CMS that protects certain entries from being deleted.
yarn add strapi-plugin-do-not-delete@latest
Don't forget to restart or rebuild your Strapi app when installing a new plugin.
property | type (default) | description |
---|---|---|
contentTypes | object (null ) | A config object that describes protection rules for content types. |
contentTypes
A config object that describes protection rules for content types using the model UID and an array of comparators.
The object keys for contentTypes
should be a valid model UID and the value should be an array or arrays which describe the comparison rules.
Below, the comparison rule is querying if the slug
value for Page
is equal to home
, and if this condition is true, the delete operation is cancelled.
1// ./config/plugins.js`
2'use strict';
3
4module.exports = {
5 'do-not-delete': {
6 config: {
7 contentTypes: {
8 'api::page.page': [
9 ['slug', 'is', 'home'],
10 ],
11 },
12 },
13 },
14};
The items in each comparison array must follow the format:
1['attribute', 'comparator', 'value or pattern']
Comparator | Description |
---|---|
is | Strict equality with === |
isNot | Strict inequality with !== |
in | Does the value array include attribute ? |
notIn | Does the value array not include attribute ? |
has | Does the attribute contain value ? |
hasNot | Does the attribute not contain value ? |
lt | Is attribute less than value ? |
lte | Is attribute less than or equal to value ? |
gt | Is attribute greater than value ? |
gte | Is attribute greater than or equal to value ? |
between | Does attribute fall between the pair of values ? |
after | Does attribute occur after the value date? |
before | Does attribute occur before the value date? |
day | Does attribute occur on the same day of the value date? |
month | Does attribute occur in the same month of the value date? |
year | Does attribute occur in the same year of the value date? |
matches | RegExp test against pattern (do not include outer slashes) |
1// ./config/plugins.js`
2'use strict';
3
4module.exports = {
5 'do-not-delete': {
6 config: {
7 contentTypes: {
8 'api::page.page': [
9 // Equality.
10 ['slug', 'is', 'home'],
11 ['slug', 'isNot', 'test'],
12
13 // Contains.
14 ['slug', 'in', ['home', 'blog', '404']],
15 ['slug', 'notIn', ['test', 'temp']],
16 ['slug', 'has', 'admin'],
17 ['slug', 'hasNot', '-test'],
18
19 // Regular expression.
20 ['slug', 'matches', '^foobar'], // same as "starts with"
21 ['slug', 'matches', 'foobar$'], // same as "ends with"
22
23 // Greater than or equal to.
24 ['rating', 'lt', 10],
25 ['rating', 'lte', 9],
26 ['rating', 'gt', 4],
27 ['rating', 'gte', 5],
28 ['rating', 'between', [3, 6]],
29
30 // Dates (any valid date string format can be used).
31 ['publishedAt', 'after', '2022-12-31'],
32 ['publishedAt', 'before', '2020-01-01T00:00:00.000Z'],
33 ['publishedAt', 'day', 'Wed, 01 Jan 2020 00:00:00 GMT'],
34 ['publishedAt', 'month', 'January 2020'],
35 ['publishedAt', 'year', '2023'],
36 ],
37 },
38 },
39 },
40};
When attempting to delete a protected entry, a validation error will display at the top of the screen that reads:
This protected entry cannot be deleted.
If you find yourself in a scenario where a protected entry actually does need to be deleted, you must first update the plugin config to remove the protection rule that applies to that entry.
Remember to rebuild your app after making changes to some config or other code.
yarn build
# OR
yarn develop
If you are enjoying this plugin and feel extra appreciative, you can buy me a beer or 3 🍺🍺🍺.
id
.npm install strapi-plugin-do-not-delete
Check out the available plugin resources that will help you to develop your plugin or provider and get it listed on the marketplace.